当向 Access 数据库中添加数据时,显示以下错误:

huangapple go评论116阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年5月31日 05:45:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/62109049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定