1

我可以用 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>
4

1 回答 1

0

我有解决方案....事实上,我应该把conn = dataSource.getConnection();userName 和 userPassword 放在后面.....所以我的代码现在看起来像这样:

        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=?");

有用 !

于 2021-03-26T09:46:50.093 回答