java程序连接MYSQL数据库原本还是可以连接成功的,但是现在用IP地址连接就是不成功,用localhost连接能成功!!root有远程访问的权限!端口也没有问题是3306!PING ip地址也ping的通!重装MYSQL后用IP地址连接还是不行!!!希望哪位高手帮帮忙!!急啊!!
报错:
访问数据库错误!com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failureLast packet sent to the server was 0 ms ago.
程序代码如下:
import java.sql.*;
public class MySQLDemo {
public static void main(String[] args)
{
Connection conn;
Statement stmt;
ResultSet rs;
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException e)
{
System.out.println("加载驱动程序错误!"+e);
}
try
{
String url="jdbc:mysql://10.2.85.57:3306/bank";
conn=DriverManager.getConnection(url,"root","1111");
stmt=conn.createStatement();//发送SQL语句
rs=stmt.executeQuery("SELECT *FROM account");//处理查询结果
while(rs.next())//循环输出结果集的每一行
{
System.out.print(rs.getString(1)+" ");
System.out.print(rs.getString(2)+" ");
System.out.print(rs.getString(3)+" ");
System.out.println(rs.getString(4));
}
rs.close();//关闭 ResultSet
stmt.close();//关闭 Statement
conn.close();//关闭Connection
}
catch(SQLException e)
{
System.out.println("访问数据库错误!"+e);
}
}
}
报错:
访问数据库错误!com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failureLast packet sent to the server was 0 ms ago.
程序代码如下:
import java.sql.*;
public class MySQLDemo {
public static void main(String[] args)
{
Connection conn;
Statement stmt;
ResultSet rs;
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException e)
{
System.out.println("加载驱动程序错误!"+e);
}
try
{
String url="jdbc:mysql://10.2.85.57:3306/bank";
conn=DriverManager.getConnection(url,"root","1111");
stmt=conn.createStatement();//发送SQL语句
rs=stmt.executeQuery("SELECT *FROM account");//处理查询结果
while(rs.next())//循环输出结果集的每一行
{
System.out.print(rs.getString(1)+" ");
System.out.print(rs.getString(2)+" ");
System.out.print(rs.getString(3)+" ");
System.out.println(rs.getString(4));
}
rs.close();//关闭 ResultSet
stmt.close();//关闭 Statement
conn.close();//关闭Connection
}
catch(SQLException e)
{
System.out.println("访问数据库错误!"+e);
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货