我成功地将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条答案
按热度按时间exdqitrt1#
如果您确定要为id和位移输入数字值,那么不必使用varchars括起来就可以了。但实际上,你应该做的就是
System.out.println(e.getSQLState())
或者e.getMessage()
或者e.getErrorCode()
在catch语句中,只需查看错误是什么。这将大大有助于解决这个问题。尝试将getconnection部分修改为:
正如aj所说
e.printStackTrace()
也可以在catch块中获取关于jdbc进程中发生的事情的最详细信息。ddrv8njm2#
关于代码的一些注解:
注意您使用的是statement.execute(stringsql)方法,它主要用于执行数据库查询。要执行insert/update/delete语句,建议改用statement.executeupdate()方法。
还要注意,您的代码容易受到sql注入攻击。您应该看看preparedstatement接口,它是语句的子接口,可以帮助您避免这种情况。看看如何使用准备好的语句。
例如,您可以按如下方式重构代码:
您可能还想看看这个优秀的教程:
mysql和javajdbc教程
注解代码在
actionPerformed()
方法是在事件调度线程(aka edt)中执行的,事件调度线程是一个单独的线程,swing组件的创建/更新和事件处理在该线程中进行。耗时的任务(如数据库调用)可能会阻塞此线程,从而导致gui无响应。为了避免这种情况,您应该使用swingworker类在后台线程中执行繁重的任务,并在edt中更新swing组件。空的
catch
忽略异常的块根本不是一个好的实践。您至少应该打印堆栈跟踪:关于此评论:
但是对于那些没有单引号的文本字段…它们实际上是int
在这种情况下,也许jspinner或jformattedtextfield是更好的选择。
最后但同样重要的是,始终遵循java代码约定。变量名以小写开头,必须有意义。
shyt4zoc3#
在上面的insert查询中,您没有用
'
把它改成这样umuewwlo4#
看起来您没有处理异常,只是一个空的catch语句。