Home > Back-end >  MySQL8.0 how links the Java driver! Online to solve! Has been an error!
MySQL8.0 how links the Java driver! Online to solve! Has been an error!

Time:09-26

Package com. Itheima. First;

Import the Java. SQL. *;
Public class Example01 {
Public static void main (String [] args) throws SQLException {
The Connection conn=null;
The Statement STMT=null;
The ResultSet rs=null;
Try {
Class.forname (". Com. Mysql. Cj. JDBC Driver ");
System. Out.println (" load driver success!" );
The String url="JDBC: mysql://localhost: 3306/JDBC? UseSSL=false& ServerTimezone=UTC ";
String username="root";
String password="246010";
Conn=DriverManager. GetConnection (url, username, password);
System. Out.println (" connect to the database successfully! The url ");
STMT=conn. CreateStatement ();
String SQL="select * from tb_user";
Rs=STMT. ExecuteQuery (SQL);
System. Out.println (" id | name | sex ", "
+ "| | email birthday");
While (rs), next ()) {
Int id=rs. Get int (" id ");
String name=rs. Get String (" name ");
String sex=rs. Get String (" sex ");
The String email=rs. Get String (" email ");
The Date birthday=rs. GetDate (" birthday ");
System. Out.println (id + + name + "|" "|" "
+ sex + + email + "|" "|" + birthday);
}
} the catch (Exception e) {
e.printStackTrace();
} the finally {
If (rs!=null) {rs. The close (); }
If (STMT!=null) {STMT. Close (); }
If (conn!=null) {conn. Close (); }
}
}
}

CodePudding user response:

Reference under the
https://blog.csdn.net/jadeshu/article/details/85345734
Change the

CodePudding user response:

Loading drive successful statement did not print it out, that is the problem, drive have you introduced a driver package, to see which version is it?
 Class. Class.forname (". Com. Mysql. Cj. JDBC Driver "); 
 for the Class. The class.forname (". Com. Mysql. JDBC Driver "); 
try?

CodePudding user response:

There's no way I changed you said language or error
. Loading class ` com. Mysql. JDBC Driver '. This is deprecated. The new Driver class is ` com. Mysql. Cj. JDBC. The Driver '. The Driver is automatically registered via The SPI and manual Loading of The Driver class is generally unnecessary.
Being loaded classes ". Com. Mysql. JDBC Driver ", this is not in favor of, the new Driver class is ". Com. Mysql. Cj. JDBC Driver ", Driver registration automatically by the SPI, generally do not need to manually load Driver class,
I have changed, why so angry!

CodePudding user response:

Try this: https://blog.csdn.net/weixin_43330884/article/details/102828455
I hope it can help you
  • Related