什么查询将更新mysql数据库中的数据?

时间:2013-07-29 16:41:05

标签: java mysql sql jdbc

我有一个更新数据库中的值的查询,但我似乎错过了一些东西

String sqlStr = "INSERT INTO userdb where userID=? (username,address,email,contact,credit,userpassword)" + "VALUES (?,?,?,?,?,?)";
                PreparedStatement pstmt = conn.prepareStatement(sqlStr);
                pstmt.setInt(1, userid);
                pstmt.setString(2,name);
                pstmt.setString(3,address);
                pstmt.setString(4,email);
                pstmt.setInt(5,contact);
                pstmt.setString(6,credit);
                pstmt.setString(7,password);
                int rec = pstmt.executeUpdate();

错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where userID=1 (username,address,email,contact,credit,userpassword)VALUES ('abc'' at line 1

2 个答案:

答案 0 :(得分:2)

UPDATE userdb 
SET username = ?,
    address = ?, 
    email = ?,
    contact = ?,
    credit = ?,
    userpassword = ?
WHERE userID = ? 

答案 1 :(得分:2)

UPDATE语句用于更新表中的记录。使用更新查询修改代码。

  String sqlStr = "UPDATE userdb
                   SET username=?,
                       address = ?,
                       email = ?,
                       contact = ?,
                       credit = ?,
                       userpassword = ?
                    WHERE userID = ? ;";

   PreparedStatement pstmt = conn.prepareStatement(sqlStr);
   pstmt.setString(1,name);
   pstmt.setString(2,address);
   pstmt.setString(3,email);
   pstmt.setInt(4,contact);
   pstmt.setString(5,credit);
   pstmt.setString(6,password);
   pstmt.setInt(7, userid);

   int rec = pstmt.executeUpdate();