programing

Java를 Maria에 연결할 수 없습니다.DB

sourcejob 2023. 7. 8. 10:51
반응형

Java를 Maria에 연결할 수 없습니다.DB

Java를 MariaDB에 연결할 수 없습니다.저는 비슷한 질문에 답하는 것을 보았지만 아무도 제 문제를 해결하지 못했습니다.내 코드는 다음과 같습니다.

/** To change this license header, choose License Headers in Project
 *   Properties.  * To change this template file, choose Tools | Templates 
 * and open the template in the editor.  
 */
package mariadbtest; 

import java.sql.*;


/**
 * 
 * @author AAAA  
 */ 
public class MariaDBTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args){  
        try {
            Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
            String url = "jdbc:MariaDB://localhost:3306/customer"; // url to access the DB
            String user = "root";
            String pwd="password";
     
            Connection connection = DriverManager.getConnection(url,user,pwd);
     
            System.out.println("Connected to MariaDB");
        } 
        catch (SQLException e) {
            System.out.println("SQL Exception: "+ e.toString()); 
        } 
        catch (ClassNotFoundException cE) {
            System.out.println("Class Not Found Exception: "+ cE.toString()); 
        }
        catch (Exception ex) {
            System.out.println("Exception: "+ ex.toString()); 
        }

        // TODO code application logic here
    }
}

다음 사항에 유의하십시오.

  • 커넥터 mysql-connector-java-8.0.22를 사용하고 있습니다.
  • 저는 최신 버전의 자바를 가지고 있습니다.
  • MariaDB 버전은 10.3.27입니다.
  • 프로그램 파일의 java 폴더에 있는 lib 디렉토리에 커넥터를 복사했습니다.
  • 속성 --> 라이브러리 --> JAR/폴더 추가를 통해 커넥터 JAR 파일을 프로젝트에 추가했습니다.
  • MariaDB에는 MariaDB라는 서비스 이름이 있습니다.
  • 데이터베이스 이름은 customer입니다.

오류: SQL 예외: java.sql.SQL 예외:jdbc에 적합한 드라이버를 찾을 수 없습니다.MariaDB://localhost:3306/customer BUILD SUCCESSFUL (총 시간: 3초)

MySQL Connector/JJDBC 드라이버가 인식되지 않는 URL을 사용하는 것이 문제라고 생각합니다.이 URL

    jdbc:MariaDB://localhost:3306/customer

는 "MariaDB" 제공업체의 드라이버를 찾으라고 말합니다.그러나 MySQL 드라이버는 자신을 마리아DB 드라이버로 광고하지 않습니다.

해결책

우선 이것부터 없애야 합니다.

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

드라이버 클래스 이름이 필요하지 않으며 코드에 하드 배선되어 있습니다.연관된 예외 처리기 절도 제거합니다. (이것은 모두 부두 프로그래밍입니다.)20년 전에는 필요했을지도 모르지만, 지금은 그렇지 않습니다.)

드라이버 해결 문제는 두 가지 방법으로 해결할 수 있습니다.

  1. URL을 다음으로 변경합니다.

    jdbc:mysql://localhost:3306/customer
    

    MariaDB 설명서에 따르면 MySQL Connector/J 드라이버는 MariaDB와 호환됩니다.그러나 MySQL Connector/J 설명서에 언급되지 않은 JDBC URL에 "MariaDB"를 사용합니다.

  2. MariaDB Connector/J 드라이버로 전환합니다.문서에 따르면, 이것은 다음을 지원해야 합니다.

    jdbc:mariadb://localhost:3306/customer
    

    또는

    jdbc:mysql://localhost:3306/customer
    

문서에서 "프로토콜" 부분의 경우가 중요한지 여부는 명확하지 않습니다.그러나 MariaDB 문서의 구문 사양은 "MariaDB"가 아닌 "mariaadb"를 사용하므로 스타일 일관성을 위해 소문자를 사용하는 것이 좋습니다.

언급URL : https://stackoverflow.com/questions/64949595/cant-connect-java-to-mariadb

반응형