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行,但它无效。没有发生错误。
答案 0 :(得分:3)
这不是您问题的具体答案,但有些提示可以帮助您自己解决问题。
当心数据库调用是一项耗时的任务,可能会阻止Event Dispatch Thread (EDT)导致GUI无响应。 EDT是一个单独的特殊线程,可以在其中创建和更新Swing组件。为避免阻塞此线程,请考虑使用SwingWorker在后台线程中执行数据库调用并更新EDT中的Swing组件。在Concurrency in Swing trail中查看更多内容。
DriverManager.getConnection()
根据the documentations,不建议使用DiverManager.getConnection(),而应由DataSource.getConnection()代替。MySQL JDBC Connector。使用Connecting with DataSource Objects实现应该如下所示:
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
,而是使用PreparedStatement
如果您打算使用JDBC,请使用this related question and answer:
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
块和可关闭资源的方式更为优雅:{{3}}:
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
}