I was using following code
String sql = "select * from user where username=? and password=?";
PreparedStatement ps =connection.prepareStatement(sql);
ps.setString(1, username);ps.setString(2, password);
ResultSet rs=ps.executeQuery(sql);
while (rs.next()) {
user= new User();
user.setId(rs.getInt(1));
user.setName(rs.getString(2));
user.setEmail(rs.getString(3));
user.setQualification(rs.getString(5));
user.setRole(rs.getString(6));
}
I got following eror:
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 '? and password=?' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1200)
Solution and what is wrong?
for this line ResultSet rs=ps.executeQuery(sql);
I do not need to add SQL query there.
I removed SQL keyword it code started working fine…
String sql = "select * from user where username=? and password=?";
PreparedStatement ps =connection.prepareStatement(sql);ps.setString(1, username);
ps.setString(2, password);
ResultSet rs=ps.executeQuery();