2017-05-04 41 views
0

我希望在MySQL和我的Java應用程序之間建立連接。 當我運行程序時,我得到這個錯誤:我的日誌信息是真實的。這是一個項目,我應該能夠在本地數據庫中添加,插入,刪除和更新客戶信息。用java連接到SQL_database

java.lang.ClassNotFoundException: com/mysql/jdbc/Driver.class 
    at java.lang.Class.forName0(Native Method) 
    at java.lang.Class.forName(Class.java:264) 
    at sample.Database.Connection.getConnection(Connection.java:26) 
    at sample.Presentation.Controller.<init>(Controller.java:19) 
    at sample.Presentation.Main.start(Main.java:13) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
    at java.lang.Thread.run(Thread.java:745) 

代碼

public class Controller { 

public Controller(Stage primaryStage) { 
    ButtonPane buttonPane = new ButtonPane(); 
    AddPane addPane = new AddPane(); 
    VBox vBoxStart = new VBox(); 
    Connection connection = new Connection(); 

    try { 
     connection = connection.getConnection(); 
    }catch (Exception e){ 
     System.out.println("No connection"); 
    } 
    Querys querys = new Querys(); 


    HBox hBoxOption = new HBox(buttonPane.ButtonPane(),addPane.getPane());// Menu and add/update/delete 
    HBox hBoxView = new HBox(); // Calender/tableView 

    vBoxStart.getChildren().addAll(hBoxOption,hBoxView); 

    Scene scene = new Scene(vBoxStart,1000,1000); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 

    Connection finalConnection = connection; 
    addPane.getButtonAddCustomer().setOnAction(event ->{ 
     querys.viewTable(finalConnection,"customer"); 
    }); 

} 
} 
public class Connection { 

String userName = "root"; 
String password = "rasmus12"; 
String dbms = "mysql"; 
String serverName = "localhost"; 
String portNumber = "3306"; 

public Connection getConnection() { 

    Connection conn = null; 
     try { 
      Class.forName("com/mysql/jdbc/Driver.class"); 
      conn = (Connection) DriverManager.getConnection(
        "jdbc:" + this.dbms + "://" + 
          this.serverName + 
          ":" + this.portNumber + "/", 
        this.userName,this.password); 
      System.out.println("Connected to database"); 
     } catch (SQLException e) { 
      System.out.println("SQL Exception in connection"); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } 


    return conn; 
} 
} 

public class Querys { 

public void viewTable(Connection con, String dbName) { 


    Statement stmt = null; 
    String query = "select * " + 
      "from " + dbName; 
    try { 

     ResultSet rs = stmt.executeQuery(query); 
     while (rs.next()) { 
      int id = rs.getInt("person_id"); 
      String firstName = rs.getString("person_first_name"); 
      String lastName = rs.getString("person_last_name"); 
      String adresse = rs.getString("person_adresse"); 
      String zip = rs.getString("person_zip"); 
      String city = rs.getString("person_city"); 
      int mobilePhone = rs.getInt("person_mobile_phone"); 
      int phone = rs.getInt("person_phone"); 
      int driverLicenceNumber = rs.getInt("person_driver_licence_number"); 
      String driverSinceDate = rs.getString("person_driver_since_date"); 
      String registrationNumber = rs.getString("person_registration_number"); 
      int orderId = rs.getInt("order_id"); 
      System.out.println(id + "\t" + firstName + 
        "\t" + lastName + "\t" + adresse + 
        "\t" + zip + 
        "\t" + city + 
        "\t" + mobilePhone + 
        "\t" + phone + 
        "\t" + driverLicenceNumber + 
        "\t" + driverSinceDate + 
        "\t" + registrationNumber+ 
        "\t" + orderId); 
     } 
    } catch (SQLException e) { 
     System.out.println("SQL Exception"); 
    } finally { 
     if (stmt != null) { 
      try { 
       stmt.close(); 
      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 
} 
+0

我已經刪除了IntelliJ標籤,因爲您的問題實際上並不是關於IntelliJ。您使用IntelliJ開發程序與此問題無關。鑑於此問題是由錯字/錯誤類名引起的(請參閱[由YCF_L回答](http://stackoverflow.com/a/43784715/466862)),我已決定將您的問題關閉到用於連接來自Java的MySQL。 –

回答

2

驅動程序不正確,你必須使用:

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

取而代之的是:

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

但調用CLA ss.forName()不再是必要的,因爲Java 6中,您可以閱讀this,和喜歡他的回答@duffymo提,請確保驅動程序jar在classpath

1

ClassNotFoundException手段存在的類裝載器不能找到MySQL的JDBC驅動程序類。

您需要下載MySQL連接器JAR並將其添加到您的CLASSPATH中。我想推薦MySQL JDBC tutorial

這裏有一個Java提示:不要在catch塊寫郵件:

catch (SQLException e) { 
    System.out.println("SQL Exception"); 
} 

打印或記錄整個堆棧跟蹤。這是您的消息提供的更多信息。

catch (SQLException e) { 
    e.printStackTrace(); 
} 
+0

好吧,我已經做了,我沒有得到一個錯誤了,但是當我運行該程序sqlException正在連接內部投射。 –

+0

發佈例外。它會幫助我們弄清楚你的下一個錯誤是什麼。 – duffymo

+0

我想使用這段代碼,但它說我需要創建類JDBCTutorialUtilities, catch(SQLException e){0} {0} {0} {0} JDBCTutorialUtilities.printSQLException(e); } –