NetBeans版本 7.0
jdk版本 1.7.0
数据库驱动程序:mysql-connector-java-5.1.13
如果在NetBeans执行以下代码,是可以访问数据库的public class Main { public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root", "password");
Statement stmt = (Statement) conn.createStatement();
ResultSet result = (ResultSet) stmt.executeQuery("select * from user");
while(result.next()) {
System.out.println(result.getString("host"));
}
}
catch (Exception ex) {
System.out.println("Error : " + ex.toString());
}
}
}
或者local用本地实际ip代替也可以访问数据库。但是在新建项目添加数据库驱动程序时出现图示的问题:
另外用以上代码访问远程数据库时也无法连接,提示
Error : com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failureThe last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
确定mysql没有禁用远程连接,用户名和密码均没有错。想请教一下,这究竟是什么原因造成的,怎么才能让我的java程序远程访问mysql数据库,或者说能添加上远程数据库连接
jdk版本 1.7.0
数据库驱动程序:mysql-connector-java-5.1.13
如果在NetBeans执行以下代码,是可以访问数据库的public class Main { public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root", "password");
Statement stmt = (Statement) conn.createStatement();
ResultSet result = (ResultSet) stmt.executeQuery("select * from user");
while(result.next()) {
System.out.println(result.getString("host"));
}
}
catch (Exception ex) {
System.out.println("Error : " + ex.toString());
}
}
}
或者local用本地实际ip代替也可以访问数据库。但是在新建项目添加数据库驱动程序时出现图示的问题:
另外用以上代码访问远程数据库时也无法连接,提示
Error : com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failureThe last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
确定mysql没有禁用远程连接,用户名和密码均没有错。想请教一下,这究竟是什么原因造成的,怎么才能让我的java程序远程访问mysql数据库,或者说能添加上远程数据库连接
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货