I have to add particular row from mysql to a JTable using a button

久未见 提交于 2019-12-25 05:53:54

问题


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());
  }
}

It is working and display all database table records on JTable but,

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);
  }
}   

I want to display particular p_id row on JTable but it is not working. No errors occurred.


回答1:


This is not a concrete answer to your problem but some hints that hopefully will help you to solve it by yourself.


Avoid performing heavy tasks on the EDT

Beware database calls are time consuming tasks and may block the Event Dispatch Thread (EDT) causing the GUI become unresponsive. The EDT is a single and special thread where Swing components creation and update take place. To avoid block this thread consider use a SwingWorker to perform database calls in a background thread and update Swing components in the EDT. See more in Concurrency in Swing trail.


DriverManager.getConnection() is discouraged

According to the documentations, the use of DiverManager.getConnection() is discouraged and should be replaced by DataSource.getConnection() instead. Using MySQL JDBC Connector the implementation should look like this:

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();

See more in this article: Connecting with DataSource Objects


Don't use Statement but PreparedStatement instead

If you are going to use JDBC then use 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();

Note 1: Are you sure is a good idea pid be a String?
Note 2: To populate your table based on text fields values see this related question and answer.


Suggestion: try-with-resources

Since Java 7 there's a more elegant way to deal with try-catch blocks and closeable resources: 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
}


来源:https://stackoverflow.com/questions/25448611/i-have-to-add-particular-row-from-mysql-to-a-jtable-using-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!