【问题标题】:Update SQL database with preparedStatement in Java在 Java 中使用preparedStatement 更新SQL 数据库
【发布时间】:2016-10-19 09:03:43
【问题描述】:

我有一个带有 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

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

【问题讨论】:

  • 您的问题是什么?如果要更新行,则需要使用 UPDATE 语句,而不是 INSERT 语句。另外:您使用它的方式,TB01 是一个 table 而不是“数据库”。
  • 对不起,如果它是唯一序列,我希望代码插入更新,或者如果已经输入,则更新数据库中的一行。你是对的,TB01 是表,DB01 是数据库。
  • 在 Derby 中,您可以为此使用 MERGE 语句。不相关,但是:您应该永远将数字或日期作为字符串传递。 始终使用适当的数据类型。 setInt() 用于整数,setDate() 用于日期,setTimestamp() 用于时间戳等

标签: java sql derby


【解决方案1】:

代码现在可以工作了。感谢 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 命令上更改顺序。

【讨论】:

    【解决方案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);
    }
    

    【讨论】:

    • 谢谢。但是,我在 FINISHT1 处使用此代码时遇到错误,其中 SERIAL=?
    • 我把它改成了 FINISHT1=? where SERIAL=?... 但是代码不会执行
    • 在双引号 "" 中使用字符串对象,并更正您的数据库和表名,因为在您的问题中,您使用相同的名称来混淆我们
    猜你喜欢
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-14
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多