There seems to be a problem with Statement st = conn.createStatement();
Here's my code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model=(DefaultTableModel)p1.getModel();
try{
Class.forName("java.sql.Driver");
Connection conn=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/product","root","");
Statement st = conn.createStatement();
String query="Select*from voltron;";
ResultSet rs=st.executeQuery(query);
while(rs.next()) {
String s1=rs.getString("id");
String s2=rs.getString("product");
String s3=rs.getString("brand");
String s4=rs.getString("price");
String s5=rs.getString("release");
model.addRow(new Object[]{s1,s2,s3,s4,s5});
}
rs.close();;
st.close();
conn.close();
}
catch (Exception e) {
JOptionPane.showMessageDialog(this, "There is an ERROR");
}
}
The problem is that when you write Connection
, the compiler thinks you mean your class, rather than java.sql.Connection
... and your class doesn't have a createStatement
method.
All you've got to do is specify the class name fully:
java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/product","root","");
No need for a cast, and the error you're reporting will now go away, as the compiler will be looking for the method in the right class.
Or you could just rename your class, of course - giving your own classes names of related classes within the core Java framework isn't a great idea.
See more on this question at Stackoverflow