Java中的SQLite3-如何确定我的连接是否有效并且表是否存在?
我正在Java制作游戏,其中将一些数据存储在单个SQLITE表中。由于游戏不是强制性的DB连接,因此,如果发生某些错误,我希望它可以播放,但是在隐藏任何会导致数据库查询或更新的内容时。因此,我想确定连接是否有效以及我的表是否存在。
这是我现有的连接到它的代码:
public Connection connect() {
try {
String url = "jdbc:sqlite:C:Program Files/sqlite/points.db";
// create a connection to the database
Connection connection = DriverManager.getConnection(url);
if( connection.isValid(100)) {
return connection;
}
return null;
} catch (SQLException e) {
System.out.println("Database connection failed" + e.getMessage());
return null;
}
}
但是,即使points.db
在指定的位置不存在,这仍然返回连接。
I'm making a game in Java where I store some data in a single SQLite table. Since a db connection isn't mandatory for the game, I want it playable if some error happens, but while hiding anything that would lead to a database query or update. Therefore I want to determine if the connection is valid and if my table is present.
Here's my existing code for connecting to it:
public Connection connect() {
try {
String url = "jdbc:sqlite:C:Program Files/sqlite/points.db";
// create a connection to the database
Connection connection = DriverManager.getConnection(url);
if( connection.isValid(100)) {
return connection;
}
return null;
} catch (SQLException e) {
System.out.println("Database connection failed" + e.getMessage());
return null;
}
}
However this still returns the connection, even if points.db
does not exist at the specified location.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要独立于基础DB-引擎,您可以使用Java元数据来找出是否存在对象。要查找表,您可以使用类似的内容:(我已复制上面的代码)
To be independent of the underlying DB-engine, you can use Java MetaData to find out if an object exists. To find a table, you can use something like this: ( I've copied the code above)
有些挖了一些解决方案。
查询可以使用哪些表的查询可能不需要是准备好的语句。
Did some digging around and found a solution.
The query to check what tables are available likely doesn't need to be a prepared statement.