循環中執行查詢嘗試下面的代碼來存儲數組列表的所有數據(表),但不給任何錯誤或所需的輸出。我如何在Java
public class Dbops {
String url = "jdbc:mysql://localhost:3306/ITStuffDB";
String username = "root";
String password = "";
ResultSet rs = null;
public boolean addData(ArrayList<ArrayList<String>> table){
try {
System.out.println(table.size());
for (int i = 0; i < table.size(); i++) {
Connection con1 = (Connection) DriverManager.getConnection(url, username, password);
String query = "INSERT INTO Data (Col1,Col2,Col3) VALUES (?,?,?)";
PreparedStatement pst1 = (PreparedStatement) con1.prepareStatement(query);
pst1.setString(1, table.get(i).get(0));
pst1.setString(2, table.get(i).get(1));
pst1.setString(3, table.get(i).get(2));
pst1.executeUpdate();
pst1.close();
con1.close();
}return true;
} catch (Exception e) {
return false;
}
}
}
我該如何正確處理?
你正在捕捉所有的異常,所以當然你不會看到任何錯誤。如果你沒有捕捉到異常(或者至少打印堆棧跟蹤),你可能會知道哪裏出了問題。 – Eran