This question already has answers here:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
(35个答案)
三年前关闭。
`
import java.sql.*;

public class Match {
    public static void main(String args[]) throws Exception{
        DBConnection1 con = new DBConnection1();
        DBConnection con1 = new DBConnection();
        Connection conn = null,conn1=null;
        conn = con.getConnection();
        conn1 = con1.getConnection();
        Statement st = null;
        PreparedStatement pst = null;
        ResultSet rs = null,rs1 = null;
        st = conn.createStatement();
        String query1 = "SELECT Name FROM Employee WHERE Name=?";
        pst = conn1.prepareStatement(query1);
        st.setFetchSize(Integer.MIN_VALUE);
        String query = "SELECT name FROM emp";
        rs = st.executeQuery(query);
        String name = "";
        int count = 0;
        while(rs.next()){
            title = rs.getString("name");
            pst.setString(1, title);
            rs1 = pst.executeQuery();
            while(rs1.next()){
                    count++;
                    if(count % 100 == 0)
                        System.out.println(count);
            }
        }
        System.out.println(count);
    }
}

`
我正在从非常大的数据库中根据其他数据库中的一些值选择值。我正在一个while循环中运行select查询。在得到许多结果后运行java代码后,我得到
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure exception.

我不知道为什么会这样。如果你们有什么想法请帮忙
我已经阅读了基于这个例外的旧问题,但是没有一个是有用的。

最佳答案

import java.sql.*;

public class Match {
    public static void main(String args[]) throws Exception{
        DBConnection1 con = new DBConnection1();
        DBConnection con1 = new DBConnection();
        Connection conn = null,conn1=null;
        conn = con.getConnection();
        conn1 = con1.getConnection();
        Statement st = null;
        PreparedStatement pst = null;
        ResultSet rs = null,rs1 = null;
        st = conn.createStatement();
        st.setFetchSize(Integer.MIN_VALUE);
        String query = "SELECT name FROM emp";
        rs = st.executeQuery(query);
        String title = "",query1="";
        StringBuffer newQuery = new StringBuffer("SELECT Name FROM Employee WHERE ");
        int count = 0;
        long nameCount = 0L;
        while(rs.next()){
            nameCount++;
            title = rs.getString("name");
            query1 = "Name=? or";
            pst = conn1.prepareStatement(query1);
            pst.setString(1, title);

            newQuery.append(pst.toString().substring(pst.toString().indexOf('N'), pst.toString().length())+" ");
        }

        if ( nameCount > 0 ){
            String Query = newQuery.toString().substring(0,newQuery.toString().length() - 3);
            rs1 = conn1.createStatement().executeQuery(Query);
            while(rs1.next()){
                count++;
                if(count % 50 == 0)
                    System.out.println(count);
            }
        }
    }
}

使用PreparedStatement解决了SQL注入攻击的问题。现在代码开始工作了。

09-25 20:52