2013-10-08 107 views
0

我正在嘗試建立到遠程mysql數據庫的連接。我已經將jar連接器放到了war/WEB-INF/lib文件夾中並添加了一個庫(構建路徑)。連接上的MySQL JDBC錯誤

不過我收到此錯誤:

java.sql.SQLException: No suitable driver found for jdbc:sql4.freemysqlhosting.net 
    at java.sql.DriverManager.getConnection(Unknown Source) 

下面是完整的代碼:

/** 
* 
*/ 
package com.infograph.dbconnection; 

import java.sql.DriverManager; 
import java.sql.Connection; 
import java.sql.SQLException; 

/** 
* @author Vlad 
* 
*/ 
public class DBConnection 
{ 
    /** 
    * 
    */ 
    public DBConnection() 
    { 
     try 
     { 
      Class.forName("com.mysql.jdbc.Driver"); 
     } 
     catch (ClassNotFoundException e) 
     { 
      System.out.println("Where is your MySQL JDBC Driver?"); 
      e.printStackTrace(); 
      return; 
     } 

     System.out.println("MySQL JDBC Driver Registered!"); 
     Connection connection = null; 

     try 
     { 
      connection = DriverManager.getConnection("jdbc:sql4.freemysqlhosting.net","user", "pass"); 

     } 
     catch (SQLException e1) 
     { 
      System.out.println("Connection Failed! Check output console"); 
      e1.printStackTrace(); 
      return; 
     } 

     if (connection != null) 
     { 
      System.out.println("You made it, take control your database now!"); 
     } 
     else 
     { 
      System.out.println("Failed to make connection!"); 
     } 
    } 
} 

問題是什麼? 10倍!

+0

什麼是'sql4.freemysqlhosting.net'? –

+0

它是數據庫的主機 – vlio20

回答

1

我想你想用的是

connection = DriverManager.getConnection("jdbc:mysql://sql4.freemysqlhosting.net","user", "pass"); 

您可能需要添加一個端口號和架構名稱。

您提供的網址jdbc:sql4.freemysqlhosting.net無效,或者至少看起來不像。