Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://192.168.180.64:3306/student";// 链接数据库语句
conn =DriverManager.getConnection(url,"root","123456"); // 链接数据库
stmt = (Statement) conn.createStatement();
rs = stmt.executeQuery("select * from student");
while (rs.next()) {
System.out.println(rs.getString(1));
}
连接时就会提示Communications link failure的错误,网上说是连接超时的问题,但我查过服务器wait_timeout 的时间是280
------解决方案--------------------
试一下这这样行不行
String url = "jdbc:mysql://192.168.180.64:3306;DatabaseName=student";// 链接数据库语句
------解决方案--------------------
public class sqlcon {
public static Connection getConnection() throws SQLException ,
java.lang.ClassNotFoundException{
String url = "jdbc:mysql://localhost:3306/***";
Class.forName("com.mysql.jdbc.Driver");
String userName = "root";
String password = "123456";
Connection con = DriverManager.getConnection(url,userName,password);
return con;
}
}
注意你安装mysql的时候设置的端口是不是3306!!