I get the following error when I try to run a simple program to connect to MySql database in IntelliJ IDEA:
Код: Выделить всё
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:391)
at java.base/java.lang.Class.forName(Class.java:382)
at config.DatabaseConfig.getConnection(DatabaseConfig.java:29)
at Main.main(Main.java:8)
db.properties
Код: Выделить всё
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/database
jdbc.user=user
jdbc.password=password
Код: Выделить всё
package config;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class DatabaseConfig {
private static Connection connection = null;
public static Connection getConnection() {
if (connection != null) {
return connection;
} else {
try {
// Load database properties
InputStream inputStream = DatabaseConfig.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
properties.load(inputStream);
// Get properties
String driver = properties.getProperty("jdbc.driver");
String url = properties.getProperty("jdbc.url");
String user = properties.getProperty("jdbc.user");
String password = properties.getProperty("jdbc.password");
// Register JDBC driver
Class.forName(driver);
// Open a connection
connection = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
}
}
Код: Выделить всё
import config.DatabaseConfig;
import java.sql.Connection;
public class Main {
public static void main(String[] args) {
// Initialize database connection
Connection connection = DatabaseConfig.getConnection();
if (connection != null) {
System.out.println("Successfully connected to the database.");
// Perform database operations
} else {
System.out.println("Failed to connect to the database.");
}
// Close the connection when done
try {
if (connection != null && !connection.isClosed()) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
- I followed this link: Database Tools and SQL. I was able to connect to my database and successfully run a test connection. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error.
- I put the MySQL jar file in my project directory: RootFolder/MySql.jar then added to my dependencies as follows: file -> Project Structure -> Modules -> Dependencies -> + -> JARs or Directories -> RootFolder/MySql.jar. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error.
- I downloaded the latest MySql.jar file from this link: MySql Community Downloads. The version is mysql-connector-j.8.3.0. I added it to my dependencies as follows: file -> Project Structure -> Modules -> Dependencies -> + -> JARs or Directories -> C:\Users\me\Downloads\mysql-connector-j-8.3.0\mysql-connector-j-8.3.0\mysql-connector-j-8.3.0.jar. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error. (I tried this with zipped and unzipped jar file)
- I also tried adding the jar file as a library as follows: file -> Project Structure -> libraries -> + -> Java -> RootFolder/MySql.jar. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error.
- I also tried adding the jar file as a library as follows: file -> Project Structure -> libraries -> + -> Java -> C:\Users\me\Downloads\mysql-connector-j-8.3.0\mysql-connector-j-8.3.0\mysql-connector-j-8.3.0.jar. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error.
- I tried adding the library from Maven as follows: file -> Project Structure -> libraries -> + -> From Maven -> com.mysql:mysql-connector-j:8.3.0. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error.
- I followed ALL of the answers on this stackoverflow post: Connect Java to a MySQL database. But when I run Main.java, it prints "Failed to connect to the database" and shows the above error.
Источник: https://stackoverflow.com/questions/781 ... ql-cj-jdbc