gpt4 book ai didi

java - executeUpdate() 抛出 MySQLSyntaxErrorException

转载 作者:行者123 更新时间:2023-11-30 23:56:43 24 4
gpt4 key购买 nike

我的目标是向我创建的现有表中添加一个新行。但是,我收到以下错误。

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 (?, ?, ?, ?)

带有反引号的列名。

关于java - executeUpdate() 抛出 MySQLSyntaxErrorException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29679324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com