Java 1.8.0_60,MariaDB v10.0 和,mariadb-java-client 1.2.2,"No suitable driver found"

Java 1.8.0_60, MariaDB v10.0 and , mariadb-java-client 1.2.2, "No suitable driver found"

我想找出为什么我无法在笔记本电脑上连接到 mariadb。 MariaDB 安装了多个数据库,我可以毫无问题地使用 HeidiSQL 进行连接。

我试图让一个 Java 应用程序连接到数据库,但我得到:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mysql
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)

我已经下载 "mariadb-java-client-1.2.2.jar" 并将其添加到项目中。

我的数据库 URI 是:

jdbc:mysql://localhost:3306/mysql

我尝试更改使用:

jdbc:mariadb://localhost:3306/mysql

结果相同。我以前在另一台 PC 上运行过,但我不知道为什么它不能在笔记本电脑上运行?用户名和密码正确,与连接HeidiSQL时使用的相同。

我都试过:

Class.forName("com.mysql.jdbc.Driver");

Class.forName("org.mariadb.jdbc.Driver"); 

注册图书馆然后我读到这些不是必需的....我错过了什么?

代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class clsDB {
//The name of this class    
    private static final String TAG = clsDB.class.toString();   
//Define database URL, user name and password
    private static final String SERVER_ADDR = "localhost";
//The database address on Windows development system    
    private static final String DB_URL = "jdbc:mariadb://" + SERVER_ADDR +  ":3306/mysql";
    private static final String USER_NAME = "root";
    private static final String PASSWORD = "RRCmcs2014";
//Database connection object    
    private Connection m_con = null;
/**
 * Class constructor    
 * @throws Exception 
 */
    public clsDB() throws Exception {
//Create connection to database     
        connect();
    }
/**
 * @param strMethod the method the error occurs in
 * @param strMsg the message to display
 */
    private void errorMsg(String strMethod, String strMsg) {
        System.out.println(TAG + "." + strMethod + ": " + strMsg);
    }
/**
 * Destructor
 */
    protected void finalize() throws Throwable {
        close();
    }
/**
 * Attempts to close database connection    
 * @throws SQLException 
 */
    public void close() throws SQLException {
        if ( m_con != null && m_con.isClosed() == false ) {
            m_con.close();
        }       
    }
/**
 * Commits any changes to the database  
 * @throws SQLException
 */
    public void commit() throws SQLException {
        if ( m_con != null && m_con.isClosed() == false ) {
            m_con.commit();
        }
    }
/**
 * Attempts to connect to database  
 * @throws Exception 
 */
    private void connect() throws Exception {
//Get a connection to the database
        m_con = (Connection)DriverManager.getConnection(DB_URL,     
                                                        USER_NAME, 
                                                        PASSWORD);
        if ( m_con == null ) {
            throw new Exception( "Cannot connect to database!" );
        }
//Disable auto-commit       
        m_con.setAutoCommit(false);
    }
/**
 * Performs SQL execute or update   
 * @param strSQL, the SQL statement to perform
 * @return If an insert was performed then the insert ID, 
 *         If an update then the number of effected rows
 */
    public long execute(String strSQL) throws SQLException {
        Statement st = null;
        long lngRC = 0;
        try{
            if ( m_con != null ) {
                if ( m_con.isClosed() == true ) {
                    try{
                        connect();
                    } catch( Exception ex ) {
                        errorMsg("query", ex.getMessage());
                    }
                }
                st = (Statement)m_con.createStatement();

                if ( (lngRC = (int)st.executeUpdate(strSQL,    Statement.RETURN_GENERATED_KEYS)) > 0 ) {
                    if ( strSQL.toUpperCase().startsWith("INSERT") == true ) {
                        ResultSet keys = st.getGeneratedKeys();

                        if ( keys != null ) {
                            keys.next();
                            lngRC = keys.getLong(1);
                        }
                    }                   
                    m_con.commit();
                }
            }
        } catch( SQLException ex ) {
            errorMsg("execute", ex.getMessage());
        } finally {
            if ( st != null ) {
                st.close();
            }
        }
        return lngRC;
    }       
/**
 * @return The database connection object
 */
    public Connection getConnection() {
        return m_con;
    }
/**
 * Performs SQL query   
 * @param strSQL, the SQL statement to perform
 * @return the result of the query
 */
    public ResultSet query(String strSQL) throws SQLException {
        Statement st = null;
        ResultSet rs = null;
        try{
            if ( m_con != null ) {
                if ( m_con.isClosed() == true ) {
                    try{
                        connect();
                    } catch( Exception ex ) {
                        errorMsg("query", ex.getMessage());
                    }
                }
                st = (Statement)m_con.createStatement();
                rs = st.executeQuery(strSQL);
            }
        } catch( SQLException ex ) {
            errorMsg("query", ex.getMessage());
        }
        return rs;
    }   
}

似乎 Mariadb 驱动程序 1.2.2 对 org.slf4j.LoggerFactory 有隐藏的依赖。

其实用命令就能看到这个

Class.forName("org.mariadb.jdbc.Driver");

并查看生成的堆栈跟踪。 JDBC 4 及更高版本不需要该命令,但它对于跟踪 JDBC 驱动程序自动注册失败的原因很有用。

因此,您获得的堆栈跟踪是:

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at org.mariadb.jdbc.Driver.<clinit>(Driver.java:71)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at testing.clsDB.connect(clsDB.java:65)
    at testing.clsDB.<init>(clsDB.java:26)
    at testing.SimpleTest.main(SimpleTest.java:7)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 6 more

这是一个错误,应该向 MariaDB 的供应商报告,因为他们在 documentation.

中没有提到这个 requirement/dependency

解决方法

目前,您的解决方案只是下载 MariaDB driver 1.2.0.