I was trying to use java to insert some data into mysql database, but it kept giving syntax error:
Exception in thread "main" 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 ' postcode ='n'' at line 1
here is my code:
`
public static void saveAddress(Address address) throws SQLException{
SQLConnection sql = new
SQLConnection(SQLConnection.getConnection());
if(SQLAddress.getAddressID(address) == -1){
sql.update(String.format("INSERT INTO address(houseNumber,
street, district, city, postcode) VALUES (%s ,%s ,%s ,%s
,%s)",address.getHouseNumber(),address.getStreet(),
address.getDistrict(),address.getCity(),
address.getPostCode()));
}
sql.close();
}
`
I really don't know what to do, i used the same syntax in mysql shell, it worked perfect there.
Edit
public static void saveAddress(Address address) throws SQLException{
Connection sql = SQLConnection.getConnection();
if(SQLAddress.getAddressID(address) == -1){
PreparedStatement ps = sql.prepareStatement("INSERT INTO address (houseNumber, street, district, city, postcode) VALUES (?, ?, ?, ?, ?);");
ps.setString(1, address.getHouseNumber());
ps.setString(2, address.getStreet());
ps.setString(3, address.getDistrict());
ps.setString(4, address.getCity());
ps.setString(5, address.getPostCode());
ps.executeUpdate();
sql.commit();
}
sql.close();
}