在尝试将java NetBeans j表注册表的数据放入mysql时,我不断收到错误。

I keep getting errors when trying to put the data of my java netbeans j table registration form into MySQL(在尝试将java NetBeans j表注册表的数据放入mysql时,我不断收到错误。)

本文介绍了在尝试将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时,我不断收到错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在尝试将java NetBeans j表注册表的数据放入mysql时,我不断收到错误。

基础教程推荐