0

When I am trying retrieve values into a JTable it says "cast connectdb to connection", on the line con=Connect.ConnectDB(). But I have declared my Connect class without any error, and are able to insert values from another form successfully. This is my code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { try{ con= Connect.ConnectDB(); String sql="select * from pharmacy"; pst = con.prepareStatement(sql); pst.execute(); jTable1.setModel(DbUtils.resultSetToTableModel(rs)); JOptionPane.showMessageDialog(this,"Succesfully stored","User",JOptionPane.INFORMATION_MESSAGE); } catch(SQLException ex){ JOptionPane.showMessageDialog(this,ex); } } 

This is my Connect class:

public class Connect { Connection con=null; public static Connection ConnectDB(){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hms_db1","root","root"); return con; }catch(ClassNotFoundException | SQLException e){ JOptionPane.showMessageDialog(null, e); return null; } } } 
8
  • 1
    show Connect class Commented Jan 6, 2015 at 6:17
  • (Connection)Connect.ConnectDB(); is working? Commented Jan 6, 2015 at 6:18
  • No,its's getting the error "null pointer exception" Commented Jan 6, 2015 at 6:21
  • replace Connection con=null; by Connection con; Commented Jan 6, 2015 at 6:24
  • does db exist ??what is the line error occurred ?did you add mysql libry ? Commented Jan 6, 2015 at 6:25

2 Answers 2

2
jTable1.setModel(DbUtils.resultSetToTableModel(rs)); 

This is the line you're getting NullPointerException from.. because rs is null

change this line

pst.execute(); 

to

rs = pst.executeQuery(); 

You haven't assigned the result from the executeQuery() to the resultSet reference!! and since you're passing a null reference thats why you're getting a NPE. Hope this answers your question.

Sign up to request clarification or add additional context in comments.

Comments

0

try pst.executeQuery() in place of pst.execute()

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { try { con = Connect.ConnectDB(); String sql = "select * from pharmacy"; pst = con.prepareStatement(sql); pst.executeQuery(); jTable1.setModel(DbUtils.resultSetToTableModel(rs)); JOptionPane.showMessageDialog(this, " Succesfully stored" , "User", JOptionPane.INFORMATION_MESSAGE); } catch (SQLException ex) { JOptionPane.showMessageDialog(this, ex); } } 

7 Comments

jTable1.setModel(DbUtils.resultSetToTableModel(rs)); in this line where "rs" coming from?
ResultSet rs=null; under the class
Have you try to store the result into "rs" like: ResultSet rs= pst.executeQuery(); ?
yes sir i have done,and now i can see the jtable values from the form which i inserted values,but i want to see the jtable and it's values in another form in the same package, and getting casting error in the line con="Connect.ConnectDB()";.
i have casted that line like " con= (Connection) Connect.ConnectDB();" and worked fine.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.