本文介紹了在嘗試將java NetBeans j表注冊表的數據放入mysql時,我不斷收到錯誤。的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在用Java NetBeans制作注冊表。我使用的是CRUD、jframe和jtable函數。我還需要實現MySQL查詢來將數據保存在MySQL工作臺中。但是每當我運行項目時,都會出現同樣的錯誤,程序提示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)
推薦答案
尤其是準備好的語句不應聲明為字段。
使用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
中的Typo?
這篇關于在嘗試將java NetBeans j表注冊表的數據放入mysql時,我不斷收到錯誤。的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,