private void rtrBtnActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model = (DefaultTableModel) depTbl.getModel();
try{
Class.forName("java.sql.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test1","admin","root");
Statement stmt = con.createStatement();
String query = "SELECT * FROM dept;";
ResultSet rs = stmt.executeQuery(query);
while(rs.next()){
String dno = rs.getString("deptno");
String dName = rs.getString("dname");
String lc = rs.getString("loc");
model.addRow(new Object[] {dno,dName,lc});
}
rs.close();
stmt.close();
con.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Error In Connectivity");
}
}
我想我JForm连接到mysql数据库,但无法连接到数据库,持续执行捕捉语句"连接错误",请帮我该如何解决这个问题 ..............................................................................
更改以下行
Class.forName("java.sql.Driver");
Class.forName("com.mysql.jdbc.Driver");
这行代码是不正确的- class . forname ("java.sql.Driver");
java.sql.Driver是一个接口。您需要为您的数据库提供正确的jdbc驱动程序类。