如何在java中保持ResultSet打开?

Din*_*aru 0 java swing resultset

我正在传递一个创建JTable的java文件的值.

ResultSet res = np.InvestmentByInvestType(IType);
 String tablename = "Investment By Invest Type";
 int customAmt = np.showCustomizeInvestAmount1(IType);
 CommonTable ct = new CommonTable();
 ct.CommonSearchTable(res, customAmt,tablename);
Run Code Online (Sandbox Code Playgroud)

public void CommonSearchTable( final ResultSet res, int totally, final String tablename) throws SQLException 
{

        JButton exportTable= new JButton ("Export");

        ResultSetMetaData metaData = res.getMetaData();
        // names of columns
        Vector<String> columnNames = new Vector<String>();
        int columnCount = metaData.getColumnCount();

        for (int column = 1; column <= columnCount; column++) 
        {
            columnNames.add(metaData.getColumnName(column));
        }
        // data of the table
        Vector<Vector<String>> data = new Vector<Vector<String>>();
        while (res.next()) 
        {
            Vector<String> vector = new Vector<String>();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) 
            {
                vector.add(res.getString(columnIndex));                     
            }
            data.add(vector);
        }

        model1 = new DefaultTableModel(data, columnNames);
        JTable table = new JTable(model1);
        int rows = table.getRowCount();
        Sorter = new TableRowSorter<DefaultTableModel> (model1);
        table.setRowSorter(Sorter);
        showSearchPages(30, 1);
        table.setModel(model1);
        String showTotal = "Total Amount : Rs."+totally+"/-"; 
        JPanel footer = new JPanel();
        JLabel show = new JLabel(showTotal);
        box1.setBounds(10,30,800,30);           
        show.setBounds(10, 60, 100, 30);
        show.setFont(new Font("Tahoma",Font.BOLD,16));            
        footer.add(box1);
        footer.add(show);
        footer.setPreferredSize(new Dimension(800,100));   
        JPanel holdingPanel = new JPanel(null);  
        JScrollPane sp = new JScrollPane(table); 
        JButton print = new JButton ("Print");
        print.setBounds(10,10,100,30);
        exportTable.setBounds(120,10,100,30);
        sp.setBounds(10,50,780,580);
        holdingPanel.add(print);
        holdingPanel.add(exportTable);
        holdingPanel.add(sp);  
        JFrame f = new JFrame("Search Results");  
        f.getContentPane().add(holdingPanel,BorderLayout.CENTER);  
        f.getContentPane().add(sp.getVerticalScrollBar(),BorderLayout.EAST);  
        f.getContentPane().add(footer,BorderLayout.SOUTH);
        f.setPreferredSize(new Dimension(850,680));
        f.pack();  
        f.setLocationRelativeTo(null);  
        f.dispose();  
        f.setResizable(false);
        f.setIconImage(img.getImage());
        f.setVisible(true);

        exportTable.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent aev)
            {
                try 
                {                         
                     ExportFile ef = new ExportFile();
                     ef.WriteFile(res, tablename);                        
                } 
                catch (SQLException | IOException ex) 
                {
                    Logger.getLogger(CommonTable.class.getName()).log(Level.SEVERE, null, ex);
                }
            }                                
        });

        print.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
               PrinterJob printJob = PrinterJob.getPrinterJob();
               if (printJob.printDialog())
                    try 
                    { 
                      printJob.print();
                    } 
                    catch(PrinterException pe) 
                    {
                    }
            }
        });
}
Run Code Online (Sandbox Code Playgroud)

ResultSet res = np.InvestmentByInvestType(IType);
 String tablename = "Investment By Invest Type";
 int customAmt = np.showCustomizeInvestAmount1(IType);
 CommonTable ct = new CommonTable();
 ct.CommonSearchTable(res, customAmt,tablename);
Run Code Online (Sandbox Code Playgroud)

请告诉我方式.

小智 6

正如您可以在Resultset 的文档中阅读:

当生成它的Statement对象关闭,重新执行或用于从多个结果序列中检索下一个结果时,ResultSet对象将自动关闭.

这意味着在关闭数据库连接之前,必须将结果数据复制到另一个数据结构(如列表,映射,适合您的需要).