2014-09-12 233 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);