无法使用Java更新mySQL数据库中的行没有错误消息

时间:2014-03-11 10:46:46

标签: java mysql swing jdbc

我成功地将JDBC驱动程序导入到netbeans并测试了mySQL连接...工作正常但在执行查询后,表在mySQL中没有更新。我正在使用GUI来更新这些值,我完全没有错误,我也在使用异常处理!

//这是我的代码(已更新)

  package DBConn;

  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  import java.sql.Statement;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  //import java.util.*;

  public class FrameDBTest extends javax.swing.JFrame {

public FrameDBTest() {
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jTextField1 = new javax.swing.JTextField();
    jTextField2 = new javax.swing.JTextField();
    jTextField3 = new javax.swing.JTextField();
    jComboBox1 = new javax.swing.JComboBox();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    jTextField4 = new javax.swing.JTextField();
    jLabel6 = new javax.swing.JLabel();
    jButton2 = new javax.swing.JButton();
    jLabel5 = new javax.swing.JLabel();
    jPanel2 = new javax.swing.JPanel();
    jPanel3 = new javax.swing.JPanel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Tapiwa's FlowNation CARS");
    setResizable(false);

    jPanel1.setBackground(new java.awt.Color(153, 153, 255));

    jTextField1.setFont(new java.awt.Font("Century Gothic", 0, 18)); // NOI18N
    jTextField1.setName("JID"); // NOI18N
    jTextField1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jTextField1ActionPerformed(evt);
        }
    });

    jTextField2.setFont(new java.awt.Font("Century Gothic", 0, 18)); // NOI18N
    jTextField2.setName("JNAME"); // NOI18N
    jTextField2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jTextField2ActionPerformed(evt);
        }
    });

    jTextField3.setFont(new java.awt.Font("Century Gothic", 0, 18)); // NOI18N
    jTextField3.setName("JDISPLACE"); // NOI18N
    jTextField3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jTextField3ActionPerformed(evt);
        }
    });

    jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "FL 3", "FL 4", "FL 6", "V6", "V8", "V10", "V12", "V16", "W12", "W16" }));
    jComboBox1.setName("ComboCAR"); // NOI18N
    jComboBox1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jComboBox1ActionPerformed(evt);
        }
    });

    jLabel1.setText("Name");

    jLabel2.setText("ID - INT");

    jLabel3.setText("Displacement - FLOAT");

    jLabel4.setText("Cylinders");

    jButton1.setFont(new java.awt.Font("Century Gothic", 0, 24)); // NOI18N
    jButton1.setForeground(new java.awt.Color(102, 102, 102));
    jButton1.setText("SAVE");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jTextField4.setFont(new java.awt.Font("Century Gothic", 0, 18)); // NOI18N
    jTextField4.setName("JNAME"); // NOI18N
    jTextField4.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jTextField4ActionPerformed(evt);
        }
    });

    jLabel6.setText("Name");

    jButton2.setFont(new java.awt.Font("Century Gothic", 0, 24)); // NOI18N
    jButton2.setForeground(new java.awt.Color(102, 102, 102));
    jButton2.setText("SAVE");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(22, 22, 22)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel1)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3)
                        .addComponent(jLabel4))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jTextField3)
                        .addComponent(jTextField2)
                        .addComponent(jTextField1)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE))))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addComponent(jLabel6)
                    .addGap(18, 18, 18)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(24, 24, 24))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(34, 34, 34))))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(35, 35, 35)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel1)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)))
            .addGap(28, 28, 28)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel2))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel4))
            .addGap(25, 25, 25)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel3))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap())
    );

    jLabel5.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
    jLabel5.setForeground(new java.awt.Color(0, 153, 153));
    jLabel5.setText("FlowNation CARS");

    jPanel2.setBackground(new java.awt.Color(153, 153, 153));

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 947, Short.MAX_VALUE)
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 42, Short.MAX_VALUE)
    );

    jPanel3.setBackground(new java.awt.Color(102, 102, 102));

    javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
    jPanel3.setLayout(jPanel3Layout);
    jPanel3Layout.setHorizontalGroup(
        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );
    jPanel3Layout.setVerticalGroup(
        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 22, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addGroup(layout.createSequentialGroup()
            .addGap(23, 23, 23)
            .addComponent(jLabel5)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jLabel5)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    );

    pack();
}// </editor-fold>                        

private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:
}                                           

@SuppressWarnings("empty-statement")
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    try
    {


        Connection CC = DriverManager.getConnection("jdbc:mysql://localhost/CS","root","carrizo");
        Statement ST = (Statement) CC.createStatement();

        //String newdata;
        ST.execute("INSERT INTO carz VALUES('"+jTextField2.getText()+"',"+jTextField1.getText()+",'"+jComboBox1.getSelectedItem()+"',"+jTextField3.getText()+")");
        //ST.executeUpdate(newdata);
    }

    catch(SQLException e)
    {
        System.out.println(e.getSQLState());
        e.getMessage();
        e.getErrorCode();
        e.printStackTrace();
    }

    finally
    {
        System.out.println("\n\nExecuted...\n"); 
    };

}                                        












 ![mySQL database and table][1]

4 个答案:

答案 0 :(得分:1)

关于您的代码的一些注意事项:

请注意,您使用的是Statement.execute(String sql)方法,主要用于执行数据库查询。要执行INSERT / UPDATE / DELETE语句,建议使用Statement.executeUpdate()方法。

另请注意,您的代码容易受到SQL injection攻击。您应该查看PreparedStatement接口,它是Statement的子接口,可以帮助您避免这种情况。看看Using Prepared Statements踪迹。

例如,您可以按如下方式重构代码:

Integer param1 = Integer.valueOf(jTextField2.getText());
Integer param2 = Integer.valueOf(jTextField1.getText());
Object param3 = jComboBox1.getSelectedItem();
Integer param4 = Integer.valueOf(jTextField3.getText());

String sql = "INSERT INTO carz VALUES (?, ?, ?, ?)";
PreparedStatement statement = CC.prepareStatement(sql);
statement.setInt(1, param1);
statement.setInt(2, param1);    
statement.setObject(3, param3);
statement.setInt(4, param4);

int result = statement.executeUpdate();

你可能也想看看这个优秀的教程:


注意actionPerformed()方法中的代码在Event Dispatch Thread(又称EDT)中执行,这是一个单独的线程,其中发生Swing组件创建/更新和事件处理。耗时的任务(如数据库调用)可能会阻止此线程导致GUI无响应。为避免这种情况,您应该使用SwingWorker类在后台线程中执行繁重的任务,并更新EDT中的Swing组件。


忽略异常的空catch块根本不是一个好习惯。您至少应该打印堆栈跟踪:

catch(SQLException e) {
    e.printStackTrace(); // log the exception is even better 
}

关于此评论:

  

但对于没有单引号的那些文本字段......实际上是   整数

在这种情况下,可能JSpinnerJFormattedTextField是更好的选择。


最后但同样重要的是,请始终关注Java Code Conventions。变量名以小写字母开头,必须有意义。

答案 1 :(得分:0)

看起来你没有处理异常,只是一个空的catch语句。

答案 2 :(得分:0)

INSERT INTO carz VALUES('"+jTextField2.getText()+"',"+jTextField1.getText()+",'"+jComboBox1.getSelectedItem()+"',"+jTextField3.getText()+")");

在上面的插入查询中,您没有用'

括起varchars

将其更改为此

INSERT INTO carz VALUES('"+jTextField2.getText()+"','"+jTextField1.getText()+"','"+jComboBox1.getSelectedItem()+"','"+jTextField3.getText()+"')");

答案 3 :(得分:0)

如果您确定要输入ID和位移的数值,则应该没有使用varchars。但实际上,您应该做的只是在catch语句中添加System.out.println(e.getSQLState())e.getMessage()e.getErrorCode(),然后查看错误是什么。这对解决这个问题大有帮助。

尝试将getConnection部分修改为:

Class.forName("com.mysql.jdbc.Driver"); // Setup the connection with the DB 
connect = DriverManager .getConnection("jdbc:mysql://localhost/feedback?" + "user=sqluser&password=sqluserpw");

正如AJ评论的那样,将e.printStackTrace()放在catch块中以获取有关JDBC过程中发生的最详细信息。