Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION ** java.io.EOFException
MESSAGE: Can not read response from server. Expected to read 4,484 bytes, read 973 bytes before connection was unexpectedly lost.STACKTRACE:java.io.EOFException: Can not read response from server. Expected to read 4,484 bytes, read 973 bytes before connection was unexpectedly lost.
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1997)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2464)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2916)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:885)
at com.mysql.jdbc.MysqlIO.nextRow(MysqlIO.java:1360)
at com.mysql.jdbc.MysqlIO.readSingleRowSet(MysqlIO.java:2369)
at com.mysql.jdbc.MysqlIO.getResultSet(MysqlIO.java:451)
at com.mysql.jdbc.MysqlIO.readResultsForQueryOrUpdate(MysqlIO.java:2076)
at com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:1451)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1787)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3206)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1232)
at org.movingdata.getconnection.GetConnection.getDate(GetConnection.java:26)
at src.org.movingdata.main.Tag.setData(Tag.java:19)
at src.org.movingdata.main.Tag.main(Tag.java:73)
** END NESTED EXCEPTION **Last packet sent to the server was 1236844 ms ago.
用jdbc连接mysql时出现了这个异常,我修改了my.ini里面的配置wait_timeout和interactive_timeout,但是异常情况还是出来了,各位大侠帮帮看一下,在线等

解决方案 »

  1.   

    public class GetConnection {
    public static ResultSet getDate(String sql){
    String url = "jdbc:mysql://localhost:3306/mycompany";

    Connection connection;
    Statement stmt;

    try {   
    Class.forName("org.gjt.mm.mysql.Driver");
    // Class.forName("com.mysql.jdbc.Driver").newInstance();
    connection = DriverManager.getConnection(url, "root", "admin" );   
    stmt= connection.createStatement();

    ResultSet rs = stmt.executeQuery(sql);

    if(rs!=null){
    return rs;
    } }  
    catch ( ClassNotFoundException cnfex ) {   
    System.out.println(" 111 "+cnfex.getMessage());  
    }  

    catch ( SQLException sqlex ) { 
    System.out.println(" 222 "+sqlex.getMessage());
    }  
    return null;

    }
    public static void main(String args[]) throws ParseException{
    String getdata = "select * from bookinfo";
    getDate(getdata);
    }
    这个就是数据库的连接代码了,每次运行的时候都是那样的
      

  2.   

    不能这样写吧,getDate()返回rs时,数据连接就自动关闭了,所以rs也被迫关闭。
    你把Connection connection定义为全局变量,
    即public class GetConnection {
    private Connection connection;
    public static ResultSet getDate(String sql){
    String url = "jdbc:mysql://localhost:3306/mycompany";
    Statement stmt;try {   
    Class.forName("org.gjt.mm.mysql.Driver");
    // Class.forName("com.mysql.jdbc.Driver").newInstance();
    connection = DriverManager.getConnection(url, "root", "admin" );   
    stmt= connection.createStatement();ResultSet rs = stmt.executeQuery(sql);if(rs!=null){
    return rs;
    }}   
    catch ( ClassNotFoundException cnfex ) {   
    System.out.println(" 111 "+cnfex.getMessage());   
    }   catch ( SQLException sqlex ) {  
    System.out.println(" 222 "+sqlex.getMessage());
    }   
    return null;}
    public static void main(String args[]) throws ParseException{
    String getdata = "select * from bookinfo";
    getDate(getdata);
    }这样试试看,不过建议你不要返回ResultSet,最好返回List,或者String之类的
    这样即使关闭数据库连接,数据还可以保存。public class GetConnection {
    public static List getDate(String sql){
    String url = "jdbc:mysql://localhost:3306/mycompany";
    Connection connection;
    Statement stmt;
    try {   
    Class.forName("org.gjt.mm.mysql.Driver");
    // Class.forName("com.mysql.jdbc.Driver").newInstance();
    connection = DriverManager.getConnection(url, "root", "admin" );   
    stmt= connection.createStatement();ResultSet rs = stmt.executeQuery(sql);
    List datalist=new ArrayList();while(rs.next()){
       //这里把数据装入datalist中
    }}   
    catch ( ClassNotFoundException cnfex ) {   
    System.out.println(" 111 "+cnfex.getMessage());   
    }   catch ( SQLException sqlex ) {  
    System.out.println(" 222 "+sqlex.getMessage());
    }   
    stmt.close();
    connection.close();//记得要关闭连接
    return datalist;}
    public static void main(String args[]) throws ParseException{
    String getdata = "select * from bookinfo";
    List datalist=getDate(getdata);
    }