gpt4 book ai didi

java - 我必须使用按钮将特定行从 mysql 添加到 JTable

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:15 25 4
gpt4 key购买 nike

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/inventry","root","");
Statement stemt=con.createStatement();
String str = "select * from addproducts";
ResultSet rs = stemt.executeQuery(str);

while(rs.next())
{
model.addRow(new Object[]{rs.getString("pslno"),rs.getString("pid"),rs.getString("pname"),rs.getString("pcategory"),rs.getString("pqty"),rs.getString("ppurcst"),rs.getString("psalprc"),rs.getString("pcmprc"),rs.getString("pdate"),});
}
}catch(Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}

它正在工作并在 JTable 上显示所有数据库表记录,但是,

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/inventry","root","");
Statement stemt=con.createStatement();

String StrQr="";
if (prid.getText().trim().length()>0 ) {
StrQr=StrQr + " and pid = " + prid.getText().trim() + " ";
String str = "select pid, pname,pslno,pcategory,pqty,ppurcst,psalprc,pcmprc from addproducts where 1=1 " + StrQr + " order by pid";
ResultSet rs = stemt.executeQuery(str);
JOptionPane.showMessageDialog(null,"connected");

while(rs.next()) {
model.addRow(new Object[]{rs.getString("pslno"),rs.getString("pid"),rs.getString("pname"),rs.getString("pcategory"),rs.getString("pqty"),rs.getString("ppurcst"),rs.getString("psalprc"),rs.getString("pcmprc"),rs.getString("pdate"),});
}
}
} catch (Exception e) {
System.err.println(e);
//System.exit(1);
}
}

我想在 JTable 上显示特定的 p_id 行,但它不起作用。没有错误发生。

最佳答案

这不是您问题的具体答案,而是一些提示,希望能帮助您自行解决问题。


避免在 EDT 上执行繁重的任务

当心数据库调用是一项耗时的任务,可能会阻塞 Event Dispatch Thread (EDT)导致 GUI 变得无响应。 EDT 是一个单独的特殊线程,Swing 组件的创建和更新在其中进行。为避免阻塞此线程,请考虑使用 SwingWorker在后台线程中执行数据库调用并在 EDT 中更新 Swing 组件。查看更多 Concurrency in Swing跟踪。


DriverManager.getConnection() 不鼓励

根据 the documentations , 使用DiverManager.getConnection()不鼓励,应替换为 DataSource.getConnection()反而。使用MySQL JDBC Connector实现应如下所示:

MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setServerName("localhost");
dataSource.setDatabaseName("inventry");
dataSource.setPortNumber(3306);
dataSource.setUser("root");
dataSource.setPassword(""); // blank password is a terrible idea

Connection connection = dataSource.getConnection();

在本文中查看更多信息:Connecting with DataSource Objects


不要使用Statement,而是使用PreparedStatement

如果您要使用 JDBC,则使用 PreparedStatement :

String pid = prid.getText().trim();
...
String sql = "SELECT * FROM addproducts WHERE pid = ?";
PreparedStatemnt ps = connection.prepareStatement(sql);
ps.getString(1, pid);
ResultSet rs = ps.executeQuery();

注意 1:您确定 pid 是一个 String 是个好主意吗?
注意 2:要根据文本字段值填充表格,请参阅 this related question and answer .


建议:try-with-resources

从 Java 7 开始,有一种更优雅的方式来处理 try-catch block 和可关闭资源:try-with-resources :

try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement("sql")) {

statement.setString(1, "pid");

try (ResultSet resultSet = statement.executeQuery()) {

// process ResultSet here

} catch (SQLException ex) {
// Log the exception here
}

} catch (SQLException ex) {
// Log the exception here
}

关于java - 我必须使用按钮将特定行从 mysql 添加到 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25448611/

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