尝试将我的 java netbeans j 表注册表单的数据放入 MySQL 时,我不断收到错误
Posted
技术标签:
【中文标题】尝试将我的 java netbeans j 表注册表单的数据放入 MySQL 时,我不断收到错误【英文标题】:I keep getting errors when trying to put the data of my java netbeans j table registration form into MySQL 【发布时间】:2022-01-06 00:15:18 【问题描述】:我正在 Java Netbeans 中制作注册表单。我正在使用 CRUD jframe 和 jtable 函数。我还需要实现 mysql 查询以将数据保存在 MySQL Workbench 中。但是每当我运行项目时,都会出现同样的错误,程序说 MySQL 语法错误。
private void JB2ActionPerformed(java.awt.event.ActionEvent evt)
// TODO add code here:
try
Class.forName("com.mysql.cj.jdbc.Driver");
sqlConn = DriverManager.getConnection(data,user,password);
pst = sqlConn.prepareStatement("insert * into tracking_register_db(Name,Surname,Phone,Age,Adress,Email,"
+ "Temp,Symptoms,Exsposed) value(?,?,?,?,?,?,?,?,?,)");
pst.setString(1, JT1.getText());
pst.setString(2, JT2.getText());
pst.setString(3, JT3.getText());
pst.setString(4, JT4.getText());
pst.setString(5, JT5.getText());
pst.setString(6, JT6.getText());
pst.setString(7, JT7.getText());
pst.setString(8, JT8.getText());
pst.setString(9, JT9.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(this,"Record added");
upDateDB();
catch (ClassNotFoundException ex)
java.util.logging.Logger.getLogger(Registration.class.getName()).log(Level.SEVERE, null, ex);
catch (SQLException ex)
java.util.logging.Logger.getLogger(Registration.class.getName()).log(Level.SEVERE, null, ex);
这是我得到的错误:
java.sql.SQLSyntaxErrorException: 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 '* into tracking_register_db(Name,Surname,Phone,Age,Adress,Email,Temp,Symptoms,Ex' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1098)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1046)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1371)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1031)
at Registration.JB2ActionPerformed(Registration.java:462)
at Registration.access$1000(Registration.java:28)
at Registration$11.actionPerformed(Registration.java:302)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6539)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6304)
at java.awt.Container.processEvent(Container.java:2239)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.awt.Container.dispatchEventImpl(Container.java:2283)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
【问题讨论】:
提示:不要标记 IDE。 做标记正在使用的GUI工具包。 【参考方案1】:请通过删除插入后的 * 和末尾的逗号来更新 MySQL 插入查询。正确的部分将是-
"insert into tracking_register_db(Name,Surname,Phone,Age,Adress,Email,"
+ "Temp,Symptoms,Exsposed) value(?,?,?,?,?,?,?,?,?)"
【讨论】:
【参考方案2】:尤其是准备好的语句不应该被声明为字段。
使用 try-with-resources 自动关闭连接、语句和结果集(如果有的话)。即使在异常或返回的情况下。
String sql = "insert into tracking_register_db(Name,Surname,Phone,Age,Adress,Email,"
+ "Temp,Symptoms,Exsposed) values(?,?,?,?,?,?,?,?,?)"
try
Class.forName("com.mysql.cj.jdbc.Driver");
try (Connection sqlConn = DriverManager.getConnection(data, user, password);
PreparedStatement pst = sqlConn.prepareStatement(sql))
pst.setString(1, JT1.getText());
pst.setString(2, JT2.getText());
pst.setString(3, JT3.getText());
pst.setString(4, JT4.getText());
pst.setString(5, JT5.getText());
pst.setString(6, JT6.getText());
pst.setString(7, JT7.getText());
pst.setString(8, JT8.getText());
pst.setString(9, JT9.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(this, "Record added",
JOptionPane.INFORMATION_MESSAGE);
upDateDB();
// Closes sqlConn and pst
catch (ClassNotFoundException | SQLException ex)
Logger.getLogger(Registration.class.getName()).log(Level.SEVERE, "No record added", ex);
JOptionPane.showMessageDialog(this, "No record added: " + ex.getMessage(),
JOptionPane.ERROR_MESSAGE);
Exsposed
中的错字?
【讨论】:
【参考方案3】:欢迎
首先,我不建议您将 SQL 拆分为多个字符串,因为这会使它们难以阅读。
关于您的问题,我看到至少三个错误。
INSERT INTO
语句不使用 *
MySQL 中的INSERT INTO
使用单词VALUES
,而不是value
结尾有一个逗号","
我建议您首先在可以执行它的地方(例如 workbench 或 phpMyAdmin)编写您的 SQL,然后在您确认它可以工作后在您的代码中使用它,因为这些工具可以让您更好地了解错误.
有关 INSERT 语句的更多信息,请参阅https://www.w3schools.com/sql/sql_insert.asp
固定的 SQL 看起来像这样:
pst = sqlConn.prepareStatement("INSERT INTO tracking_register_db(Name,Surname,Phone,Age,Adress,Email,"
+ "Temp,Symptoms,Exsposed) VALUES (?,?,?,?,?,?,?,?,?)");
【讨论】:
【参考方案4】:Sql insert satement doesn't contain a star:
pst = sqlConn.prepareStatement("insert into tracking_register_db(Name,Surname,Phone,Age,Adress,Email,"
+ "Temp,Symptoms,Exsposed) values (?,?,?,?,?,?,?,?,?)");
【讨论】:
你说的是正确的,但现在发生了:java.sql.SQLSyntaxErrorException: 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 ')' 附近使用正确的语法 @EmperorDxD 去掉最后一个问号后面的逗号。我已经编辑了我的答案。 和value
应该是values
。以上是关于尝试将我的 java netbeans j 表注册表单的数据放入 MySQL 时,我不断收到错误的主要内容,如果未能解决你的问题,请参考以下文章