2016-04-26 10 views
0

Ich bin ein neuer Programmierer mit BlueJ ich eine Klasse Connexion, die eine Verbindung zu meiner MySQL-Datenbank namens erstellen hatte, aber ich bekomme diese Fehlermeldung:Wie beheben „java.lang.ClassNotFoundException: com.mysql.jdbc.Driver“ mit BlueJ und Wampserver

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Mein Code:

import java.sql.* ; 

public class Connexion{ 

    /** 
    * Connect to MySQL and read the table "Etat", then 
    * print the contents of the first column. 
    */ 
    public static void test() 
    { 
     try 
     { 
       // Load the database driver 
       Class.forName("com.mysql.jdbc.Driver") ; 

       // Get a connection to the database 
       Connection conn = DriverManager.getConnection( 

       "jdbc:mysql://localhost:3306/OACA?user=root&password=") ; 

       // Print all warnings 
       for(SQLWarning warn = conn.getWarnings(); warn != null; warn = warn.getNextWarning()) 
       { 
        System.out.println("SQL Warning:") ; 
        System.out.println("State : " + warn.getSQLState() ) ; 
        System.out.println("Message: " + warn.getMessage() ) ; 
        System.out.println("Error : " + warn.getErrorCode()) ; 
       } 

       // Get a statement from the connection 
       Statement stmt = conn.createStatement() ; 

       // Execute the query 
       ResultSet rs = stmt.executeQuery("SELECT * FROM Etat") ; 

       // Loop through the result set 
       while(rs.next()) 
       System.out.println(rs.getString(1)) ; 

       // Close the result set, statement and the connection 
       rs.close() ; 
       stmt.close() ; 
       conn.close() ; 
     } 
     catch(SQLException se) 
     { 
       System.out.println("SQL Exception:") ; 

       // Loop through the SQL Exceptions 
       while(se != null) 
       { 
        System.out.println("State : " + se.getSQLState() ) ; 
        System.out.println("Message: " + se.getMessage() ) ; 
        System.out.println("Error : " + se.getErrorCode()) ; 

        se = se.getNextException() ; 
       } 
     } 
     catch(Exception e) 
     { 
       System.out.println(e) ; 
     } 
    } 
     public static void main(String[] args) { 
     test(); 
    } 
} 

Wie kann ich eine Verbindung zu meiner MySQL v5.6.17-Datenbank herstellen, die sich in meinem Wampserver befindet? Ich denke, dass ich ein conector.jar brauche, aber ich weiß nicht, welches ich tatsächlich brauchen würde. Können Sie Ihre Meinung bitte geben?

+0

Mögliche Duplizieren: http://stackoverflow.com/questions/17484764/java-lang-classnotfoundexception-com-mysql-jdbc-driver-in-eclipse –

Antwort

0

Wahrscheinlich müssen Sie den Treiber in den Klassenpfad einschließen. Wenn Sie Maven verwenden, um den Treiber als Abhängigkeit in Ihrer pom.xml Datei

etwas wie folgt hinzu:

<dependency> 
    <groupId>mysql</groupId> 
    <artifactId>mysql-connector-java</artifactId> 
    <version>5.1.38</version> 
</dependency> 

Wenn Sie nicht Maven verwenden, versuchen Sie die Lösung in dieser Frage vorgeschlagen

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse

+0

ich es behoben war es Problem mit Anschlüssen aber in meinem Code System.out.println (rs.getString (1)); System.out.println (rs.getString (2)); System.out.println (rs.getString (3)); System.out.println (rs.getString (4)); System.out.println (rs.getString (5)); wenn ich es laufe, gibt es mir nicht den ganzen Inhalt – MakBad

Verwandte Themen