我的目标是向我创建的现有表添加一个新行.但是,我收到以下错误.
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误; 检查与MySQL服务器版本对应的手册,以便在sun.reflect的第1行使用"姓名,姓氏,电话号码"附近的VALUES(2056732,'Hello','World',3). sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)中的NativeConstructorAccessorImpl.newInstance0(Native Method)位于sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45),位于java.lang.reflect.Constructor.newInstance(构造函数. java:408)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)在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)的com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)com.mysql.jdbc.MysqlIO.sendCommand( MysqlIO.java:2468)com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2719)at com.my sql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)位于com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2450)的com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2371)at com AddingNewRow.main中的.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2355)(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");
}
Run Code Online (Sandbox Code Playgroud)
这是我运行程序时得到的输出
输入学号:1234567
输入名字:Hello
输入姓氏:世界
输入电话号码:123321
连接的
找到表
增值
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException ...
我知道这可能只是一个非常愚蠢的问题,但我刚刚开始使用数据库,即使经过这里发布的大多数其他类似问题,也无法弄清楚代码是否有问题.
如果有人能告诉我什么是错的,我将非常感激.提前致谢.
试试这个
INSERT INTO registration (`ID`, `First Name`, `Last Name`, `Tel No.`) VALUES (?, ?, ?, ?)
Run Code Online (Sandbox Code Playgroud)
列名称backticks
.
归档时间: |
|
查看次数: |
1303 次 |
最近记录: |