2016-04-04 72 views
0
try { 
    Statement stmt = conn.createStatement(); 
    String sql = "CREATE OR REPLACE FUNCTION search_admin(p_username IN varchar2) \n"+ 
       "RETURN SYS_REFCURSOR \n" + 
       "AS \n" + 
       "my_cursor SYS_REFCURSOR; \n" + 
       "BEGIN \n" + 
       "OPEN my_cursor FOR SELECT * FROM login where username=p_username; \n" + 
       "RETURN my_cursor; \n" + 
       "END; \n"; 

    stmt.execute(sql); 

    CallableStatement cstmt = conn.prepareCall("BEGIN " + "? :=search_admin(?); " + "END; "); 
    cstmt.registerOutParameter(1, OracleTypes.CURSOR); 
    cstmt.setString(2, UserText3.getText()); 
    cstmt.execute(); 

    rs = (ResultSet) cstmt.getObject(1); 

    if(!rs.isBeforeFirst()) { 
    JOptionPane.showMessageDialog(null, "Invalid Username"); 
    } 
    else { 
    DeleteTable.setModel(DbUtils.resultSetToTableModel(rs)); 
    DeleteTable.setEnabled(false); 
    } 

    while(rs.next()) { 
    NameLabel.setText(rs.getString("name")); 
    UsernameLabel.setText(rs.getString("username")); 
    PasswordLabel.setText(rs.getString("password")); 
    } 
} catch(Exception e) { 
    JOptionPane.showMessageDialog(null, e); 
} 

我想顯示在jlabels用戶名,姓名,密碼,但它沒有顯示...我在數據庫中名爲search_admin存儲功能。當我執行程序沒有顯示錯誤,但jlabels不顯示resultset數據。的JLabel沒有顯示的結果中的JLabel

+0

下面這行可能會耗盡ResultSet:'DbUtils.resultSetToTableModel(rs)',所以你沒有什麼可讀的了。 – Berger

+0

那麼解決方案是什麼? –

回答

0

此行似乎耗盡的ResultSet:

DbUtils.resultSetToTableModel(rs) 

假設它返回的TableModel一個實例,你可以瀏覽它的內容通過這種方式,而不是試圖再次讀取ResultSet

 for(int i =0;i<model.getRowCount();i++){ 

      for(int j=0;j<model.getColumnCount();j++){ 

       System.out.println(model.getValueAt(i, j)); 
      } 
     }