table of Contents
2. Select the download content and download it
3. Import the driver into the java project
2. What should I do if the driver still fails to be imported?
When we use java to operate mysql database in idea, it will appear:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at src.mySql.Jdbc.main(Jdbc.java:10)
1. General solution ideas
1. JDBC download link
2. Select the download content and download it
- Choose Platform Independent as the operating system, and the two records in the list below, the suffix tar.gz is the Linux version, and the .zip is the windows version.
- Download the windows version of the jdbc driver here, the version is 8.0.18
- Skip to log in, just click the content in the red box to download
3. Import the driver into the java project
- Click File-Project Structure in IDEA
- Module module, Dependencies tab
- Click the plus sign (+) on the far right and select JARS or directories
2. What should I do if the driver still fails to be imported?
1. Click File-Project Structure in IDEA
2. Select SDKs, click the plus sign (+) on the far right, select the jdbc driver of the windows version we downloaded before , and then click OK to confirm
3. Finally, when we connect to the MYSQL database on IDEA , we can use this driver
example:
package src.mySql;
import java.sql.*;
public class Jdbc {
public static void main(String[] args) throws Exception {
Connection conn = null;
try {
// 加载驱动类
Class.forName("com.mysql.cj.jdbc.Driver");
long start = System.currentTimeMillis();
// 建立连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1",
"root", "root");
long end = System.currentTimeMillis();
System.out.println(conn);
System.out.println("建立连接耗时: " + (end - start) + "ms 毫秒");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
result:
Reference link:
IDEA imports the jdbc driver of MySQL and operates the database