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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17929501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 19:39:20  来源:igfitidea点击:

What query will update data in mysql database?

javamysqlsqljdbc

提问by KAKAK

I have a query that is updating values in the database however i seem to be missing out something

我有一个查询正在更新数据库中的值,但是我似乎遗漏了一些东西

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

error:

错误:

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

回答by juergen d

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

回答by Ankur Lathi

The UPDATE statement is used to update records in a table. Modify your code with update query.

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