Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MySQL异常com.MySQL.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障_Java_Mysql - Fatal编程技术网

Java MySQL异常com.MySQL.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障

Java MySQL异常com.MySQL.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障,java,mysql,Java,Mysql,` ` 我根据其他数据库中的一些值从非常大的数据库中选择值。我正在while循环中运行select查询。在得到许多结果后运行java代码,我得到 import java.sql.*; public class Match { public static void main(String args[]) throws Exception{ DBConnection1 con = new DBConnection1(); DBConnection con1

`

`

我根据其他数据库中的一些值从非常大的数据库中选择值。我正在while循环中运行select查询。在得到许多结果后运行java代码,我得到

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);
    }
}
我不知道为什么会这样。如果你们有什么想法,请帮忙


我已经阅读了基于此异常的旧问题,但没有一个有帮助。

我认为此代码已优化,但可能有一些语法错误:

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;

        ResultSet rs = null,rs1 = null;
        st = conn.createStatement();
        //String query1 = "SELECT Name FROM Employee WHERE Name=?";

        st.setFetchSize(Integer.MIN_VALUE);
        String query = "SELECT name FROM emp";
        rs = st.executeQuery(query);
        String name = "";

        StringBuffer newQuery = new StringBuffer("SELECT Name FROM Employee WHERE");
        int count = 0;
        long nameCount = 0L;
        while(rs.next()){
            nameCount++;
            title = rs.getString("name");
            newQuery.append(" Name='" + title + "' or");
        }

        if ( nameCount > 0 ){
            newQuery = newQuery.subString( newQuery.length() - 3);  
            rs1 = conn1.createStatement.executeQuery( newQuery );
            while(rs1.next()){
                count++;
                if(count % 100 == 0)
                    System.out.println(count);
            }
        }
    }
}

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

您可以在服务器上执行的连续查询数量可能有限制。你能将任务划分为没有达到这个假设极限的任务吗?计算你正在执行的查询的数量可能是个好主意。另一种可能性是存在超时时间。如果查询花费的时间太长,您可能正在访问它。我正在计算查询运行的次数。280万个结果后失败查询运行了很多次还是很长时间?检查MySQL日志…查询要么不包含要满足的条件(如果有0个名称),要么总是有不正确的语法(尾随
)。如果纠正了这些错误,可能会导致达到最大允许的数据包。但是,可以通过微调最大查询长度和要执行的最大查询数来解决该问题。代码中已经处理了拖尾或拖尾问题,最大允许的数据包大小也可以增加,为了避免错误并提高性能,查询中存在语法错误,因为“或”,您可以尝试使用newQuery=newQuery.subString(0,newQuery.length()-3);而不是newQuery=newQuery.subString(newQuery.length()-3);如果不需要同步,也可以使用线程
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);
            }
        }
    }
}