1.从本地读取图片并显示在JLabel中
Image image = null;
image = ImageIO.read(new File("c://2.png")); //读入图片
label.setIcon(new ImageIcon(image)); //把图片作为icon显示在label中
2.把本地的一张图片插入到数据库中
File fImage = null;
FileInputStream isImage = null;
FileInputStream InImage=null;
fImage = new File("c://2.png");
isImage = new FileInputStream(fImage);
MScn = MScon.getCon();
PreparedStatement pst = null;
String sql = "insert into dbo.CHAT_DET values ('1','01','A','B',NULL,?)";
pst = MScn.prepareStatement(sql);
pst.setBinaryStream(1, isImage, (int) (fImage.length()));
pst.executeUpdate());
3.从数据库中读取图片并显示在JLable中
String sql = "select * from dbo.CHAT_DET";
pst=MScn.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
if (rs.next()) {
InputStream long_out=rs.getBinaryStream("IMAGE");
if(long_out!=null){
try {
image = ImageIO.read(long_out);
} catch (IOException ex) {
Logger.getLogger(NewJFrame5.class.getName()).log(Level.SEVERE, null, ex);
}
label.setIcon(new ImageIcon(image)); //把图片作为icon显示在label中
}
}