“java.sql.SQLException:结果集结束后”错误

时间:2017-03-24 10:27:47

标签: java mysql

以下是有错误的代码:

  

java.sql.SQLException:结果集结束后

请帮忙。

import java.sql.*;
import java.util.ArrayList;

public class ExtractCallInfo1 {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost?autoReconnect=true&useSSL=false";  
static final String USER = "root";
static final String PASS = "root";
public static boolean checkDBExists(String dbName){

    try{
        Class.forName(JDBC_DRIVER); //Register JDBC Driver

        //System.out.println("Creating a connection...");
        Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); //Open a connection

        ResultSet resultSet = conn.getMetaData().getCatalogs();

        while (resultSet.next()) {

          String databaseName = resultSet.getString(1);
            if(databaseName.equals(dbName)){
                return true;
            }
        }
        resultSet.close();

    }
    catch(Exception e){
        e.printStackTrace();
    }

    return false;
}

public static boolean checkTABLEExists(String DB_NAME, String TABLE_NAME){
    try{
        Class.forName(JDBC_DRIVER);
        if(!checkDBExists(DB_NAME)){
            System.out.println("Database "+DB_NAME+" doesn't exist");
            return false;
        }
        System.out.println("Creating a connection...");
        Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
        Statement stmt = conn.createStatement();
        String sql = "USE "+DB_NAME;
        stmt.executeUpdate(sql);
        DatabaseMetaData dbm = conn.getMetaData();
        ResultSet tables = dbm.getTables(null, null,TABLE_NAME, null);
        if (tables.next()) {
            return true;
        }
        else {
            return false;
        }

    }
    catch(Exception e){
        e.printStackTrace();
    }
    return false;
}
public static void main(String[] args){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
        Statement stmt = conn.createStatement();
        String DB_NAME = "SIP";
        String PACKET_TABLE_NAME = "PACKETDATABASE";
        String USERS_TABLE_NAME = "USERS";
        String COMPLETE_TABLE_NAME = "COMPLETECALLS";
        String INCOMPLETE_TABLE_NAME = "INCOMPLETECALLS";
        String CALLERAVERAGE_TABLE_NAME = "CALLERAVERAGE";
        String CALLERFREQUENCY_TABLE_NAME = "CALLERFREQUENCY";
        String CALLERCALLEEAVERAGE_TABLE_NAME = "CALLERCALLEEAVERAGE";
        String CALLERCALLEEFREQUENCY_TABLE_NAME = "CALLERCALLEEFREQUENCY";
        String CALLERTOTALDURATION_TABLE_NAME = "CALLERTOTALDURATION";
        String CALLERBETA_TABLE_NAME = "CALLERBETA";
        String CALLERINCOMPLETEFREQUENCY_TABLE_NAME = "CALLERINCOMPLETEFREQUENCY";
        String CALLERGAMMA_TABLE_NAME = "CALLERGAMMA";
        String CALLERALPHA_TABLE_NAME = "CALLERALPHA";
        String CALLEEFREQUENCY_TABLE_NAME = "CALLEEFREQUENCY";
        String CALLERRHO_TABLE_NAME = "CALLERRHO";
        String CALLERAVERAGECLASSIFICATION_TABLE_NAME = "CALLERAVERAGECLASSIFICATION";

        if(!checkDBExists(DB_NAME)){
            System.out.println("Database "+DB_NAME+" doesn't exist");
            return ;
        }

        String sql = "USE " +DB_NAME;
        stmt.executeUpdate(sql);

        sql = "DROP TABLE IF EXISTS "+USERS_TABLE_NAME;
        stmt.executeUpdate(sql);

        sql = "CREATE TABLE "+USERS_TABLE_NAME+" "+
                "SELECT CallerName AS User from "+PACKET_TABLE_NAME+" "+
                "UNION "+
                "SELECT CalleeName AS User from "+PACKET_TABLE_NAME;
        stmt.executeUpdate(sql);

        sql = "DROP TABLE IF EXISTS "+COMPLETE_TABLE_NAME;
        stmt.executeUpdate(sql);

        sql = "CREATE TABLE "+COMPLETE_TABLE_NAME+" "+
                "(CallId VARCHAR(255) NOT NULL PRIMARY KEY," +
                "CallerName VARCHAR(255) NOT NULL," + 
                "CallerIp VARCHAR(255) NOT NULL," + 
                "CalleeName VARCHAR(255) NOT NULL," + 
                "CalleeIp VARCHAR(255) NOT NULL," +
                "CallDuration DOUBLE NOT NULL)";
        stmt.executeUpdate(sql);

        sql = "DROP TABLE IF EXISTS "+INCOMPLETE_TABLE_NAME;
        stmt.executeUpdate(sql);

        sql = "CREATE TABLE "+INCOMPLETE_TABLE_NAME+" "+
                "(CallId VARCHAR(255) NOT NULL PRIMARY KEY," +
                "CallerName VARCHAR(255) NOT NULL," + 
                "CallerIp VARCHAR(255) NOT NULL," + 
                "CalleeName VARCHAR(255) NOT NULL," + 
                "CalleeIp VARCHAR(255) NOT NULL," +
                "CallDuration DOUBLE)";
        stmt.executeUpdate(sql);

        sql = "SELECT DISTINCT CallId from "+ PACKET_TABLE_NAME;
        ArrayList<String> uniqueCallIds = new ArrayList<String>();
        ResultSet result = stmt.executeQuery(sql);
        while(result.next()){
            uniqueCallIds.add(result.getString("CallId"));
        }
        for(int i = 0;i < uniqueCallIds.size();i++){
            String cur_id = uniqueCallIds.get(i);
            sql = "SELECT * from "+PACKET_TABLE_NAME+" "+
            "where Method = 'INVITE' and CallId = '"+cur_id+"'";
            Statement stmt1 = conn.createStatement();
            Statement stmt2 = conn.createStatement();
            ResultSet result_one = stmt1.executeQuery(sql);
            sql = "SELECT * from "+PACKET_TABLE_NAME+" "+
            "where Method = 'BYE' and CallId = '"+cur_id+"'";
            ResultSet result_two = stmt2.executeQuery(sql);
            if(!result_two.next()){
                result_one.next();
                sql = "INSERT INTO "+INCOMPLETE_TABLE_NAME+
                        " values('"+result_one.getString("CallId")+"','"+
                        result_one.getString("CallerName")+"','"+
                        result_one.getString("CallerIp")+"','"+
                        result_one.getString("CalleeName")+"','"+
                        result_one.getString("CalleeIp")+"',"+
                        "NULL"+")";
                stmt.executeUpdate(sql);
            }
            else{
                long call_start = Long.MAX_VALUE;
                while(result_one.next()){
                    String time = result_one.getString("TimeOfArrival");
                    long cur = Long.parseLong(time);
                    if(cur < call_start){
                        call_start = cur;
                    }
                }
                result_two.next();
                long call_end = Long.parseLong(result_two.getString("TimeOfArrival"));
                long dura = (call_end - call_start);
                double duration = dura/1000.0;
                sql = "INSERT INTO "+ COMPLETE_TABLE_NAME +
                        " values('"+result_two.getString("CallId")+"','"+
                        result_two.getString("CalleeName")+"','"+
                        result_two.getString("CalleeIp")+"','"+
                        result_two.getString("CallerName")+"','"+
                        result_two.getString("CallerIp")+"',"+
                        duration+")";
                stmt.executeUpdate(sql);
            }



        }

        sql = "DROP TABLE IF EXISTS "+CALLERAVERAGE_TABLE_NAME;
        stmt.executeUpdate(sql);

        sql = "CREATE TABLE "+CALLERAVERAGE_TABLE_NAME+" "+
                "SELECT CallerName, AVG(CallDuration) AS Average "+
                "from "+COMPLETE_TABLE_NAME+" "+
                "GROUP BY CallerName";
        stmt.executeUpdate(sql);

        sql = "DROP TABLE IF EXISTS "+CALLERAVERAGECLASSIFICATION_TABLE_NAME;
        stmt.executeUpdate(sql);

        sql = "CREATE TABLE "+CALLERAVERAGECLASSIFICATION_TABLE_NAME+"(CallerName VARCHAR(255), Average DOUBLE, GroupId VARCHAR(255))";
        stmt.executeUpdate(sql);

        sql = "SELECT * from "+CALLERAVERAGE_TABLE_NAME;
        Statement new_stmt = conn.createStatement();
        ResultSet res = new_stmt.executeQuery(sql);

        while(res.next()){
            String Callername = res.getString("CallerName");
            Double Average = res.getDouble("Average");
            String GroupId = "";
            if(Average <= 60.0){
                GroupId = "A";
            }
            else if(Average > 60.0 && Average <= 5*60.0){
                GroupId = "B";
            }
            else if(Average > 5*60.0 && Average <= 10*60.0){
                GroupId = "C";
            }
            else if(Average > 10*60.0){
                GroupId = "D";
            }
            sql = "INSERT INTO "+CALLERAVERAGECLASSIFICATION_TABLE_NAME+
                    " values('"+Callername+"','"+
                    Average+"','"+
                    GroupId+"'"+")";
            stmt.executeUpdate(sql);
        }

        sql = "DROP TABLE IF EXISTS "+CALLERFREQUENCY_TABLE_NAME;
        stmt.executeUpdate(sql);

        sql = "CREATE TABLE "+CALLERFREQUENCY_TABLE_NAME+" "+
                "SELECT CallerName, COUNT(CallId) AS Frequency "+
                "from "+COMPLETE_TABLE_NAME+" "+
                "GROUP BY CallerName";
        stmt.executeUpdate(sql);

        sql = "DROP TABLE IF EXISTS "+CALLERCALLEEAVERAGE_TABLE_NAME;
        stmt.executeUpdate(sql);

        sql = "CREATE TABLE "+CALLERCALLEEAVERAGE_TABLE_NAME+" "+
                "SELECT CallerName, CalleeName, AVG(CallDuration) "+
                "from "+COMPLETE_TABLE_NAME+" "+
                "GROUP BY CallerName, CalleeName";
        stmt.executeUpdate(sql);

        sql = "DROP TABLE IF EXISTS "+CALLERCALLEEFREQUENCY_TABLE_NAME;
        stmt.executeUpdate(sql);

        sql = "CREATE TABLE "+CALLERCALLEEFREQUENCY_TABLE_NAME+" "+
                "SELECT  CallerName,CalleeName, COUNT(CallId)  AS Frequency "+
                "from "+COMPLETE_TABLE_NAME+" "+
                "GROUP BY CallerName, CalleeName";
        stmt.executeUpdate(sql);

        sql = "DROP TABLE IF EXISTS "+CALLERTOTALDURATION_TABLE_NAME;
        stmt.executeUpdate(sql);

        sql = "CREATE TABLE "+CALLERTOTALDURATION_TABLE_NAME+" "+
                "SELECT CallerName, SUM(CallDuration) "+
                "from "+COMPLETE_TABLE_NAME+" "+
                "GROUP BY CallerName";
        stmt.executeUpdate(sql);

        sql = "DROP TABLE IF EXISTS "+CALLERBETA_TABLE_NAME;
        stmt.executeUpdate(sql);

        sql = "CREATE TABLE "+CALLERBETA_TABLE_NAME+" "+
                "SELECT CallerName, SUM(CallDuration)/86400.0 as Beta "+
                "from "+COMPLETE_TABLE_NAME+" "+
                "GROUP BY CallerName";
        stmt.executeUpdate(sql);

        sql = "DROP TABLE IF EXISTS "+CALLERINCOMPLETEFREQUENCY_TABLE_NAME;
        stmt.executeUpdate(sql);

        sql = "CREATE TABLE "+CALLERINCOMPLETEFREQUENCY_TABLE_NAME+" "+
                "SELECT ALL CallerName, COUNT(CallId)  AS Frequency "+
                "from "+INCOMPLETE_TABLE_NAME+" "+
                "GROUP BY CallerName";
        stmt.executeUpdate(sql);

        sql = "DROP TABLE IF EXISTS "+CALLERGAMMA_TABLE_NAME;
        stmt.executeUpdate(sql);

        sql = "CREATE TABLE "+CALLERGAMMA_TABLE_NAME+" "+
                "SELECT CallerName, COUNT(CallerIp) as Gamma "+
                "from "+COMPLETE_TABLE_NAME+" "+
                "GROUP BY CallerName";

        stmt.executeUpdate(sql);

        sql = "DROP TABLE IF EXISTS TEMP";
        stmt.executeUpdate(sql);

        sql = "CREATE TABLE TEMP SELECT "+CALLERFREQUENCY_TABLE_NAME+".CallerName AS P, "+CALLERFREQUENCY_TABLE_NAME+
                ".Frequency as Q, "+CALLERINCOMPLETEFREQUENCY_TABLE_NAME+
                ".CallerName as R, "+
                CALLERINCOMPLETEFREQUENCY_TABLE_NAME+
                ".Frequency AS S FROM  "+
                CALLERFREQUENCY_TABLE_NAME+" INNER JOIN "+CALLERINCOMPLETEFREQUENCY_TABLE_NAME+
                " ON "+CALLERFREQUENCY_TABLE_NAME+".CallerName = "+CALLERINCOMPLETEFREQUENCY_TABLE_NAME+".CallerName";

        stmt.executeUpdate(sql);

        sql = "DROP TABLE IF EXISTS "+CALLERALPHA_TABLE_NAME;
        stmt.executeUpdate(sql);

        sql = "CREATE TABLE "+CALLERALPHA_TABLE_NAME+" "+
              "SELECT P as CallerName, Q/(Q+S) as Alpha FROM TEMP";
        stmt.executeUpdate(sql);

        sql = "DROP TABLE IF EXISTS TEMP";
        stmt.executeUpdate(sql);

        sql = "DROP TABLE IF EXISTS "+CALLEEFREQUENCY_TABLE_NAME;
        stmt.executeUpdate(sql);

        sql = "CREATE TABLE "+CALLEEFREQUENCY_TABLE_NAME+" "+
                "SELECT CalleeName, COUNT(CallId) AS Frequency "+
                "from "+COMPLETE_TABLE_NAME+" "+
                "GROUP BY CalleeName";
        stmt.executeUpdate(sql);

        sql = "DROP TABLE IF EXISTS TEMP";
        stmt.executeUpdate(sql);

        sql = "CREATE TABLE TEMP SELECT "+CALLERFREQUENCY_TABLE_NAME+".CallerName AS P, "+CALLERFREQUENCY_TABLE_NAME+
                ".Frequency as Q, "+CALLEEFREQUENCY_TABLE_NAME+
                ".CalleeName as R, "+
                CALLEEFREQUENCY_TABLE_NAME+
                ".Frequency AS S FROM  "+
                CALLERFREQUENCY_TABLE_NAME+" INNER JOIN "+CALLEEFREQUENCY_TABLE_NAME+
                " ON "+CALLERFREQUENCY_TABLE_NAME+".CallerName = "+CALLEEFREQUENCY_TABLE_NAME+".CalleeName";

        stmt.executeUpdate(sql);

        sql = "DROP TABLE IF EXISTS "+CALLERRHO_TABLE_NAME;
        stmt.executeUpdate(sql);

        sql = "CREATE TABLE "+CALLERRHO_TABLE_NAME+" "+
              "SELECT P as UserName, Q/S as Rho FROM TEMP";
        stmt.executeUpdate(sql);

        sql = "DROP TABLE IF EXISTS TEMP";
        stmt.executeUpdate(sql);


        /*
        ArrayList<String> users = new ArrayList<String>();
        sql = "SELEECT User from "+USERS_TABLE_NAME;
        Statement new_stmt = conn.createStatement();
        ResultSet user_list = new_stmt.executeQuery(sql);
        while(user_list.next()){
            users.add(user_list.getString("User"));
        }
        */


        stmt.close();
        conn.close();
        System.out.println("Finished");
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return ;
}
}

2 个答案:

答案 0 :(得分:0)

在这段代码中,您认为理所当然而不是result_one正在返回一些东西,也许这不是真的。

        ResultSet result_one = stmt1.executeQuery(sql);
        sql = "SELECT * from "+PACKET_TABLE_NAME+" "+
        "where Method = 'BYE' and CallId = '"+cur_id+"'";
        ResultSet result_two = stmt2.executeQuery(sql);
        if(!result_two.next()){

            //If this return false
            result_one.next();
            sql = "INSERT INTO "+INCOMPLETE_TABLE_NAME+

                    //this is going to fail 
                    " values('"+result_one.getString("CallId")+"','"+
                    result_one.getString("CallerName")+"','"+
                    result_one.getString("CallerIp")+"','"+
                    result_one.getString("CalleeName")+"','"+
                    result_one.getString("CalleeIp")+"',"+
                    "NULL"+")";
            stmt.executeUpdate(sql);
        }

答案 1 :(得分:0)

我认为这里的问题是ResultSet next()方法。

请记住,它将光标移动到数据库中结果集的下一行,如果有任何行则返回true,否则返回false。

您的代码看起来好像不期望光标移动到下一行。

例如:

if(!result_two.next()){
   -- Some code
} else {
  -- Some code
  result_two.next(); -- Whoops!
}

此外,您假设result_oneresult_two之间存在关系。仅仅因为result_two没有返回任何内容并不一定意味着result_one会!