无法连接到MariaDB

时间:2019-05-05 20:35:12

标签: java mariadb

我想使用我的MariaDB数据库,但收到错误消息:

  

java.lang.ClassNotFoundException:org.mariadb.jdbc.Driver

我的 pom.xml 包括:

<dependencies>
    <dependency>
        <groupId>org.mariadb.jdbc</groupId>
        <artifactId>mariadb-java-client</artifactId>
        <version>1.1.7</version>
    </dependency>
</dependencies>

我还下载了文件mariadb-java-client-2.4.1-sources.jar并将其放入WEB-INF / lib。

Database.java

package ch.yourclick.zt;

import java.sql.*;

    public class Database {
        public static void main(String[] args) throws ClassNotFoundException, SQLException {
            // Ensure we have mariadb Driver in classpath
            Class.forName("org.mariadb.jdbc.Driver");

            // create our mysql database connection
            String host = "localhost";
            String dbname = "zt_productions";
            String url = "jdbc:mariadb://" + host + "/" + dbname;
            String username = "root";
            String password = "test";
            Connection conn = DriverManager.getConnection(url, username, password);

            // our SQL SELECT query.
            // if you only need a few columns, specify them by name instead of using "*"
            String query = "SELECT * FROM users";

            // create the java statement
            Statement st = conn.createStatement();

            st.close();
        }
    }

我也尝试过Class.forName("org.mariadb.jdbc.Driver").newInstance();

无论我尝试什么,我总是收到相同的错误消息。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您使用了错误的jar文件。消息来源不会帮助您。

尝试使用mariadb-java-client-2.4.1-sources.jar代替mariadb-java-client-2.4.1.jar