不能通过java在sqlite数据库中插入超过254的数据

时间:2015-03-01 15:02:17

标签: java windows sqlite netbeans netbeans-8

我正在使用一个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

2 个答案:

答案 0 :(得分: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]);
}

答案 1 :(得分:0)

这是一个可能有帮助的重新分解和更新。

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

我还添加了PreparedStatement的使用(对SQL注入攻击效率更高,风险更小)以及正确关闭资源(当然,在Java 7尝试使用资源之前)。

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

希望这有帮助。