1

I am using Java 11 JDBC and MySQL Connector/J 8 jar. All other CRUDs are running OK, but when I am trying to run SET foreign_key_checks = 0 or SET foreign_key_checks = 1, it shows syntax error with SQLState: 42000 and VendorError: 1064. So if there is any way to run such queries using JDBC?

try {
    Statement checks = connection.createStatement();
    checks.execute("set foreign_key_checks=0");
    checks.close();

    String tableName = json.getString("table");
    RowOperation rowOperation = RowOperation.valueOf(json.getString("activity"));
    JSONArray rows = json.getJSONArray("rows");

    for (Object obj : rows) {
        JSONObject row = (JSONObject) obj;
        List<String> keys = new ArrayList<>(row.keySet());
        StringBuilder sb = new StringBuilder();
        String columns = keys.stream().collect(Collectors.joining(",")) + ",rowOperation,rowCreatedOn";
        String questionMakrs = keys.stream().map(x -> "?").collect(Collectors.joining(",")) + ",?,?";
        String query = "insert into " + tableName + " (" + columns + ") values (" + questionMakrs + "); SET FOREIGN_KEY_CHECKS=0";
        PreparedStatement stmt = connection.prepareStatement(query);
        int i = 0;
        for (i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            stmt.setString(i + 1, String.valueOf(row.get(key)));
        }
        stmt.setString(i + 1, rowOperation.name());
        i++;
        stmt.setTimestamp(i + 1, Timestamp.valueOf(LocalDateTime.now()));
        stmt.execute();
        stmt.close();
    }

} finally {
    Statement checks = connection.createStatement();
    checks.execute("set foreign_key_checks=1");
    checks.close();
}
8
  • 1
    How does your code looks like?
    – Jens
    Aug 26, 2022 at 8:52
  • Sure. See stackoverflow.com/a/4120517/139985
    – Stephen C
    Aug 26, 2022 at 9:17
  • But if you are getting a 1064 error, that means you have an SQL syntax error. Hence we need to see what your code is actually doing. It could also be that the DB user doesn't have the required rights to set session variables.
    – Stephen C
    Aug 26, 2022 at 9:22
  • @Stephen C. I have added code snap. connection is JDBC connection object. I am getting error at first 3 lines. Error is
    – Vicky
    Aug 26, 2022 at 9:47
  • SQLException: 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 'SET FOREIGN_KEY_CHECKS=0' at line 1 SQLState: 42000 VendorError: 1064
    – Vicky
    Aug 26, 2022 at 9:47

1 Answer 1

1

You are trying to execute two statements, an INSERT followed by a SET, separated by a semicolon, in a single call to prepareStatement().

https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html

SQL syntax for prepared statements does not support multi-statements (that is, multiple statements within a single string separated by ; characters).

You must execute the SET statement in a separate statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.