gpt4 book ai didi

java - 不断获取结果集关闭sql异常

转载 作者:行者123 更新时间:2023-12-01 09:33:06 26 4
gpt4 key购买 nike

这是发生错误的类

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

Here is where i believe the problem to be as you can see there are no existing close statements

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com