0

I am doing a simple program to connect MySQL database with Java, but the program throws the ClassCastException error.

Java long to MySql

This question says that Unsigned Bigint in MySQL is equivalent to long in Java.

Given below is the java part:

public static void main(String[] args) throws ClassNotFoundException, SQLException {
    // TODO code application logic here

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql:///check1","uname","pwd");
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("select * from t1");
    while(rs.next()){
        System.out.println(rs.getObject(1)+ " "+ rs.getObject(2));
    }
    rs.close();
    st.close();
    con.close();
}

I am also including the schema for the table I created

mysql> desc t1;                                                                                                                                                         
+--------+-------------+------+-----+---------+----------------+                                                                                                        
| Field  | Type        | Null | Key | Default | Extra          |                                                                                                        
+--------+-------------+------+-----+---------+----------------+                                                                                                        
| rollno | bigint(20)  | NO   | PRI | NULL    | auto_increment |                                                                                                        
| name   | varchar(40) | NO   |     | NULL    |                |                                                                                                        
+--------+-------------+------+-----+---------+----------------+                                                                                                        
2 rows in set (0.39 sec)   
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Anurag
  • 3
  • 1
  • 5
  • Have you checked this? [bigint in mysql to java](https://stackoverflow.com/questions/46998896/mysql-bigint-conversion-to-java-long) – Worthless Oct 03 '18 at 17:24
  • 2
    Where is the code that throws your exception? – PM 77-1 Oct 03 '18 at 17:24
  • 1
    Please post the full exception stacktrace – Mark Rotteveel Oct 03 '18 at 17:27
  • Its the big int you have to get its long value in java. But still the stacktrace would be helpful to confirm – urs_ng Oct 03 '18 at 17:33
  • 1
    I'm really sorry I forgot to post the full stacktrace it totally slipped my mind. Also, @LukeWoodward thanks, your link helped. The problem was the JDBC connector I was using. Using the latest version solved the problem. Thanks for your help everyone – Anurag Oct 04 '18 at 03:22

1 Answers1

-2

Try to do this way

Class.forName("com.mysql.jdbc.Driver").newInstance();
Hihikomori
  • 950
  • 6
  • 12
  • That is entirely unnecessary and will not solve the problem. It was once (15+ years ago) necessary with a buggy version of the predecessor of MySQL Connector/J to get it to register (because they had put the driver registration in an instance initializer and not in a static initializer). And using `Class.forName` itself is not even necessary in this case (and hasn't been for 10+ years), assuming the OP is using a JDBC 4 compliant driver. – Mark Rotteveel Oct 03 '18 at 17:41
  • If the driver had not been loaded correctly, the OP would have received a `SQLException` with the "No suitable driver" exception message. – Mark Rotteveel Oct 03 '18 at 17:44