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上执行繁重的任务
注意数据库调用是耗时的任务,可能会阻塞事件调度线程(EDT),导致GUI变得无响应。EDT是一个单独的、特殊的线程,Swing组件的创建和更新发生在这里。为了避免阻塞这个线程,可以考虑使用SwingWorker在后台线程中执行数据库调用,并在EDT中更新Swing组件。在Swing trail中查看更多关于并发的信息。
不鼓励DriverManager.getConnection()
根据文档,不鼓励使用DiverManager.getConnection(),应该用DataSource.getConnection()代替。使用MySQL JDBC连接器,实现应该如下所示:
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();
在本文中查看更多信息:连接数据源对象
用PreparedStatement
代替Statement
如果你打算使用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:要根据文本字段值填充表,请参阅相关的问题和答案。
建议:try-with-resources
自Java 7以来,有一种更优雅的方式来处理try-catch
块和可关闭的资源: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
}