2014-09-21 80 views
1

基本上我有保存图像我把一个JLabel在我的DB(我有一个BLOB变种有)保存在数据库映像

JLabel LImg = new JLabel(); ImageIcon img = new ImageIcon("**filepath**"); LImg.setIcon(img); stmt.executeUpdate("INSERT INTO CLIENT(ID, IMAGE) VALUES (1,"+LImg.getIcon()+")");

运行一些测试后,我发现了getIcon方法返回文件路径,这解释了此错误

GDS异常。 335544569.动态SQL错误 SQL错误代码= -104 令牌未知 - 第1行,列132

请在你的答案especific(我在JAVA新)

回答

1

您不能将图像本身粘贴到SQL语句中。您需要使用参数化准备语句,并将插入的值设置为字节数组:

BufferedImage bi = (BufferedImage)LImg.getImage(); 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
ImageIO.write(bi, type, baos); 
byte[] dataToWrite = baos.toByteArray(); 
PreparedStatement stmt = con.prepareStatement("INSERT INTO CLIENT(ID, IMAGE) VALUES (1,?)"); 
stmt.setBlob(1, dataToWrite); 
stmt.executeUpdate();