准备参数中的陈述?

时间:2014-12-24 14:21:49

标签: java mysql sql jdbc

代码:

        java.sql.PreparedStatement pstmt=null;
        String sql = "UPDATE Person SET address = ? WHERE name = ? ";
        pstmt=thecon.prepareStatement(sql);
        String addVar= JOptionPane.showInputDialog("Enter the new address");
        String nameVar=JOptionPane.showInputDialog("Enter the Name");
        pstmt.setString(1 , addVar);
        pstmt.setString(2, nameVar);


        // Tell affected no of rows in table
        int num = pstmt.executeUpdate(sql);
      //Step 7: Process the results of the query
        System.out.println(num + " records updated"); 
        thecon.close();
    } catch (SQLException e) {
        System.out.println("sql problem:   "+e);
    }

例外:

  

sql问题:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:SQL语法中有错误;检查与您的MySQL服务器版本对应的手册,以便在'附近使用正确的语法?名字=?'在第1行

1 个答案:

答案 0 :(得分:-1)

你可以试试这个: String sql =“UPDATE Person SET address =?WHERE name =?;”;