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;
}
}
}
Connectclass(Connection)Connect.ConnectDB();is working?Connection con=null;byConnection con;