0
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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 3
    Did you SQL work? Are any records returned in the ResultSet? Did you add a System.out.println(..) statement to your code to check if your code is being executed. – camickr Aug 22 '14 at 14:14
  • 1
    Check the value returned by prid.getText() and verify there is any record available for that pid value in database. Another point, "pdate" is missing in your query string and 1=1 where clause is not required at all. – Loganathan Mohanraj Aug 22 '14 at 14:36

1 Answers1

3

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
}
Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • 1
    +1 but Connection/PreparedStatement/ResultSet should be close() – mKorbel Aug 22 '14 at 19:05
  • Thanks @mKorbel Yes they should be closed, but that is what `try-with-resources` does. Given Connection/PreparedStatement/ResultSet interfaces extend from [AutoCloseable](http://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html) introduced in Java 7, there's no need to close this resources manually, they'll be auto-closed when try block finishes either normally or because of an exception. – dic19 Aug 22 '14 at 23:23