Java 和 MariaDB 连接驱动程序错误
Java and MariaDB connection drivers errors
我可以用 JDBC 和 DriverManager 连接 Java 和 Maria DB。有用。我可以在我的数据库表等中写一行......
但是使用“基本数据源”我无法连接到 MariaDB。我在 FEDORA 上使用 openJDK 15。
我不明白为什么我无法连接到数据库。
这是我的代码:
public static void main(String... args){
Connection conn = null;
try {
long identifiant = 24L;
String nom = "Errani";
String prenom = "Sara";
//data for connection with DB
String userName = "kent1";
String password= "*****";
BasicDataSource dataSource = new BasicDataSource();
conn = dataSource.getConnection();
dataSource.setUrl("jdbc:mariadb://localhost:3306/TENNIS");
dataSource.setUsername(userName);
dataSource.setPassword(password);
//conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/TENNIS?useSSL=false&useLegacyDatetimeCode=false&serverTimezone=Europe/Paris","kent1","*****");
//conn.setAutoCommit(false);
PreparedStatement preparedStatement = conn.prepareStatement("UPDATE JOUEUR SET NOM=?, PRENOM=? WHERE ID=?");
preparedStatement.setString(1, nom);
preparedStatement.setString(2, prenom);
preparedStatement.setLong(3, identifiant);
int nombreEnregistrementModifies = preparedStatement.executeUpdate();
System.out.println("Le nombre d'enregistrement modifié est : "+nombreEnregistrementModifies);
System.out.println("success");
//conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
finally {
try {
if (conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
这是我 运行 我的代码时的错误:
java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.commons.dbcp2.DriverFactory.createDriver(DriverFactory.java:75)
at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:462)
at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:528)
at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:734)
at com.mycompany.tennis.core.TestDeConnection.main(TestDeConnection.java:28)
Caused by: java.sql.SQLException: No suitable driver
at java.sql/java.sql.DriverManager.getDriver(DriverManager.java:298)
at org.apache.commons.dbcp2.DriverFactory.createDriver(DriverFactory.java:60)
... 4 more
这是我的 pom.xml Maven 和我对 MariaDB 连接器和 DBCP 的依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client -->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.7.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
</project>
我有办法....
事实上,我应该把 conn = dataSource.getConnection();
放在用户名和用户密码之后......
所以我的代码现在看起来像这样:
dataSource.setUrl("jdbc:mariadb://localhost:3306/TENNIS");
dataSource.setUsername(userName);
dataSource.setPassword(password);
//conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/TENNIS?useSSL=false&useLegacyDatetimeCode=false&serverTimezone=Europe/Paris","kent1","*****");
//conn.setAutoCommit(false);
conn = dataSource.getConnection();
PreparedStatement preparedStatement = conn.prepareStatement("UPDATE JOUEUR SET NOM=?, PRENOM=? WHERE ID=?");
有效!
我可以用 JDBC 和 DriverManager 连接 Java 和 Maria DB。有用。我可以在我的数据库表等中写一行...... 但是使用“基本数据源”我无法连接到 MariaDB。我在 FEDORA 上使用 openJDK 15。 我不明白为什么我无法连接到数据库。
这是我的代码:
public static void main(String... args){
Connection conn = null;
try {
long identifiant = 24L;
String nom = "Errani";
String prenom = "Sara";
//data for connection with DB
String userName = "kent1";
String password= "*****";
BasicDataSource dataSource = new BasicDataSource();
conn = dataSource.getConnection();
dataSource.setUrl("jdbc:mariadb://localhost:3306/TENNIS");
dataSource.setUsername(userName);
dataSource.setPassword(password);
//conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/TENNIS?useSSL=false&useLegacyDatetimeCode=false&serverTimezone=Europe/Paris","kent1","*****");
//conn.setAutoCommit(false);
PreparedStatement preparedStatement = conn.prepareStatement("UPDATE JOUEUR SET NOM=?, PRENOM=? WHERE ID=?");
preparedStatement.setString(1, nom);
preparedStatement.setString(2, prenom);
preparedStatement.setLong(3, identifiant);
int nombreEnregistrementModifies = preparedStatement.executeUpdate();
System.out.println("Le nombre d'enregistrement modifié est : "+nombreEnregistrementModifies);
System.out.println("success");
//conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
finally {
try {
if (conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
这是我 运行 我的代码时的错误:
java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.commons.dbcp2.DriverFactory.createDriver(DriverFactory.java:75)
at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:462)
at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:528)
at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:734)
at com.mycompany.tennis.core.TestDeConnection.main(TestDeConnection.java:28)
Caused by: java.sql.SQLException: No suitable driver
at java.sql/java.sql.DriverManager.getDriver(DriverManager.java:298)
at org.apache.commons.dbcp2.DriverFactory.createDriver(DriverFactory.java:60)
... 4 more
这是我的 pom.xml Maven 和我对 MariaDB 连接器和 DBCP 的依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client -->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.7.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
</project>
我有办法....
事实上,我应该把 conn = dataSource.getConnection();
放在用户名和用户密码之后......
所以我的代码现在看起来像这样:
dataSource.setUrl("jdbc:mariadb://localhost:3306/TENNIS");
dataSource.setUsername(userName);
dataSource.setPassword(password);
//conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/TENNIS?useSSL=false&useLegacyDatetimeCode=false&serverTimezone=Europe/Paris","kent1","*****");
//conn.setAutoCommit(false);
conn = dataSource.getConnection();
PreparedStatement preparedStatement = conn.prepareStatement("UPDATE JOUEUR SET NOM=?, PRENOM=? WHERE ID=?");
有效!