如何在java中保持ResultSet打开?

 咖啡的因_411 发布于 2023-02-09 14:28

我正在传递一个创建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);

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 columnNames = new Vector();
        int columnCount = metaData.getColumnCount();

        for (int column = 1; column <= columnCount; column++) 
        {
            columnNames.add(metaData.getColumnName(column));
        }
        // data of the table
        Vector> data = new Vector>();
        while (res.next()) 
        {
            Vector vector = new Vector();
            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 (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) 
                    {
                    }
            }
        });
}

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);

请告诉我方式.

1 个回答
  • 正如您可以在Resultset 的文档中阅读:

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

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

    2023-02-09 14:31 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有