使用Java中的preparedStatement更新SQL数据库

时间:2016-10-19 09:03:43

标签: java sql derby

我有一个带有SQL数据库的Java应用程序,使用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,表是TB01

我希望程序根据序列号是否已经在db中插入/更新记录。

2 个答案:

答案 0 :(得分:0)

代码现在正在运行。感谢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命令。

答案 1 :(得分:-2)

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);
}