【问题标题】:When add data into Access Database show the error below将数据添加到 Access 数据库时显示以下错误
【发布时间】:2020-06-02 15:57:16
【问题描述】:

线程“AWT-EventQueue-0”java.lang.ClassCastException 中的异常:net.ucanaccess.jdbc.UcanaccessPreparedStatement 类无法转换为 com.mysql.jdbc.PreparedStatement 类(net.ucanaccess.jdbc.UcanaccessPreparedStatement 和 com. mysql.jdbc.PreparedStatement 位于加载程序“app”的未命名模块中) 在 hamody.NewJFrame.jButton1ActionPerformed(NewJFrame.java:115) 在 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);
    }

} 

【问题讨论】:

    标签: java ms-access


    【解决方案1】:

    这行好像犯法了:

    pst =  (PreparedStatement) connection.prepareStatement("insert into user ( username ,password ) values( ?,? )") ;
    

    你为什么要把这个投射到(PreparedStatement)connection.prepareStetement 应该已经给你一个PreparedStatement,而不需要强制转换。看看你的进口。我猜你会在那里找到一条看起来像import com.mysql.jdbc.PreparedStatementimport com.mysql.jdbc.* 的行。删除它,同时删除不必要的演员表。

    【讨论】:

    • 我已经进行了您指示的更改,现在出现此错误...线程“AWT-EventQueue-0”中的异常 java.lang.NullPointerException
    • 这有点好,但我仍然不知道您发布的代码中的哪一行是抛出异常(您可以在堆栈跟踪中找到行号)
    • 在 hamody.NewJFrame.jButton1ActionPerformed(NewJFrame.java:122) .... 第 122 行:- pst.setString(1,num);
    • 很难说,因为您的帖子中不包含该代码。最好准备一个minimal, reproducible example
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    相关资源
    最近更新 更多