2016-08-30 73 views
0

這是在錯誤發生不斷得到結果集關閉SQL異常

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:結果集關閉,我無法找到結果集關閉的位置或者是否存在另一個錯誤

+3

嘗試調用'rS.next()''之前rS.getString(2)'。我不認爲這是該集合被關閉的原因,但應該完成。另外,請看['try-with-resources'](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) – bradimus

+2

「你可以看到沒有現有的關閉語句」,這就是沒有什麼值得你知道的。你應該關閉你的資源:D – Kayaman

+0

xD刪除了希望我的問題會消失的關閉聲明 – Lgotti

回答

1

我沒有足夠的評論意見,所以我會在這裏回答。

你應該嘗試

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