gpt4 book ai didi

java - 数据写入错误

转载 作者:行者123 更新时间:2023-11-29 02:21:42 25 4
gpt4 key购买 nike

我正在向 mySql 数据库中插入一些数据。由于某些未知原因,数据未插入到我的表中。这适用于 SQLITE。这是我的代码:

try {

Class.forName("com.mysql.jdbc.Driver");

Connection conn = DriverManager.getConnection(url, username, password);



String query1 = "insert into Project (uniqueid,name,address,startingdate,estcompletion,engname) values(?,?,?,?,?,?)";
String query2 = "insert into EngineerData (UniqueId,Name,Password) values(?,?,?)";
String query3 = "insert into " +tableName +" (UniqueId,Category,Item,EstimatedQuantity,Unit,UnitPrice,TotalPrice,TotalSpent) values(?,?,?,?,?,?,?,?)";
String query4 = "insert into ProjectImages (uniqueId, Path) values(?,?)";

PreparedStatement pst1= conn.prepareStatement(query1);
PreparedStatement pst2 = conn.prepareStatement(query2);
PreparedStatement pst3 = conn.prepareStatement(query3);
PreparedStatement pst4 = conn.prepareStatement(query4);

pst1.setInt(1, uniqueID);
pst1.setString(2, projectName.getText());
pst1.setString(3, address.getText());
pst1.setString(4, day1.getText()+"/"+month1.getText()+"/"+year1.getText());
pst1.setString(5, day2.getText()+"/"+month2.getText()+"/"+year2.getText());
pst1.setString(6, engineerName.getText());

pst2.setInt(1, uniqueID);
pst2.setString(2, engineerName.getText());
pst2.setString(3, engineerPassword.getText());

try{
for (int j = 0; j < table.getRowCount(); j++ ){

pst3.setInt(1, uniqueID);
pst3.setString(2, table.getValueAt(j, 0).toString());
pst3.setString(3, table.getValueAt(j, 1).toString());
pst3.setString(4, table.getValueAt(j, 2).toString());
pst3.setString(5, table.getValueAt(j, 3).toString());
pst3.setString(6, table.getValueAt(j, 4).toString());
pst3.setString(7, table.getValueAt(j, 5).toString());

pst3.setDouble(8, 0.0);
pst3.execute();

}}catch(Exception e3){
/*JOptionPane.showMessageDialog(null, e3);
*
*/
}

pst4.setInt(1, uniqueID);
pst4.setString(2, null);

pst1.execute();
pst2.execute();
pst4.execute();

System.out.println(pst1.toString());
System.out.println(pst2.toString());

pst1.close();
pst2.close();
pst3.close();
pst4.close();



conn.close();

}

最佳答案

您应该省略自动生成的列“uniqueID”并重写您的代码。例如。

String query4 = "insert into ProjectImages (Path) values(?)";

另一种方法 - 为自动生成的列传递空值。例如。

String query4 = "insert into ProjectImages (uniqueId, Path) values(null,?)";

关于java - 数据写入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30637504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com