简体   繁体   中英

How to append data in resultset?

I want to append data of two tables in a resultset. I have tried the below code but not getting the desired output only the first resultset data ie the first table data

   ResultSet rs=null;
            String sql_query="select * from exception_main;select * from m_roles"
            String query1=sql_query.toUpperCase();
                        String[] results=query1.split(";");
                for(int i=0;i<results.length;i++)
                    {
                        if(results[i].startsWith("SELECT"))
                        {
                            System.out.println("Inside select"+ results[i]);
                        ps = conn1.prepareStatement(results[i].toString());
                        rs = ps.executeQuery();

    ...
                   //writing to csv file
                    CSVWriter writer = new CSVWriter(new FileWriter(csv_file_path + csv_file_name), ',',CSVWriter.NO_QUOTE_CHARACTER);
                    System.out.println("Count..." + rs.getRow());
                    writer.writeAll(rs, true);
                    System.out.println("Count...2::::" + rs.getRow());
                    writer.close();
                     while(rs.next()){

                            rs.deleteRow();

                          }
                     System.out.println("Count...3:::::::" + rs1.getRow());
}
}

I am getting Count as 0 in all three places Guide me please.

You should pull all the data from each ResultSet before executing a new query using the same connection.

Under the covers, the ResultSet object is making using of the connection/statement in order to pull data from the db. When you execute a new query using that connection, the existing query info will be trashed.

Hence you should iterate through each ResultSet and pull the data (into a custom object, most likely) before executing the next query.

opened the file before the for loop and closed it afterwards, rather than opening and closing it every time round the loop.

ResultSet rs=null;
                String sql_query="select * from exception_main;select * from m_roles"
                String query1=sql_query.toUpperCase();
                            String[] results=query1.split(";");
        CSVWriter writer = new CSVWriter(new FileWriter(csv_file_path + csv_file_name), ',',CSVWriter.NO_QUOTE_CHARACTER);
                    for(int i=0;i<results.length;i++)
                        {
                            if(results[i].startsWith("SELECT"))
                            {
                                System.out.println("Inside select"+ results[i]);
                            ps = conn1.prepareStatement(results[i].toString());
                            rs = ps.executeQuery();




        ...
                       //writing to csv file

                        System.out.println("Count..." + rs.getRow());
                        writer.writeAll(rs, true);
                        System.out.println("Count...2::::" + rs.getRow());
                                             while(rs.next()){

                                rs.deleteRow();

                              }
                         System.out.println("Count...3:::::::" + rs1.getRow());
    }
    }
    writer.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM