英文:
When add data into Access Database show the error below
问题
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: 类 net.ucanaccess.jdbc.UcanaccessPreparedStatement 无法强制转换为类 com.mysql.jdbc.PreparedStatement(net.ucanaccess.jdbc.UcanaccessPreparedStatement 和 com.mysql.jdbc.PreparedStatement 在加载程序的未命名模块中)
at hamody.NewJFrame.jButton1ActionPerformed(NewJFrame.java:115)
at hamody.NewJFrame$1.actionPerformed(NewJFrame.java:66)
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String 用户名 ;
用户名 = jTextField1.getText();
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
String dbURL = "jdbc:ucanaccess://E:/Database410.mdb" ;
connection = DriverManager.getConnection(dbURL);
// 步骤 2.B:创建 JDBC 语句
statement = connection.createStatement();
pst = (PreparedStatement) connection.prepareStatement("insert into user ( username ,password ) values( ?,? )") ;
pst.setString(1,用户名);
int b=Integer.parseInt(jTextField2.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(this,"已添加");
} catch (ClassNotFoundException ex){
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this,ex);
} catch (SQLException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this,ex);
}
}
英文:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class net.ucanaccess.jdbc.UcanaccessPreparedStatement cannot be cast to class com.mysql.jdbc.PreparedStatement (net.ucanaccess.jdbc.UcanaccessPreparedStatement and com.mysql.jdbc.PreparedStatement are in unnamed module of loader 'app')
at hamody.NewJFrame.jButton1ActionPerformed(NewJFrame.java:115)
at hamody.NewJFrame$1.actionPerformed(NewJFrame.java:66)
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String usernames ;
usernames = jTextField1.getText();
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
String dbURL = "jdbc:ucanaccess://E:/Database410.mdb" ;
connection = DriverManager.getConnection(dbURL);
// Step 2.B: Creating JDBC Statement
statement = connection.createStatement();
pst = (PreparedStatement) connection.prepareStatement("insert into user ( username ,password ) values( ?,? )") ;
pst.setString(1,usernames);
int b=Integer.parseInt(jTextField2.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(this,"تم الاضافة");
} catch (ClassNotFoundException ex){
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this,ex);
} catch (SQLException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this,ex);
}
}
答案1
得分: 1
这一行似乎有问题:
pst = (PreparedStatement) connection.prepareStatement("insert into user (username, password) values(?, ?)");
为什么要将它强制转换为 (PreparedStatement)
?connection.prepareStetement
应该已经返回了一个 PreparedStatement
,不需要强制转换。请检查你的导入语句。我猜你可能会在那里找到一行类似于 import com.mysql.jdbc.PreparedStatement
或者 import com.mysql.jdbc.*
的代码。删除它,并且也删除这个不必要的强制转换。
英文:
This line seems to be an offender:
pst = (PreparedStatement) connection.prepareStatement("insert into user ( username ,password ) values( ?,? )") ;
Why are you casting this to (PreparedStatement)
? connection.prepareStetement
should give you a PreparedStatement
already, without need of casting. Look into your imports. My guess is you will find a line there looking like import com.mysql.jdbc.PreparedStatement
or import com.mysql.jdbc.*
. Delete it, and also delete the unnecessary cast.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论