Retrieving image in a jtable

74 views Asked by At

I wrote a program in java to retrieve images and text data from a table. Everything is right, only when i try to retrieve an image(Blob) from the table, i get an exhausted result set error.I tried google it, but the explanation was fa too condensed. Can anyone help me with this? I will appreciate a nice explanation.

Here is the code

       try {
  Class.forName(classforname);
  Connection con = DriverManager.getConnection(Connectionurl, username, password);
  String sql = "Select * from teacherdata where teachername='" + tf1.getText() + "'";
  PreparedStatement ps = con.prepareStatement(sql);
  ResultSet rs = ps.executeQuery(sql);
  rs.next();
  String thename = rs.getString("teachername");
  String sub1, sub2, sub3, sub4;
  sub1 = rs.getString("sub1");
  sub2 = rs.getString("sub2");
  sub3 = rs.getString("sub3");
  sub4 = rs.getString("sub4");
  String sql2 = "select tid,tpic from teacherimages where teachername='" + tf1.getText() + "'";
  PreparedStatement ps2 = con.prepareStatement(sql2);
  ResultSet rs2 = ps2.executeQuery(sql2);
  rs2.next();
  String Tid = rs2.getString("tid");
  Blob b = rs.getBlob("tpic");
  byte barr
   [] = new byte[(int) b.length()]; //an array is created but contains no  data
  barr = b.getBytes(3, (int) b.length());
  Image im = jInternalFrame1.getToolkit().createImage(barr);
  ImageIcon icon = new ImageIcon(im);
  JLabel label = new JLabel(icon);
  Object[] row = {
   thename,
   sub1,
   sub2,
   sub3,
   sub4,
   b,
   Tid,
   icon
  };
  DefaultTableModel model = (DefaultTableModel) jTable1.getModel();

  model.addRow(row);

  jTable1.setVisible(true);
 } catch (SQLException e) {
  e.printStackTrace();
 } catch (ClassNotFoundException ex) {
  Logger.getLogger(SearchBox.class.getName()).log(Level.SEVERE, null, ex);
 }
1

There are 1 answers

3
Kenneth Clark On BEST ANSWER

You are not checking the return value of next on the ResultSet.

Please consider changing your code to

if(result.next()){
    //  the logic 
}

EDIT Further on the code change , please check that your tables are not empty. If your tables are empty you will not return values.

The error Exhausted Resultset set occurs when you attempt to access the result set after iterating or not checking if the result set returns data

while (resultSet.next()) {
  //result set logic
}
// 
resultset.getString[1] //<- will throw error Exhausted Resultset