在jtable中检索图像

时间:2015-06-10 04:31:43

标签: java swing jdbc jtable

我在java中编写了一个程序来从表中检索图像和文本数据。一切都是正确的,只有当我尝试从表格中检索图像(Blob)时,我才会得到一个用尽的结果集错误。我试过谷歌它,但解释是太浓缩了。谁能帮我这个?我将非常感谢你的解释。

这是代码

       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 个答案:

答案 0 :(得分:2)

您没有在ResultSet上检查next的返回值。

请考虑将代码更改为

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

修改 关于代码更改,请检查您的表是否为空。如果表是空的,则不会返回值。

在迭代后尝试访问结果集或未检查结果集是否返回数据时,会出现错误Exhausted Resultset设置

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