【发布时间】:2019-04-13 17:27:37
【问题描述】:
我的代码说没有错误,但是当我去运行程序时它会说数据库已经关闭。
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();
}
}
}
我希望它输出我正在使用的数据库,但它只输出以下错误消息:
java.sql.SQLException: 数据库已关闭
【问题讨论】:
-
@MustafaÇil 是 not 该问题的重复 - OP 在这里没有使用 try-with-resource,这不可能是问题
-
String url = "jdbc:sqlite:C://CmpSci 221/Lab 13/src/main/resources.db";。添加调试器并在 getConnection 行后验证连接对象是否可用 -
请发布完整的异常堆栈跟踪。