복붙노트

[SQL] java.sql.SQLException이있다 : mysql : 없음 적합한 드라이버는 JDBC을 찾을 수 없습니다 // localhost를 : 3306 / DBNAME [중복]

SQL

java.sql.SQLException이있다 : mysql : 없음 적합한 드라이버는 JDBC을 찾을 수 없습니다 // localhost를 : 3306 / DBNAME [중복]

나는이 자바 프로그램이 있습니다 MySQLConnectExample.java를

import java.sql.*;
import java.util.Properties;

public class MySQLConnectExample {
    public static void main(String[] args) {
        Connection conn1 = null;
        Connection conn2 = null;
        Connection conn3 = null;

        try {
            String url1 = "jdbc:mysql://localhost:3306/aavikme";
            String user = "root";
            String password = "aa";

            conn1 = DriverManager.getConnection(url1, user, password);
            if (conn1 != null)
                System.out.println("Connected to the database test1");

            String url2 = "jdbc:mysql://localhost:3306/aavikme?user=root&password=aa";
            conn2 = DriverManager.getConnection(url2);
            if (conn2 != null) {
                System.out.println("Connected to the database test2");
            }

            String url3 = "jdbc:mysql://localhost:3306/aavikme";
            Properties info = new Properties();
            info.put("user", "root");
            info.put("password", "aa");

            conn3 = DriverManager.getConnection(url3, info);
            if (conn3 != null) {
                System.out.println("Connected to the database test3");
            }
        } catch (SQLException ex) {
            System.out.println("An error occurred. Maybe user/password is invalid");
            ex.printStackTrace();
        }
    }
}

나는이 같은 컴파일 :

E:\java mysql code driver>javac MySQLConnectExample.java

E:\java mysql code driver>java -cp mysql-connector-java-3.0.11-stable-bin.jar;.
MySQLConnectExample

나는이 오류가 발생합니다 :

An error occurred. Maybe user/password is invalid
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/
aavikme
        at java.sql.DriverManager.getConnection(DriverManager.java:596)
        at java.sql.DriverManager.getConnection(DriverManager.java:215)
        at MySQLConnectExample.main(MySQLConnectExample.java:20)

내가 도대체 ​​뭘 잘못하고있는 겁니까?

해결법

  1. ==============================

    1.당신이 첫 번째를 실행합니다 :

    당신이 첫 번째를 실행합니다 :

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

    이 힘은 드라이버는 자바 그 데이터베이스 연결 문자열을 처리하는 방법을 알고 그래서, 자신을 등록합니다.

    자세한 내용은 MySQL의 커넥터 참조를 참조하십시오.

  2. ==============================

    2.당신은 JDBC 드라이버를로드해야합니다. 코드 아래에 고려하십시오.

    당신은 JDBC 드라이버를로드해야합니다. 코드 아래에 고려하십시오.

    try {
               Class.forName("com.mysql.jdbc.Driver");
    
                // connect way #1
                String url1 = "jdbc:mysql://localhost:3306/aavikme";
                String user = "root";
                String password = "aa";
    
                conn1 = DriverManager.getConnection(url1, user, password);
                if (conn1 != null) {
                    System.out.println("Connected to the database test1");
                }
    
                // connect way #2
                String url2 = "jdbc:mysql://localhost:3306/aavikme?user=root&password=aa";
                conn2 = DriverManager.getConnection(url2);
                if (conn2 != null) {
                    System.out.println("Connected to the database test2");
                }
    
                // connect way #3
                String url3 = "jdbc:mysql://localhost:3306/aavikme";
                Properties info = new Properties();
                info.put("user", "root");
                info.put("password", "aa");
    
                conn3 = DriverManager.getConnection(url3, info);
                if (conn3 != null) {
                    System.out.println("Connected to the database test3");
                }
       } catch (SQLException ex) {
                System.out.println("An error occurred. Maybe user/password is invalid");
                ex.printStackTrace();
       } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
    
  3. ==============================

    3.갖는 컬럼 컬럼 1 표, 2 열, 3 열 column4, cloumn1 2 개 홀드 INT 값 및 칼럼 3, 4 홀드 VARCHAR로부터 데이터를 검색하는 예 (10)

    갖는 컬럼 컬럼 1 표, 2 열, 3 열 column4, cloumn1 2 개 홀드 INT 값 및 칼럼 3, 4 홀드 VARCHAR로부터 데이터를 검색하는 예 (10)

    import java.sql.*; 
    // need to import this as the STEP 1. Has the classes that you mentioned  
    public class JDBCexample {
        static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
        static final String DB_URL = "jdbc:mysql://LocalHost:3306/databaseNameHere"; 
        // DON'T PUT ANY SPACES IN BETWEEN and give the name of the database (case insensitive) 
    
        // database credentials
        static final String USER = "root";
        // usually when you install MySQL, it logs in as root 
        static final String PASS = "";
        // and the default password is blank
    
        public static void main(String[] args) {
            Connection conn = null;
            Statement stmt = null;
    
            try {
        // registering the driver__STEP 2
                Class.forName("com.mysql.jdbc.Driver"); 
        // returns a Class object of com.mysql.jdbc.Driver
        // (forName(""); initializes the class passed to it as String) i.e initializing the
        // "suitable" driver
                System.out.println("connecting to the database");
        // opening a connection__STEP 3
                conn = DriverManager.getConnection(DB_URL, USER, PASS);
        // executing a query__STEP 4 
                System.out.println("creating a statement..");
                stmt = conn.createStatement();
        // creating an object to create statements in SQL
                String sql;
                sql = "SELECT column1, cloumn2, column3, column4 from jdbcTest;";
        // this is what you would have typed in CLI for MySQL
                ResultSet rs = stmt.executeQuery(sql);
        // executing the query__STEP 5 (and retrieving the results in an object of ResultSet)
        // extracting data from result set
                while(rs.next()){
        // retrieve by column name
                    int value1 = rs.getInt("column1");
                    int value2 = rs.getInt("column2");
                    String value3 = rs.getString("column3");
                    String value4 = rs.getString("columnm4");
        // displaying values:
                    System.out.println("column1 "+ value1);
                    System.out.println("column2 "+ value2);
                    System.out.println("column3 "+ value3);
                    System.out.println("column4 "+ value4);
    
                }
        // cleaning up__STEP 6
                rs.close();
                stmt.close();
                conn.close();
            } catch (SQLException e) {
        //  handle sql exception
                e.printStackTrace();
            }catch (Exception e) {
        // TODO: handle exception for class.forName
                e.printStackTrace();
            }finally{  
        //closing the resources..STEP 7
                try {
                    if (stmt != null)
                        stmt.close();
                } catch (SQLException e2) {
                    e2.printStackTrace();
                }try {
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e2) {
                    e2.printStackTrace();
                }
            }
            System.out.println("good bye");
        }
    }
    
  4. ==============================

    4.내 코드는 다음과 같습니다, 같은 문제가 있었다 :

    내 코드는 다음과 같습니다, 같은 문제가 있었다 :

    private Connection conn = DriverManager.getConnection(Constant.MYSQL_URL, Constant.MYSQL_USER, Constant.MYSQL_PASSWORD);
    private Statement stmt = conn.createStatement();
    

    나는 드라이버 클래스를로드하지 않은,하지만 난 톰캣에 배포 할 때, 그러나, 그것은 작동하지 않습니다 내가 MySQL의에서 결과를 조회 할 수 있습니다, 로컬로 작동하고, 오류가 아래의 발생 :

    No suitable driver found for jdbc:mysql://172.16.41.54:3306/eduCloud
    

    내가봤을 때, 다른 답변을 게시 아래와 같이 그래서, 드라이버 클래스를로드 :

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

    이제 작품! 나는 그것이 로컬 잘 작동 왜, 나는 당신의 도움이 필요합니다 정말 감사 몰라!

  5. ==============================

    5.당신은 LIB 폴더로 MySQL의 커넥터 / J jar 파일을 복사하지 않은 수 있으며이 파일은 클래스 경로에있을 수 있습니다.

    당신은 LIB 폴더로 MySQL의 커넥터 / J jar 파일을 복사하지 않은 수 있으며이 파일은 클래스 경로에있을 수 있습니다.

    당신이하지 않은 경우, 제가 답을 정교하게된다 알려 주시기 바랍니다

  6. ==============================

    6.코드에서는 Class.forName을 ( "com.mysql.jdbc.Driver")을 누락;

    코드에서는 Class.forName을 ( "com.mysql.jdbc.Driver")을 누락;

    이것은 당신이 모든 것을 작동을 가지고 누락 된 것입니다.

  7. ==============================

    7.여기에 답가 Class.forName ( "my.vandor.Driver")를 사용하는 모든; 드라이버를로드 라인.

    여기에 답가 Class.forName ( "my.vandor.Driver")를 사용하는 모든; 드라이버를로드 라인.

    (더 나은) 다른 방법으로 당신이 당신의 JDBC 드라이버를 처리하는 방법의 소수를 제공 DriverManager에 도우미 클래스를 사용할 수 있습니다 / S.

    당신은에 할 수 있습니다

    예:

    Driver driver = new oracle.jdbc.OracleDriver();
    DriverManager.registerDriver(driver);
    Connection conn = DriverManager.getConnection(url, user, password);
    // ... 
    // and when you don't need anything else from the driver
    DriverManager.deregisterDriver(driver);
    

    또는 더 나은 아직, 데이터 소스를 사용

  8. ==============================

    8.이 시도

    이 시도

    String url = "jdbc:mysql://localhost:3306/<dbname>";
    String user = "<username>";
    String password = "<password>";
    conn = DriverManager.getConnection(url, user, password); 
    
  9. ==============================

    9.난 그냥 문제를 해결할 것이다, 당신의 MySQL 서버가 실행중인 포트를 확인, 비슷한 문제가 있었다

    난 그냥 문제를 해결할 것이다, 당신의 MySQL 서버가 실행중인 포트를 확인, 비슷한 문제가 있었다

    예를 들어, 내 코드했다 :

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:8080/bddventas","root","");
    

    난에 문자열을 변경

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bddventas","root","");
    

    짜잔!,이 세계는 내 서버는 해당 포트에서 실행 되었기 때문에

    이 도움말을 희망

  10. from https://stackoverflow.com/questions/22384710/java-sql-sqlexception-no-suitable-driver-found-for-jdbcmysql-localhost3306 by cc-by-sa and MIT license