我正在做一个简单的程序来将MySQL数据库与Java连接,但是程序会引发ClassCastException错误。
这个问题说MySQL中的Unsigned Bigint等同于Java中的long。
下面是Java部分:
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();
}
我还包括了我创建的表的模式
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)
答案 0 :(得分:-2)
尝试这样做
Class.forName("com.mysql.jdbc.Driver").newInstance();