这是发生错误的类

public class Drinks extends JFrame {

    private JPanel contentPane;
    private JButton button;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Drinks frame = new Drinks();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Drinks() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 401, 401);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JList list = new JList();
        list.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent arg0) {
            }

            private Component StringtoInt(String string) {
                // TODO Auto-generated method stub
                return null;
            }
        });
        list.setBorder(new CompoundBorder(new MatteBorder(3, 3, 3, 3, (Color) new Color(0, 0, 0)), null));
        list.setFont(new Font("Tahoma", Font.PLAIN, 14));

        JLabel lblDrink = new JLabel("Drink");
        lblDrink.setFont(new Font("Tahoma", Font.PLAIN, 18));

        JLabel lblIngredients = new JLabel("Ingredients");
        lblIngredients.setFont(new Font("Tahoma", Font.PLAIN, 18));

        button = new JButton("Back");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                MainMenu frame = new MainMenu();
                frame.setVisible(true);
                dispose();
            }
        });

        JTextPane textPane = new JTextPane();
        textPane.setBorder(new LineBorder(new Color(0, 0, 0), 3));



正如您所见,这是我认为存在的问题所在
现有的结束语


JButton btnLoad = new JButton("Load");
            btnLoad.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    try{
                        Connection connDrink = Connections.sqlConn();
                        int countDrinks = Connections.count();

                        System.out.println(countDrinks);
                        for (int j = 0; j < countDrinks; j++) {
                        String dk = "select DrinkID, Drink from Drinks where DrinkID = " + j + ";";
                        PreparedStatement pSt = connDrink.prepareStatement(dk);
                        ResultSet rS = pSt.executeQuery();
                        System.out.println(rS.getString(2));
                        int oi = Integer.parseInt(rS.getString(1).toString());
                        `//`list.add(rS.getString(2).toString(), null);
                        }

                    }catch(Exception e){
                        JOptionPane.showMessageDialog(null, e.getClass().getName() + ": " + e.getMessage());
                    }
                }
            });


总是给出java.sql.SQLException:结果集关闭,我找不到结果集关闭的位置或是否存在另一个错误

最佳答案

我没有足够的意见要发表,所以我在这里回答。

你应该试试

if(rS.first())// move the cursor to the first row, true if there's a row, false otherwise
    System.out.println(rS.getString(2));

关于java - 不断获取结果集关闭的SQL异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39235286/

10-10 19:58