在 Java 中使用 preparedStatement 更新 SQL 数据库
Update SQL database with preparedStatement in Java
我有一个 java 应用程序和一个 SQL 数据库,使用 preparedStatement 将行插入数据库。我希望程序能够根据序列号(唯一)更新行。
Connection conn = null;
Statement st = null;
try {
conn = DriverManager.getConnection ("jdbc:derby://localhost:1527/db01", "Administrator", "admin"); //run procedure getConnection to connect to the database - see below
st = conn.createStatement(); //set up a statement st to enable you to send SQL statements to the database.
} catch (SQLException ex) {
Logger.getLogger(FormTwo1.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println ("Successful Connection");
...
String query = "insert into TB01(SERIAL,BLADETYPE,STARTT1,AIRT1,FOAMT1,SCT1,FINISHT1) values (?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(query)) {
pstmt.setString(1, bladeSerial);
pstmt.setString(2, itemText);
pstmt.setString(3, String.valueOf(startTime1));
pstmt.setString(4, String.valueOf(airTime1));
pstmt.setString(5, String.valueOf(foamTime1));
pstmt.setString(6, String.valueOf(scTime1));
pstmt.setString(7, String.valueOf(finishTime1));
pstmt.executeUpdate();
} catch (SQLException ex) {
// Exception handling
Logger.getLogger(FormTwo1.class.getName()).log(Level.SEVERE, null, ex);
}
其中 serial、bladetype 是 VARCHAR,startT1、foamTime1、scTime1 和 finishTime1 都是 LocalTime 变量(因此 string.valueof 用于格式化)。
数据库是db01,table是TB01
我希望程序 insert/update 记录取决于序列号是否已经在数据库中。
try{
String host="jdbc:derby://localhost:1527/databasename";
String user="database_user_name";
String pass="database_password";
Connection con=DriverManager.getConnection(host,user,pass);
PreparedStatement stmt=con.prepareStatement("update table_name set BLADETYPE=?,STARTT1=?,AIRT1=?,FOAMT1=?,SCT1=?,FINISHT1, where SERIAL=?");
stmt.setString(1, itemText);
stmt.setString(2, String.valueOf(startTime1));
stmt.setString(3, String.valueOf(airTime1));
stmt.setString(4, String.valueOf(foamTime1));
stmt.setString(5, String.valueOf(scTime1));
stmt.setString(6, String.valueOf(finishTime1));
stmt.setString(7, bladeSerial);
stmt.executeUpdate();
}
catch (SQLException ex) {
Logger.getLogger(FormTwo1.class.getName()).log(Level.SEVERE, null, ex);
}
代码现在可以运行了。感谢 Prashant 的回答。稍作调整后效果很好
String query = ("UPDATE TB01 SET BLADETYPE=?,STARTT1=?,AIRT1=?,FOAMT1=?,SCT1=?,FINISHT1=? WHERE SERIAL=?");
try (PreparedStatement pstmt = conn.prepareStatement(query)) {
pstmt.setString(7, bladeSerial);
pstmt.setString(1, itemText);
pstmt.setString(2, String.valueOf(startTime1));
pstmt.setString(3, String.valueOf(airTime1));
pstmt.setString(4, String.valueOf(foamTime1));
pstmt.setString(5, String.valueOf(scTime1));
pstmt.setString(6, String.valueOf(finishTime1));
pstmt.executeUpdate();
}
catch (SQLException ex) {
// Exception handling
Logger.getLogger(FormTwo1.class.getName()).log(Level.SEVERE, null, ex);
}
请注意,由于 SERIAL 已移至字符串的末尾,因此还需要更改 setString 命令的顺序。
我有一个 java 应用程序和一个 SQL 数据库,使用 preparedStatement 将行插入数据库。我希望程序能够根据序列号(唯一)更新行。
Connection conn = null;
Statement st = null;
try {
conn = DriverManager.getConnection ("jdbc:derby://localhost:1527/db01", "Administrator", "admin"); //run procedure getConnection to connect to the database - see below
st = conn.createStatement(); //set up a statement st to enable you to send SQL statements to the database.
} catch (SQLException ex) {
Logger.getLogger(FormTwo1.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println ("Successful Connection");
...
String query = "insert into TB01(SERIAL,BLADETYPE,STARTT1,AIRT1,FOAMT1,SCT1,FINISHT1) values (?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(query)) {
pstmt.setString(1, bladeSerial);
pstmt.setString(2, itemText);
pstmt.setString(3, String.valueOf(startTime1));
pstmt.setString(4, String.valueOf(airTime1));
pstmt.setString(5, String.valueOf(foamTime1));
pstmt.setString(6, String.valueOf(scTime1));
pstmt.setString(7, String.valueOf(finishTime1));
pstmt.executeUpdate();
} catch (SQLException ex) {
// Exception handling
Logger.getLogger(FormTwo1.class.getName()).log(Level.SEVERE, null, ex);
}
其中 serial、bladetype 是 VARCHAR,startT1、foamTime1、scTime1 和 finishTime1 都是 LocalTime 变量(因此 string.valueof 用于格式化)。
数据库是db01,table是TB01
我希望程序 insert/update 记录取决于序列号是否已经在数据库中。
try{
String host="jdbc:derby://localhost:1527/databasename";
String user="database_user_name";
String pass="database_password";
Connection con=DriverManager.getConnection(host,user,pass);
PreparedStatement stmt=con.prepareStatement("update table_name set BLADETYPE=?,STARTT1=?,AIRT1=?,FOAMT1=?,SCT1=?,FINISHT1, where SERIAL=?");
stmt.setString(1, itemText);
stmt.setString(2, String.valueOf(startTime1));
stmt.setString(3, String.valueOf(airTime1));
stmt.setString(4, String.valueOf(foamTime1));
stmt.setString(5, String.valueOf(scTime1));
stmt.setString(6, String.valueOf(finishTime1));
stmt.setString(7, bladeSerial);
stmt.executeUpdate();
}
catch (SQLException ex) {
Logger.getLogger(FormTwo1.class.getName()).log(Level.SEVERE, null, ex);
}
代码现在可以运行了。感谢 Prashant 的回答。稍作调整后效果很好
String query = ("UPDATE TB01 SET BLADETYPE=?,STARTT1=?,AIRT1=?,FOAMT1=?,SCT1=?,FINISHT1=? WHERE SERIAL=?");
try (PreparedStatement pstmt = conn.prepareStatement(query)) {
pstmt.setString(7, bladeSerial);
pstmt.setString(1, itemText);
pstmt.setString(2, String.valueOf(startTime1));
pstmt.setString(3, String.valueOf(airTime1));
pstmt.setString(4, String.valueOf(foamTime1));
pstmt.setString(5, String.valueOf(scTime1));
pstmt.setString(6, String.valueOf(finishTime1));
pstmt.executeUpdate();
}
catch (SQLException ex) {
// Exception handling
Logger.getLogger(FormTwo1.class.getName()).log(Level.SEVERE, null, ex);
}
请注意,由于 SERIAL 已移至字符串的末尾,因此还需要更改 setString 命令的顺序。