微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java 1.8.0_60,MariaDB v10.0和mariadb-java-client 1.2.2,“找不到合适的驱动程序”

我试图找出为什么我无法连接到笔记本电脑上的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中没有提到这个要求/依赖.

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐