programing

Java 1.8.0_60, MariaDB v10.0 및 mariadb-java-client 1.2.2, "적절한 드라이버를 찾을 수 없습니다"

bestcode 2022. 11. 7. 22:06
반응형

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 벤더에 보고해야 합니다.이러한 요건/의존성에 대해서는 설명서에 기재되어 있지 않기 때문입니다.

회피책

현시점에서는 MariaDB 드라이버 1.2.0을 다운로드하기만 하면 됩니다.

언급URL : https://stackoverflow.com/questions/32654481/java-1-8-0-60-mariadb-v10-0-and-mariadb-java-client-1-2-2-no-suitable-drive

반응형