My previous post! , after which I changed the code, give an example:

protected static boolean postDatabase(String [] loginData, HttpServletResponse response) throws IOException { try { output = response.getWriter(); Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(URL,USER_NAME,PASSWORD); String checkingUser = "SELECT nick_name,email,password FROM users WHERE nick_name = ?"; PreparedStatement preparedStatement = connection.prepareStatement(checkingUser); preparedStatement.setString(1,loginData[0]); ResultSet resultSet = preparedStatement.executeQuery(checkingUser); int passwordColumn = resultSet.findColumn("password"); if(resultSet.getString(passwordColumn).equals(loginData[1])) { int emailColumn = resultSet.findColumn("email"); if(resultSet.getString(emailColumn).equals(loginData[2])){ return true; } } } catch (IOException ioError) { ResponseDataHandler.ToJSONresponse(ioError.toString(),response); } catch (ClassNotFoundException notFound) { ResponseDataHandler.ToJSONresponse(notFound.toString(),response); } catch (SQLException sql) { ResponseDataHandler.ToJSONresponse(sql.toString(),response); }finally { try { connection.close(); } catch (SQLException sql) { ResponseDataHandler.ToJSONresponse(sql.toString(),response); } } return false; } } 

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the syntax to use mySQL? at line 1

But this code below works:

 protected void postDataBase(User user, HttpServletResponse response) throws IOException { try { output = response.getWriter(); System.out.print(user); Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(URL,USER_NAME,PASSWORD); String creatingUser = "INSERT INTO coupon_system.users(nick_name, first_name, last_name, password, email) VALUES (?,?,?,?,?)"; String creatingUserRole = "INSERT INTO roles (rol, nickname) VALUES (?,?)"; PreparedStatement createUser = connection.prepareStatement(creatingUser); createUser.setString(1,user.getNickName()); createUser.setString(2,user.getFirstName()); createUser.setString(3,user.getLastName()); createUser.setString(4,user.getPassword()); createUser.setString(5,user.getEmail()); createUser.execute(); PreparedStatement createRole = connection.prepareStatement(creatingUserRole); createRole.setString(1,user.getCustomerType()); createRole.setString(2,user.getNickName()); createRole.execute(); ResponseDataHandler.ToJSONresponse("Success",response); } catch (com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException duplicate){ ResponseDataHandler.ToJSONresponse(duplicate.toString(),response); }catch (ClassNotFoundException notFound){ ResponseDataHandler.ToJSONresponse(notFound.toString(),response); } catch (SQLException sql){ ResponseDataHandler.ToJSONresponse(sql.toString(),response); }catch (IOException io){ ResponseDataHandler.ToJSONresponse(io.toString(),response); } finally { try { connection.close(); } catch (SQLException sql) { ResponseDataHandler.ToJSONresponse(sql.toString(),response); } } } } 

I can not understand the error that I get .. If there was a connection about the problem with the version of MySQL Server on my PC, then this design should also not work when registering .. If anyone has come across this, I will be glad to help!

  • Maybe you have extra spaces around the same before the question? - Yuriy SPb
  • What? What are you talking about ? - Maks.Burkov
  • nick_name = ? What if all the spaces are removed from here? to become nick_name=? - Yuriy SPb
  • String checkingUser = "SELECT nick_name, email, password FROM users WHERE nick_name =?"; - removed in this way, getting the same error. - Maks.Burkov

1 answer 1

According to en-so, you need to run a query with no arguments.

https://stackoverflow.com/a/24692705/3212712

ResultSet resultSet = preparedStatement.executeQuery ();

  • Really. Why dissect it, if in the end you start up with the same, but in fact a completely different request. - Sergey