Simmer Slow
Simmer Slow

Reputation: 31

How to fix 'java.sql.SQLException: The database has been closed' problem

My code says that there are no errors, but when I go to run the program it will say that the database has closed.

public class main
{
public static void main(String[] args)
{
    //language=SQLite
    String sql = "SELECT * FROM titles WHERE title_id = ?";

    //Create Scanner

    try
    {
        String url = "jdbc:sqlite:C:\\CmpSci 221\\Lab 13\\src\\main\\resources";
        Connection connection = DriverManager.getConnection( url );

        PreparedStatement statement = connection.prepareStatement( sql );
        statement.setInt( 1,1);

        ResultSet rSet = statement.executeQuery();

        while( rSet.next() )
        {
            System.out.println( rSet.getString( "title" ) );
        }
    }
    catch ( SQLException e )
    {
        e.printStackTrace();
    }
}

}

I expect it to output the database that I am working with but it just outputs this error message:

java.sql.SQLException: The database has been closed

Upvotes: 2

Views: 4619

Answers (2)

Luke Woodward
Luke Woodward

Reputation: 65044

Your SQLite JDBC URL needs to point to a file but it is pointing to a directory instead.

I've reproduced your error attempting to open a SQLite database but changing the connection URL to point to a directory on my machine. I can't say why you get an error about the database being closed: I would regard that as a bug in the driver.

You need to change the URL to a point to file instead, for example:

        String url = "jdbc:sqlite:C:\\CmpSci 221\\Lab 13\\src\\main\\resources\\your_database.db";

Upvotes: 2

Gulshan Yadav
Gulshan Yadav

Reputation: 451

Make sure you are passing right URL inside DriverManager.getConnection()

If URL is correct then make sure you have created the database at that location.

It seems the database is not found.

Upvotes: 2

Related Questions