M FA
M FA

Reputation: 15

When add data into Access Database show the error below

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);
    }

} 

Upvotes: 1

Views: 188

Answers (1)

pafau k.
pafau k.

Reputation: 1687

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.

Upvotes: 1

Related Questions