The following is my code for my StudentDetails Gui, however when I try to connect to my database I cannot connect. How can I correct this error and properly display my table of what is popualted in database on my gui?
public class StudentDetailsGui extends javax.swing.JFrame {
/**
* Creates new form
* StudentDetailsGui
*/
public StudentDetailsGui() {
initComponents();
//DBConnection.getConnection();
//Student_Load();
Connect();
}
Connection con;
PreparedStatement pst;
ResultSet rs;
DefaultTableModel d;
public void Connect(){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentmanage","root","");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,"Cannot connect!");
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(null,"Still Cannot connect!");
}
}
public void Student_Load(){
int c;
try {
pst = con.prepareStatement("select * from student");
rs = pst.executeQuery();
ResultSetMetaData rsd = rs.getMetaData();
c = rsd.getColumnCount();
d = (DefaultTableModel)jTableData.getModel();
d.setRowCount(0);
while (rs.next()) {
for(int i=1; i<=c; i++){
rs.getString("StudentID");
rs.getString("name");
rs.getString("surname");
rs.getString("email_address");
rs.getString("course");
}
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,"Cannot display");
}
}