简体   繁体   English

使用JDBC的Mysql中的更新查询问题

[英]Problem with Update query in Mysql with JDBC

  1. see the screenshots 看截图
  2. see the 2nd screenshot 见第二张截图
  3. see the 3rd screenshot 见第三张截图

Okay so I am building a project on java and mysql, I am stuck at this point that I have to update a data which is in MySql but from my java gui application, I've executed that update command from MySql command line client 好的,所以我要在Java和mysql上构建一个项目,此时我不得不更新MySql中的数据,但是从我的Java GUI应用程序中,我已经从MySql命令行客户端执行了update命令

update user set bldu = 50 where userid = 1001;

and it's working perfectly fine there but from my java application on clicking on assigned jbutton it says: 它在那里工作得很好,但是从我的Java应用程序中,单击分配的jbutton时,它说:

you have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'userid= 1001' at line 1 检查与您的MySQL服务器版本相对应的手册,以在第1行的'userid = 1001'附近使用正确的语法

Please help me..! 请帮我..!

String query = "update user SET bldu = " + bldut + " WHERE userid = " + uid + ";";

使用此查询代替您的旧查询可能会对您有所帮助。

In your first screenshot you must add a space before WHERE clause: 在第一个屏幕截图中,您必须在WHERE子句之前添加一个空格:

String query = "UPDATE user SET bdlu = " + bldut + "WHERE userid = " + uid + ";";

So your query will be interpretated as: 因此,您的查询将被解释为:

UPDATE user SET bdlu = 50WHERE userid = 1001

So you'll raise a syntax error. 因此,您将引发语法错误。

Then you'll have the following query: 然后,您将具有以下查询:

String query = "UPDATE user SET bdlu = " + bldut + " WHERE userid = " + uid + ";";

Try this snippet in your code. 在您的代码中尝试使用此代码段。

String query = "update user SET bldu = " + bldut + " WHERE userid = " + uid + ";";
Statement = con.prepareStatement(query);
Statement.executeUpdate();

by looking at your code you cannot store results of update query in resultSet the executeUpdate() only return 0 or 1 for success and failure of Update. 通过查看代码,您无法将更新查询的结果存储在resultSet中。executeUpdate()仅针对更新成功和失败返回0或1。

Okay i guys i have figured out something that it is working i mean this program is updating the data stored in mysql from netbeans via jdbc but it won't stop showing that error message like: 好的,伙计们,我已经弄清楚了它正在起作用,这意味着该程序正在通过jdbc从netbeans更新存储在mysql中的数据,但它不会停止显示以下错误消息:

"Can not issue data manipulation statements with executeQuery()" “无法通过executeQuery()发出数据操作语句”

everytime i click one that assigned jButton..! 每次我单击一个分配了jButton的按钮。 but i checked the database the value i want to change is being changed but then why it is showing this error..? 但我检查了数据库,我要更改的值正在更改,但是为什么它显示此错误..?

Please use this code in your java file, do changes according to your file. 请在您的Java文件中使用此代码,并根据您的文件进行更改。 your issue is you are using the same query in a result set that already uses for the update 您的问题是,您正在已用于更新的结果集中使用相同的查询

Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/bdb", "root", "root"); 连接conn = DriverManager.getConnection(“ jdbc:mysql:// localhost:3306 / bdb”,“ root”,“ root”);

try { 尝试{

    String query = "update user SET bldu = " + bldut+ " WHERE userid = " + uid + ";";           
    // create the java mysql update preparedstatement
    Class.forName("com.mysql.jdbc.Driver").newInstance();

    PreparedStatement preparedStmt = conn.prepareStatement(query);
    preparedStmt.executeUpdate();

    query = "select * from user  WHERE userid = " + uid +";";
    ResultSet rs = stmt.executeQuery(query);
    // STEP 5: Extract data from result set
    while (rs.next()) {
        // Retrieve by column name
        String userid = rs.getString("userid");
        String userfname = rs.getString("userfname");
        // all your column
        // Display values
        System.out.print("userid: " + userid);
    }
    // STEP 6: Clean-up environment
    rs.close();
    stmt.close();
    conn.close();
} catch (Exception e) {
    System.err.println("Got an exception! ");
    System.err.println(e.getMessage());
} finally {
    // finally block used to close resources
    try {
        if (conn != null)
            conn.close();
    } catch (SQLException se) {
        se.printStackTrace();
    }// end finally try
}// end try

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM