如何解决"java.sql.SQLException:数据库已关闭"的问题 [英] How to fix 'java.sql.SQLException: The database has been closed' problem

查看:247
本文介绍了如何解决"java.sql.SQLException:数据库已关闭"的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码说没有错误,但是当我运行该程序时,它将说数据库已关闭.

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:数据库已关闭

java.sql.SQLException: The database has been closed

推荐答案

您的SQLite JDBC URL需要指向文件,但它指向目录.

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

我已重现您尝试打开SQLite数据库但将连接URL更改为指向本机目录的错误.我不能说为什么您会收到有关数据库关闭的错误消息:我认为这是驱动程序中的错误.

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.

您需要将URL更改为指向文件的指向,例如:

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";

这篇关于如何解决"java.sql.SQLException:数据库已关闭"的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆