2017-05-14 43 views
1

当我尝试运行此代码时,出现一个错误,表示我错过了正确的驱动程序,但是当我下载驱动程序时,错误仍然存​​在。试图将java程序连接到mySQL服务器

public static void main(String[] args) 
{ 
    try { 
     Connection con = DriverManager.getConnection("108.167.137.42" ,"********", "********"); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     System.out.println(e.getMessage()); 
    } 
} 
+0

您需要使用'class.forName()'加载jdbc驱动程序。参考本教程https://www.javatpoint.com/example-to-connect-to-the-mysql-database – Rohan

+0

您需要驱动程序mySql,请加载驱动程序。如果您遇到困难,可以在谷歌上找到大量的样板代码来做到这一点。 –

+0

如果要远程连接到数据库,请确保使用提供的参数从外部访问它。 – dosdebug

回答

0

的网址不应该只包含IP住址就应该是这样的:

jdbc:mysql://[host1][:port1][,[host2][:port2]]...[/[database]] 

因此,你必须使用:

jdbc:mysql://108.167.137.42:3306/bd_name 

read more here

0

使用MySQL,JDBC c onnection字符串应该如下:

String URL = "jdbc:mysql://108.167.137.42:3306/database_name"; 
String USERNAME = "root"; 
String PASSWORD = "root"; 
Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD); 

随着所建议的代码的变化,你可能必须做你my.cnf文件中的以下变化太大,否则你将无法连接到你的MySQL数据库。

bind-address = xx.xx.xx.xx 

一旦更改此设置,您需要重新启动MySQL服务。希望这可以帮助!

+0

'bind-address'与连接有什么关系? – dosdebug

+0

@dosdebug,OP正在使用外部IP而不是通常的!因此建议改变! –