2016-03-08 73 views
0
package pokemon; 
import java.sql.Connection; 
import java.sql.DriverManager; 


public class Main { 

public static void main(String[] args) throws Exception { 

    getConnection(); 

} 
public static Connection getConnection() throws Exception{ 
    try{ 
     String driver = "com.mysql.jdbc.Driver"; 
     String url = "jdbc:mysql://localhost:3306:Pokedex"; 
     String username = "test"; 
     String password = "password"; 
     Class.forName(driver); 

     Connection conn =  DriverManager.getConnection(url,username,password); 
     System.out.println("Connected"); 
     return conn;    
    } catch(Exception e){System.out.println(e);} 


    return null; 
} 
} 

我得到的錯誤用sql連接java。錯誤

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: 無法加載,因爲底層異常的連接類: 「java.lang.NumberFormatException:對於輸入字符串: 「3306:圖鑑」」。

我只是想看看我是否連接到數據庫,以便我可以在我的表中輸入數據。

+1

您的網址不正確。它應該是'jdbc:mysql:// localhost:3306/Pokedex' – Bunti

回答

1

你的數據庫的URL是不正確更換。它需要是

jdbc:mysql://localhost:3306/Pokedex 

只要有可能,您都會使用JDBC連接池,除非您有一個非常基本的應用程序。

+0

非常感謝!那確實解決了它。 – Coty