executeUpdate() 抛出 MySQLSyntaxErrorException
executeUpdate() throwing a MySQLSyntaxErrorException
我的 objective 是向我创建的现有 table 添加一个新行。但是,我收到以下错误。
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 'Name, Last Name, Tel No.)VALUES (2056732, 'Hello', 'World', 3)' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2719)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2450)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2371)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2355)
at AddingNewRow.main(AddingNewRow.java:50)
这是代码的一部分。如果您需要完整的代码来找出问题所在,请告诉我。
Connection conn = null;
PreparedStatement ps = null;
Scanner in = new Scanner(System.in);
System.out.print("Enter the student number: ");
int studentNO = in.nextInt();
System.out.print("Enter the first name: ");
String fName = in.next();
System.out.print("Enter the last name: ");
String lName = in.next();
System.out.print("Enter the telephone number: ");
int telNO = in.nextInt();
try {
conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
System.out.println("Connected");
//Executing a query
String sql_query = "INSERT INTO registration (ID, First Name, Last Name, Tel No.) VALUES (?, ?, ?, ?)";
System.out.println("Table found");
ps = conn.prepareStatement(sql_query);
ps.setInt(1, studentNO);
ps.setString(2, fName);
ps.setString(3, lName);
ps.setInt(4, telNO);
System.out.println("Values added");
ps.executeUpdate();
System.out.println("Values updated");
}
This is the output I get when I run the program
Enter the student number: 1234567
Enter the first name: Hello
Enter the last name: World
Enter the telephone number: 123321
Connected
Table found
Values added
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException...
我知道这可能只是一个非常愚蠢的问题,但我刚刚开始使用数据库,即使在解决了此处发布的大多数其他类似问题之后也无法弄清楚代码有什么问题。
如果有人能告诉我哪里出了问题,我将不胜感激。提前致谢。
试试这个
INSERT INTO registration (`ID`, `First Name`, `Last Name`, `Tel No.`) VALUES (?, ?, ?, ?)
列名 backticks
。
可能是您使用setString
设置学号造成的。这将告诉准备好的语句它应该是一个字符串,而不是一个整数。此外,您可能需要在 Tel No.
字段周围放回勾号。
尝试替换行
String sql_query = "INSERT INTO registration (ID, First Name, Last Name, Tel No.) VALUES (?, ?, ?, ?)";
和
String sql_query = "INSERT INTO registration (`ID`, `First Name`, `Last Name`, `Tel No.`) VALUES (?, ?, ?, ?)";
此外,如果我是您,我会尽量避免在 table 和字段名称中使用特殊字符和空格,因为这些是导致您出现问题的原因。
我的 objective 是向我创建的现有 table 添加一个新行。但是,我收到以下错误。
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 'Name, Last Name, Tel No.)VALUES (2056732, 'Hello', 'World', 3)' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:408) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2719) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2450) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2371) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2355) at AddingNewRow.main(AddingNewRow.java:50)
这是代码的一部分。如果您需要完整的代码来找出问题所在,请告诉我。
Connection conn = null;
PreparedStatement ps = null;
Scanner in = new Scanner(System.in);
System.out.print("Enter the student number: ");
int studentNO = in.nextInt();
System.out.print("Enter the first name: ");
String fName = in.next();
System.out.print("Enter the last name: ");
String lName = in.next();
System.out.print("Enter the telephone number: ");
int telNO = in.nextInt();
try {
conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
System.out.println("Connected");
//Executing a query
String sql_query = "INSERT INTO registration (ID, First Name, Last Name, Tel No.) VALUES (?, ?, ?, ?)";
System.out.println("Table found");
ps = conn.prepareStatement(sql_query);
ps.setInt(1, studentNO);
ps.setString(2, fName);
ps.setString(3, lName);
ps.setInt(4, telNO);
System.out.println("Values added");
ps.executeUpdate();
System.out.println("Values updated");
}
This is the output I get when I run the program
Enter the student number: 1234567
Enter the first name: Hello
Enter the last name: World
Enter the telephone number: 123321
Connected
Table found
Values added
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException...
我知道这可能只是一个非常愚蠢的问题,但我刚刚开始使用数据库,即使在解决了此处发布的大多数其他类似问题之后也无法弄清楚代码有什么问题。
如果有人能告诉我哪里出了问题,我将不胜感激。提前致谢。
试试这个
INSERT INTO registration (`ID`, `First Name`, `Last Name`, `Tel No.`) VALUES (?, ?, ?, ?)
列名 backticks
。
可能是您使用setString
设置学号造成的。这将告诉准备好的语句它应该是一个字符串,而不是一个整数。此外,您可能需要在 Tel No.
字段周围放回勾号。
尝试替换行
String sql_query = "INSERT INTO registration (ID, First Name, Last Name, Tel No.) VALUES (?, ?, ?, ?)";
和
String sql_query = "INSERT INTO registration (`ID`, `First Name`, `Last Name`, `Tel No.`) VALUES (?, ?, ?, ?)";
此外,如果我是您,我会尽量避免在 table 和字段名称中使用特殊字符和空格,因为这些是导致您出现问题的原因。