java 无法在 sqlite 数据库中插入超过 254 的数据
Can't insert data more than 254 in sqlite database by java
我正在使用 java class,它允许我将数据从字符串插入到 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
您可能 运行 没有连接,因为您没有在打开新连接之前按要求关闭它们。 运行 连接不足不应抛出 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]);
}
这里的重构和更新可能会有所帮助。
我假设 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;
}
}
希望对您有所帮助。
我正在使用 java class,它允许我将数据从字符串插入到 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
您可能 运行 没有连接,因为您没有在打开新连接之前按要求关闭它们。 运行 连接不足不应抛出 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]);
}
这里的重构和更新可能会有所帮助。
我假设 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;
}
}
希望对您有所帮助。