Java中的SQLite3-如何确定我的连接是否有效并且表是否存在?

发布于 2025-02-01 17:39:49 字数 721 浏览 2 评论 0原文

我正在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别念他 2025-02-08 17:39:49

要独立于基础DB-引擎,您可以使用Java元数据来找出是否存在对象。要查找表,您可以使用类似的内容:(我已复制上面的代码)

public Connection connect() {

// The database to connect to
String database = "C:Program Files/sqlite/points.db";

// The tablename, we are checking for
String tableName = "points";

// TableTypes might be: "TABLE", "VIEW", "ALIAS","SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM" to find all
// in our case, we are just interested in the table
String[] tableTypes = {"TABLE"};

ResultSet rs = null;

try {
    String url = "jdbc:sqlite:" + database ;
    // create a connection to the database
    Connection connection = DriverManager.getConnection(url);
    if( connection.isValid(100) ) {

       // get Information about the Database
       DatabaseMetaData dmd = connection.getMetaData(); 

       // I'm not experienced with SQLite, evtl. database can be null as well                 
       rs = dmd.getTables(database,null,tableName,tableTypes);

       // is there a result?
       if ( rs.next) {
          return connection;
       ) else {
         // the Table does not exist, so we can create it and return the connection or directly return null
         
       }
    }
    return null;
} catch (SQLException e) {
    System.out.println("Database connection failed" + e.getMessage());
    return null;
} finally {
    if ( rs != null ) {
      try {
        rs.close();
      } catch ( SQLException sqlex ) {
          System.out.println("Exception while closing resultset. " + sqlex.getMessage();
      }
    }
}
}

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)

public Connection connect() {

// The database to connect to
String database = "C:Program Files/sqlite/points.db";

// The tablename, we are checking for
String tableName = "points";

// TableTypes might be: "TABLE", "VIEW", "ALIAS","SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM" to find all
// in our case, we are just interested in the table
String[] tableTypes = {"TABLE"};

ResultSet rs = null;

try {
    String url = "jdbc:sqlite:" + database ;
    // create a connection to the database
    Connection connection = DriverManager.getConnection(url);
    if( connection.isValid(100) ) {

       // get Information about the Database
       DatabaseMetaData dmd = connection.getMetaData(); 

       // I'm not experienced with SQLite, evtl. database can be null as well                 
       rs = dmd.getTables(database,null,tableName,tableTypes);

       // is there a result?
       if ( rs.next) {
          return connection;
       ) else {
         // the Table does not exist, so we can create it and return the connection or directly return null
         
       }
    }
    return null;
} catch (SQLException e) {
    System.out.println("Database connection failed" + e.getMessage());
    return null;
} finally {
    if ( rs != null ) {
      try {
        rs.close();
      } catch ( SQLException sqlex ) {
          System.out.println("Exception while closing resultset. " + sqlex.getMessage();
      }
    }
}
}
牛↙奶布丁 2025-02-08 17:39:49

有些挖了一些解决方案。

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) ) {
            String sql =  "SELECT * FROM sqlite_master where type = ? and tbl_name = ? ";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "table");
            preparedStatement.setString(2, "points");
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next() && resultSet.getString("name").equals("points") ) {
                resultSet.close();
                return connection;
            }
        }
        return null;
    } catch (SQLException e) {
        System.out.println("Database connection failed" + e.getMessage());
        return null;
    }
}

查询可以使用哪些表的查询可能不需要是准备好的语句。

Did some digging around and found a solution.

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) ) {
            String sql =  "SELECT * FROM sqlite_master where type = ? and tbl_name = ? ";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "table");
            preparedStatement.setString(2, "points");
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next() && resultSet.getString("name").equals("points") ) {
                resultSet.close();
                return connection;
            }
        }
        return null;
    } catch (SQLException e) {
        System.out.println("Database connection failed" + e.getMessage());
        return null;
    }
}

The query to check what tables are available likely doesn't need to be a prepared statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文