I just want to make a simple insert into a table in my sql data base. Nonetheless I get this error eventhough I tried it in several ways. I got a table named "values" in my database "test" with a simple column for id "idValues" and a column for int values called "value". I just want to insert a simple record with 1 for ID and 1 for value (1/1). Is there maybe some problem with certain versions?
package jdbcdemo;
import java.sql.*;
public class Driver {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?autoReconnect=true&useSSL=false", "root", "rmi1337");
Statement statement = myConn.createStatement();
String sql = "INSERT INTO values " + "(idValues, value)" + "VALUES (1, 1)";
statement.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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 'values(idValues, value)VALUES (1, 1)' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486)
at com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1552)
at com.mysql.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2607)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1480)
at jdbcdemo.Driver.main(Driver.java:22)
valuesas a tablename isn't a good choicevaluesis a keyword in SQL. If you're going to call your table "values" (and I suggest you don't), you would have to escape it; e.g.`values`sqlvariable. It be cleaner looking at least if you removed the concatenation and made it one big string.