2016-04-26 38 views
0

我与BlueJ的一个新的程序员,我不得不创建一个名为Connexion类连接到我的MySQL数据库,但我得到这个错误:如何解决“抛出java.lang.ClassNotFoundException:com.mysql.jdbc.Driver”与BlueJ的和Wampserver

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

我的代码:

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(); 
    } 
} 

那么,如何连接到我的Wampserver中的MySQL v5.6.17数据库。 我认为我需要一个coonector.jar但我不知道我会真正需要哪一个。 你能给出你的意见吗?

+0

可能重复:http://stackoverflow.com/questions/17484764/java-lang-classnotfoundexception-com-mysql-jdbc-driver-in-eclipse –

回答

0

您可能需要将驱动程序包含在类路径中。 如果你使用Maven,添加驱动程序作为依赖在你的pom.xml文件

是这样的:

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

如果你不使用Maven,试图在这一问题提出的解决方案:

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

+0

我固定它是与连接器问题但在我的代码中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));当我运行它时,它并没有给我所有的内容 – MakBad

相关问题