-1

I was tryng to learn how to connect to mysql from an desktop java application, but there is something wrong with the connection method Class.forName("com.mysql.jdbc.Driver");

When I call it returns this expection :

java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long

I reinstalled the J/connection (I couldn't download it alone, it comes with the other drivers in the installer, I added the driver as a library and i Tried using the com.mysql.jdbc.Connection, and the java.sql.Connection class. The two of them throw the same error. I looked for other similar questions and none of those solved the problem, what else can I do? where can I download an old J/Connection driver ? should I reinstall the whole netbeans? I wonder... these are my methods with the unnecessary code commented:

public static final String URL = "jdbc:mysql://localhost:3306/escuela";
public static final String USERNAME = "root";
public static final String PASSWORD = "123";

public static Connection getConnection(){
   Connection con = null;
    try{
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(URL, USERNAME, PASSWORD); //here 
     fails.
        JOptionPane.showMessageDialog(null, "Conexion exitosa.");
    }
    catch(Exception e){
     e.printStackTrace();
    }
    return con;
}

private void btnConectarActionPerformed(java.awt.event.ActionEvent evt) {                                            
     Connection con = getConnection(); //I call the connection here
//        Statement st;
//        ResultSet rs;
//        
//         try{
//         st = con.createStatement();
//         rs = st.executeQuery("SELECT * FROM personas");
//        
//         while(rs.next()){
//             JOptionPane.showMessageDialog(null, rs.getString("nombre") 
//                + " " + rs.getString("telefono"));
//         } 
//       con.close();
//        }
//        catch(Exception e){
//            System.out.println(e);
//        }

}     

The whole e.printStackTrace:

java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
    at com.mysql.jdbc.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:1062)
    at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3556)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2513)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at App.conexion.getConnection(conexion.java:93)
    at App.conexion.btnConectarActionPerformed(conexion.java:70)
    at App.conexion.access$000(conexion.java:19)
    at App.conexion$1.actionPerformed(conexion.java:45)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6535)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6300)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
  Caused by: java.lang.ClassCastException: java.math.BigInteger cannot be cast 
  to java.lang.Long
    at 
  com.mysql.jdbc.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:1007)
    ... 54 more
user207421
  • 305,947
  • 44
  • 307
  • 483
XaelGa
  • 140
  • 2
  • 10
  • 1
    What mysql connector did you download, what version of mysql are you running? You haven't needed `Class.forName(String)` to load the driver since JDBC 4 (Java 6). – Elliott Frisch Sep 19 '19 at 00:22
  • Looks like an error in the JDBC driver. You should report it to them. – Andreas Sep 19 '19 at 00:23
  • 1
    Try to download the latest connector here https://dev.mysql.com/downloads/connector/j/ select platform independent, and dwonload zip file – Cyrille Con Morales Sep 19 '19 at 00:30
  • Read the stack trace. This exception was not thrown by the `Class.forName()` line, which BTW you can remove, as it hasn't been necessary since 2007. It was thrown by `DriverManager.getConnection()`. – user207421 Sep 19 '19 at 01:40
  • And if you check the documentation `com.mysql.jdbc.Driver` has been replace by `com.mysql.cj.jdbc.Driver` in the version 5.1+ – Cyrille Con Morales Sep 19 '19 at 01:54
  • @CyrilleConMorales That's not correct. `com.mysql.jdbc.Driver` appears in all JARs I have from 5.1.34 to 8.0.11, while `com.mysql.cj.jdbc.Driver` only appears from version 6+. I don't see any reason to ever mention the `cj` one in code. – user207421 Sep 19 '19 at 02:17

1 Answers1

0

I finally made it thanks to Cirille Con Morales,thanks a lot for the link and the download instruction. My Netbeans has an old installation of MySql connector driver (5.xxx) I downloaded the (8.0.17) version, added to the global libraries and I hed other issues but digging in google I resolve them, this pats must be changed: I used the driver has an string url :

public static final String DRIVER = "com.mysql.cj.jdbc.Driver"; 

Then an error abour timezine appeared, so to fix this the URL should be modified whith this:

public static final String URL = 
"jdbc:mysql://localhost:3306/escuela? 
 useUnicode=true&useJDBCCompliantTimezoneShift
=true&useLegacyDatetimeCode=false&serverTimezone=UTC";

Then I used it like this:

public static Connection getConnection(){
   Connection con = null;

    try{
        Class.forName(DRIVER);
        con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        JOptionPane.showMessageDialog(null, "Conexion exitosa.");
    }
    catch(Exception e){
     e.printStackTrace();
    }
    return con;
}

Regards!

XaelGa
  • 140
  • 2
  • 10