2013-10-08 75 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无效,或者至少看起来不像。