Java MySQL executeUpdate SQLSyntaxErrorException

时间:2018-06-01 17:58:28

标签: java mysql sql jdbc

我刚刚开始在Java中使用MySQL,我正在尝试更新数据库中的现有数据。主要目的是创建一个计数器,当一个动作完成后,将在数据库中更新一个int。

在这种情况下,我试图通过在编译代码时增加整数来更新daily_search_count。您可以在下面看到我的数据库数据的图片: data within the database 我编写的代码旨在每次代码运行时将“daily_search_count”增加1。但不幸的是我收到以下错误:

Exception in thread "main" java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''accounts' set 'daily_search_count' = '4' where 'id' = '1'' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:118)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:960)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1116)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1066)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1396)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1051)
at Database.main(Database.java:29)

我无法看到我的代码有什么问题,如下所示:

import java.sql.*;

public class Database {

public static void main(String[] args) throws Exception {
    String host = "jdbc:mysql://localhost:3306/presearch";
    String username = "root";
    String password = "";
    String query = "select * from accounts";

    Connection con = DriverManager.getConnection(host, username, password);

    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery(query);

    String userData = "";

    while (rs.next()) {
        if (rs.getInt(4) < 33) {
            userData = rs.getInt(1) + " : " + rs.getString(2) + " daily search count : " + rs.getInt(4);
            System.out.println(userData);

            int counter = rs.getInt(4) + 1;
            PreparedStatement updatexdd = con.prepareStatement("update 'accounts' set 'daily_search_count' = '" + counter + "' where 'id' = '" + rs.getInt(1) + "'");
            int updatexdd_done = updatexdd.executeUpdate();
        }
    }

    st.close();
    con.close();

}

}

我希望有人能看到我做错了什么。

提前致谢!

1 个答案:

答案 0 :(得分:0)

您必须避免在代码中遇到一些问题:

  • 表和列的名称不应介于两个引号Date now = new Date(); // 2018-06-01 Date tomorrow = now.plus(1, DAY); // 2018-06-02
  • 之间
  • 确保使用占位符('accounts')在PreparedStatement
  • 的查询中指定您的属性
  • 确保关闭finally块中的连接和语句
  • 我还注意到您只需要一个查询

您的代码应如下所示:

?

在我的代码中,我使用支持public static void main(String[] args) throws Exception { String host = "jdbc:mysql://localhost:3306/presearch"; String username = "root"; String password = ""; String query = "update accounts set daily_search_count = daily_search_count + 1 where daily_search_count < 33"; try (Connection con = DriverManager.getConnection(host, username, password); PreparedStatement updatexdd = con.prepareStatement(query)) { int updatexdd_done = updatexdd.executeUpdate(); } } AutoCloseable接口的try-with-resources Statement,这意味着您无需关闭连接或声明。