我一直在尝试构建一个GUI,它从MySQL数据库中获取图像并在每次点击下一个按钮后显示一个,它工作正常,除了它跳过一个图像并在其后显示一个,我检查了id同一行的id也被跳过,所以我认为它是一个问题mysql连接器。
方法示例:
public Pair<Integer,Image> image2()throws SQLException
{
int id;
System.out.println("I am in Image");
try {
System.out.println(rs);
boolean anyResults = false;
if (rs.next())
{
anyResults = true;
Blob blob = rs.getBlob("image");
id = rs.getInt("id");
System.out.println(id);
System.out.println(blob);
System.out.println(blob.length());
InputStream in = blob.getBinaryStream(1, blob.length());
System.out.println(rs.next());
System.out.println(in);
BufferedImage image = ImageIO.read(in);
System.out.println(image);
Image image1 = SwingFXUtils.toFXImage(image,null);
return new Pair<>(id, image1);
}
else if (!anyResults)
{
JOptionPane.showMessageDialog(null, "Not Found");
}
System.out.println("reached here");
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
nextButton的例子:
public void NextButtomClicked() throws SQLException
{
// this is what i used before => Image image1 = sql.image2();
//Pair<Integer, Image> image1 = sql.image2();
//Pair<Integer, Image> pair = sql.image2();
Pair<Integer, Image> pair = sql.image2();
Image image = pair.getValue();
list.add(pair.getKey());
this.imageView.setImage(image);
}
PS。 if(rs.Next)的工作方式与while(rs.Next)相同
答案 0 :(得分:0)
System.out.println(rs.next());跳行
尝试:
public Pair<Integer,Image> image2()throws SQLException
{
int id;
System.out.println("I am in Image");
try {
System.out.println(rs);
boolean anyResults = false;
boolean hasNext = rs.next();
if (hasNext )
{
anyResults = true;
Blob blob = rs.getBlob("image");
id = rs.getInt("id");
System.out.println(id);
System.out.println(blob);
System.out.println(blob.length());
InputStream in = blob.getBinaryStream(1, blob.length());
System.out.println(hasNext );
System.out.println(in);
BufferedImage image = ImageIO.read(in);
System.out.println(image);
Image image1 = SwingFXUtils.toFXImage(image,null);
return new Pair<>(id, image1);
}
else if (!anyResults)
{
JOptionPane.showMessageDialog(null, "Not Found");
}
System.out.println("reached here");
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}