【问题标题】:Can't insert data more than 254 in sqlite database by java无法通过java在sqlite数据库中插入超过254的数据
【发布时间】:2015-03-01 15:02:17
【问题描述】:

我正在使用一个 java 类,它允许我将数据从字符串插入 SQLite 数据库。我通过循环插入数据。我的字符串有 260 个数据。当我尝试将这些数据从字符串插入到 sqlite 数据库时,它工作正常,但每次都停在 254 的位置!为什么?!

for(i = 0; i < 260; i++)
    {
        try {
            Class.forName("org.sqlite.JDBC");
            connection = DriverManager.getConnection("jdbc:sqlite:D:\\new.db");
            java.sql.Statement statement = connection.createStatement();
            statement .executeUpdate("INSERT INTO suggestion (suggesting) VALUES('"+words[i]+"')");
             System.out.println(i + " - " + words[i]);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Word.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

这是来自 netbeans 的错误日志!

Exception in thread "main" java.lang.NullPointerException
    at org.sqlite.NestedDB$CausedSQLException.fillInStackTrace(NestedDB.java:442)
    at java.lang.Throwable.<init>(Throwable.java:250)
    at java.lang.Exception.<init>(Exception.java:54)
    at java.sql.SQLException.<init>(SQLException.java:140)
    at org.sqlite.NestedDB$CausedSQLException.<init>(NestedDB.java:435)
    at org.sqlite.NestedDB._open(NestedDB.java:63)
    at org.sqlite.DB.open(DB.java:77)
    at org.sqlite.Conn.<init>(Conn.java:88)
    at org.sqlite.JDBC.connect(JDBC.java:64)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:233)
    at test.Word.main(Word.java:106)
Java Result: 1

【问题讨论】:

  • 您确定该数组的值有效吗?
  • 除了假设驱动程序拒绝创建另一个连接之外,我无法评论空指针,因为您一直在创建连接并且从不关闭它们中的任何一个。上面的代码非常需要重新- 使用准备好的语句、单个连接和正确的错误检查的因素。

标签: java windows sqlite netbeans netbeans-8


【解决方案1】:

您可能用完了连接,因为在打开新连接之前您没有按要求关闭它们。连接用完不应该抛出NullPointerException;这似乎是 sqlite 中的一个错误。

更好更快的方法是:不要关闭连接,但不要每次都打开新连接

你可以这样做:

try {
    Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException ex) {
    Logger.getLogger(Word.class.getName()).log(Level.SEVERE, null, ex);
}
connection = DriverManager.getConnection("jdbc:sqlite:D:\\new.db");

for(i = 0; i < 260; i++)
{
    java.sql.Statement statement = connection.createStatement();
    try {
        statement.executeUpdate("INSERT INTO suggestion (suggesting) VALUES('"+words[i]+"')");
    } finally {
        // Statements, like connections, also need to be closed after use
        statement.close();
    }
    System.out.println(i + " - " + words[i]);
}

【讨论】:

  • 非常感谢! :D 我已经通过循环创建了这么多连接!现在解决了!
【解决方案2】:

这里有一个重构和更新可能会有所帮助。

我假设 SQLite 驱动程序不喜欢您在使用它们后不关闭连接的事实(实际的 NullPointer 可能是您正在使用的库中的一个错误)。

我还添加了PreparedStatement 的使用(这将更有效且对 SQL 注入攻击的风险更小)以及正确关闭资源(当然是在 Java 7 try-with-resources 之前)。

    Connection connection = null;
    PreparedStatement insert = null;
    try {
        Class.forName("org.sqlite.JDBC");
        connection = DriverManager.getConnection("jdbc:sqlite:D:\\new.db");
        insert = connection.prepareStatement("INSERT INTO suggestion (suggesting) VALUES(?)");
        for (String word : words) {                
            insert.setString(1, word);
            System.out.println("Word "+word+" inserted");
        }
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Word.class.getName()).log(Level.SEVERE, "Driver not found", ex);
    } catch (SQLException sqle) {
        Logger.getLogger(Word.class.getName()).log(Level.SEVERE, "Could not execute SQL", sqle);
    } finally {
        if (insert != null) {
            try {
                insert.close();
            } catch (SQLException sqle) {
                Logger.getLogger(Word.class.getName()).log(Level.SEVERE, "Could not close prepared statement", sqle);
            }
            insert = null;
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException sqle) {
                Logger.getLogger(Word.class.getName()).log(Level.SEVERE, "Could not close connection", sqle);
            }
            connection = null;
        }
    }

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多