2014-09-12 294 views
-3

本地主機JDBC連接到服務器數據庫

connection = DriverManager 
    .getConnection("jdbc:mysql://localhost:3306/skead","admin", "admin"); 

這是訪問我的本地數據庫的方式。

但是如果我想訪問我的服務器數據庫(不是本地主機),該怎麼辦? 我該怎麼做?

+1

你就不能更改'的mysql://本地主機:3306/skead'到'MySQL的://>您的服務器IP在這裏<:3306/skead'? – Nekojimi 2014-09-12 16:22:40

+0

我想看downvoter的評論。這將有助於提問者看到他做了什麼錯誤 – Nabin 2014-09-12 16:22:55

+2

「* not localhost *」 - 好吧,用所需的主機名替換localhost ... – 2014-09-12 16:28:01

回答

2

,那麼你需要提供URL作爲

jdbc:mysql://yourRemoteHost:portToWhichRemoteHostMysqlListens/remoteDBName 
1
connection = DriverManager.getConnection("jdbc:mysql://Server_Name/DBName","UserName", "Password"); 
0
 String dbURL = "jdbc:mysql://serverIPAddress:3307/db_name"; 
     String username = "root"; 
     String password = "redmouse"; 

//  String dbURLs = "jdbc:mysql://localhost/db_name"; 
//  String usernames = "root"; 
//  String passwords = "redmouse"; 

     Connection dbCon = DriverManager.getConnection(dbURL, username, password);