Reputation: 147
I have this query and I don't understand why I get an error.
Error: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'from, to, subject) VALUES ('me','you','Missed you!')' at line 1
String from = "me";
String to = "you";
String subject = "Missed you!";
String query = "INSERT INTO emailApp (from, to, subject) VALUES ('"+from+"','"+to+"','"+subject+"')";
st.executeUpdate(query);
Upvotes: 0
Views: 45
Reputation: 159864
FROM
and TO
are SQL reserved keywords. ALTER both column names in the database and adjust your query to match the new name.
PreparedStatement prepareStatement =
connection.prepareStatement("INSERT INTO emailApp (sender, recipient, subject) VALUES (?, ?, ?)");
prepareStatement.setInt(1, sender);
prepareStatement.setInt(2, recipient);
prepareStatement.setInt(3, subject);
prepareStatement.executeUpdate();
Upvotes: 1
Reputation: 1
String from = "me";
String to = "you";
String subject = "Missed you!";
String query = "INSERT INTO emailApp ("columnName1", "columnName2", "columnName3") VALUES ('"+from+"','"+to+"','"+subject+"')";
st.executeUpdate(query);
Upvotes: 0