[英]SQL database not populating via Java by using execute/addBatch
我目前有一個非常大的文件,其中包含幾百萬行條目,並希望它們插入到數據庫中。 從java到SQL建立的連接起作用,因為我嘗試單獨插入數據並且它可以工作,但是,當我切換到使用executeBatch
和addBatch
,它似乎循環但不填充到我的數據庫中。
代碼如下:
import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class fedOrganiser6 {
private static String directory = "C:\\Users\\x\\Desktop\\Files\\";
private static String file = "combined.fed";
private static String mapperValue = "";
public static void main(String[] args) throws Exception {
Connection conn = null;
try {
BufferedReader mapper = new BufferedReader(new FileReader(directory + file));
String dbURL = "jdbc:sqlserver://localhost\\SQLExpress;database=TIMESTAMP_ORGANISER;integratedSecurity=true";
String user = "sa";
String pass = "password";
conn = DriverManager.getConnection(dbURL, user, pass);
if (conn != null) {
DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData();
System.out.println("Driver name: " + dm.getDriverName());
System.out.println("Driver version: " + dm.getDriverVersion());
System.out.println("Product name: " + dm.getDatabaseProductName());
System.out.println("Product version: " + dm.getDatabaseProductVersion());
System.out.println("clearing database");
conn.createStatement().executeUpdate("truncate table TimestampsStorage");
System.out.println("bulk insert into database");
System.out.println("complete");
int i = 0;
int records = 0;
String query = "INSERT INTO TimestampsStorage " + "values(" + "'" + mapperValue.toString() + "'"+ ")";
conn.prepareStatement(query);
for (mapperValue = mapper.readLine(); mapperValue != null; mapperValue = mapper.readLine()) {
i++;
records++;
System.out.println("Batching " + records + " records...");
conn.createStatement().addBatch(query);
if (i == 100000) {
conn.createStatement().executeBatch();
i = 0;
}
}
}
conn.createStatement().executeBatch();
conn.createStatement().close();
System.out.print("Done");
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
createStatement()
創建一個新的語句對象,因此您執行的語句與您正在批處理的語句不同。 您應該創建一次PreparedStatement
,向其添加幾個批次,然后在同一個對象上執行:
String query = "INSERT INTO TimestampsStorage VALUES (?)";
PreparedStatement ps = conn.prepareStatement(query);
for (mapperValue = mapper.readLine();
mapperValue != null;
mapperValue = mapper.readLine()) {
i++;
records++;
System.out.println("Batching " + records + " records...");
ps.setString(1, mapperValue);
ps.addBatch();
if (i == 100000) {
ps.executeBatch();
i = 0;
}
}
你扔掉了准備好的陳述
String query = "INSERT INTO TimestampsStorage VALUES (?)";
PreparedStatement statement = conn.prepareStatement(query);
for (mapperValue = mapper.readLine(); mapperValue != null; mapperValue = mapper.readLine()) {
i++;
records++;
System.out.println("Batching " + records + " records...");
statement.setString(1,mapperValue);
statement.addBatch();
if (i == 100000) {
statement.executeBatch();
i = 0;
}
我認為你對JDBC的批處理工作方式有點誤解。
每次調用conn.createStatement()
時,您都在創建一個新的Statement
。
相反,您將需要使用PreparedStatement
。 首先,更改您的查詢以包含?
你想要你的價值觀去哪里。
String query = "INSERT INTO TimestampsStorage VALUES(?)";
然后,當您調用conn.prepareStatement(query)
,存儲返回的PreparedStatement
。
PreparedStatement ps = conn.prepareStatement(query);
然后,這個PreparedStatement
將“記住”您的查詢,您可以簡單地更改您想要的值在哪里?
是循環的每次迭代。
ps.setString(1, mapperValue);
setString
方法將使用mapperValue
並使用它而不是第一個?
它在您的查詢中找到(因為您傳入索引1)。
然后,不是調用conn.createStatement().addBatch()
,而是調用ps.addBatch()
。
然后,在循環之外,您可以調用ps.executeBatch()
。 (無需在循環內調用它,因此可以刪除if (i == 100000)
條件)。
最后,如果您使用的是Java 7+,則可以嘗試使用資源,這樣您就不必擔心在finally塊中關閉PreparedStatement
或Connection
。
這是你的最終結果應該是什么樣子。
String query = "INSERT INTO TimestampsStorage VALUES (?)";
try (Connection con = DriverManager.getConnection(dbURL, user, pass); PreparedStatement ps = con.prepareStatement(query);) {
for (mapperValue = mapper.readLine(); mapperValue != null; mapperValue = mapper.readLine()) {
records++;
ps.setString(1, mapperValue);
ps.addBatch();
}
System.out.println("Executing batch of " + records + " records...");
ps.executeBatch();
} catch (SQLException ex) {
//handle exception
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.