gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:09:05 25 4
gpt4 key购买 nike

我成功地将 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]

最佳答案

有关代码的一些注释:

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

另请注意,您的代码容易受到 SQL injection 的攻击攻击。您应该看看PreparedStatement接口(interface)是 Statement 的子接口(interface)并将帮助您避免这种情况。看看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 block 根本不是一个好的做法。您至少应该打印堆栈跟踪:

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

关于此评论:

but for those textfields without single quotes...they are actually ints

也许JSpinnerJFormattedTextField在这种情况下是更好的选择。

<小时/>

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

关于java - 无法使用 Java 更新 mySQL 数据库中的行,没有错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22323364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com