Java 如何编辑/更新BLOB图像(PNG)
我创建的用于编辑水滴图像的方法有问题。你可以在下面找到我的代码(所有代码都被缩短) 这就是我在JLabel(Swing组件)上显示图像的方式 这是导致问题的代码Java 如何编辑/更新BLOB图像(PNG),java,sqlite,blob,Java,Sqlite,Blob,我创建的用于编辑水滴图像的方法有问题。你可以在下面找到我的代码(所有代码都被缩短) 这就是我在JLabel(Swing组件)上显示图像的方式 这是导致问题的代码 static void Edit(){ try{ String edit=txt_gid.getText(); String edit1=txt_title.getText().toUpperCase().trim(); String edit2=cmb_platform.g
static void Edit(){
try{
String edit=txt_gid.getText();
String edit1=txt_title.getText().toUpperCase().trim();
String edit2=cmb_platform.getSelectedItem().toString();
if(edit2==null){edit2="";}
String edit3=cmb_format.getSelectedItem().toString();
if(edit3==null){edit3="";}
String edit4=list_genres.getSelectedValue().toString();
if(edit4==null){edit4="";}
String edit5=((JTextField)pck_releasedate.getDateEditor().getUiComponent()).getText();
String edit6=txt_desc.getText().toUpperCase().trim();
String edit7=txt_developer.getText().toUpperCase().trim();
String edit8=txt_publisher.getText().toUpperCase().trim();
String edit9=txt_series.getText().toUpperCase().trim();
String edit10=txt_barcode.getText().toUpperCase().trim();
String edit11=collection;
String edit12=cmb_esrb.getSelectedItem().toString();
if(edit12==null){edit12="";}
String edit13=cmb_pegi.getSelectedItem().toString();
if(edit13==null){edit13="";}
String edit14 = Integer.toString(rating_slider.getValue());
**byte[] e = icon_image1;**
String sql="update GamesCollector set Title=='"+edit1+"',Platform='"+edit2+"',Format='"+edit3+"',Genres='"+edit4+"',ReleaseDate='"+edit5+"',Description='"+edit6+"',Developer='"+edit7+"',Publisher='"+edit8+"',Series='"+edit9+"',Barcode='"+edit10+"',Collection='"+edit11+"',ESRB='"+edit12+"',PEGI='"+edit13+"',Rating='"+edit14+"',FrontCover='"+e+"' where GameId='"+edit+"' ";
ps=conn.prepareStatement(sql);
ps.execute();
JOptionPane.showMessageDialog(null, "Data Edited");
Browse.UpdateTable();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
finally {
try{
rs.close();
ps.close();
}
catch(Exception e){}
}
}
}
此方法用于获取路径并将其转换为ByteArray
protected static void AttachImage(){
JFileChooser chooser= new JFileChooser("D:\\Downloads\\Software Development\\GameCollector\\Images\\Game");
chooser.showOpenDialog(null);
File s=chooser.getSelectedFile();
filename = s.getAbsolutePath();
try{
File image = new File(filename);
FileInputStream fis = new FileInputStream(image);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bug = new byte[1024];
for(int readNum; (readNum=fis.read(bug)) !=-1;){
baos.write(bug,0,readNum);
}
icon_image1=baos.toByteArray();
icon_image2=baos.toByteArray();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}
此方法用于保存文件
static void Save(){
try{
String sql="insert into GamesCollector (GameId,Title,Platform,Format,Genres,ReleaseDate,Description,Developer,Publisher,Series,Barcode,Collection,ESRB,PEGI,Rating,FrontCover,BackCover) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
ps=conn.prepareStatement(sql);
ps.setString(1, txt_gid.getText().toUpperCase().trim());
ps.setString(2, txt_title.getText().toUpperCase().trim());
String platform=cmb_platform.getSelectedItem().toString();
if(platform==null){platform="";}
ps.setString(3, platform);
String format=cmb_format.getSelectedItem().toString();
if(format==null){format="";}
ps.setString(4, format);
String genres=list_genres.getSelectedValue().toString();
if(genres==null){genres="";}
ps.setString(5, genres);
ps.setString(6, ((JTextField)pck_releasedate.getDateEditor().getUiComponent()).getText());
ps.setString(7, txt_desc.getText().toUpperCase().trim());
ps.setString(8, txt_developer.getText().toUpperCase().trim());
ps.setString(9, txt_publisher.getText().toUpperCase().trim());
ps.setString(10, txt_series.getText().toUpperCase().trim());
ps.setString(11, txt_barcode.getText().toUpperCase().trim());
ps.setString(12, collection);
String esrb=cmb_esrb.getSelectedItem().toString();
if(esrb==null){esrb="";}
ps.setString(13, esrb);
String pegi=cmb_pegi.getSelectedItem().toString();
if(pegi==null){pegi="";}
ps.setString(14, pegi);
int rating = rating_slider.getValue();
ps.setString(15, Integer.toString(rating));
ps.setBytes(16, icon_image1);
ps.setBytes(17, icon_image2);
ps.execute();
JOptionPane.showMessageDialog(null, "Data Saved");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
finally {
try{
rs.close();
ps.close();
}
catch(Exception e){}
}
}
当我检索数据时,一切正常
此方法用于编辑数据
static void Edit(){
try{
String edit=txt_gid.getText();
String edit1=txt_title.getText().toUpperCase().trim();
String edit2=cmb_platform.getSelectedItem().toString();
if(edit2==null){edit2="";}
String edit3=cmb_format.getSelectedItem().toString();
if(edit3==null){edit3="";}
String edit4=list_genres.getSelectedValue().toString();
if(edit4==null){edit4="";}
String edit5=((JTextField)pck_releasedate.getDateEditor().getUiComponent()).getText();
String edit6=txt_desc.getText().toUpperCase().trim();
String edit7=txt_developer.getText().toUpperCase().trim();
String edit8=txt_publisher.getText().toUpperCase().trim();
String edit9=txt_series.getText().toUpperCase().trim();
String edit10=txt_barcode.getText().toUpperCase().trim();
String edit11=collection;
String edit12=cmb_esrb.getSelectedItem().toString();
if(edit12==null){edit12="";}
String edit13=cmb_pegi.getSelectedItem().toString();
if(edit13==null){edit13="";}
String edit14 = Integer.toString(rating_slider.getValue());
byte[] e = icon_image1;
String sql="update GamesCollector set Title=='"+edit1+"',Platform='"+edit2+"',Format='"+edit3+"',Genres='"+edit4+"',ReleaseDate='"+edit5+"',Description='"+edit6+"',Developer='"+edit7+"',Publisher='"+edit8+"',Series='"+edit9+"',Barcode='"+edit10+"',Collection='"+edit11+"',ESRB='"+edit12+"',PEGI='"+edit13+"',Rating='"+edit14+"',FrontCover='"+icon_image1+"' where GameId='"+edit+"' ";
ps=conn.prepareStatement(sql);
ps.execute();
JOptionPane.showMessageDialog(null, "Data Edited");
Browse.UpdateTable();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
finally {
try{
rs.close();
ps.close();
}
catch(Exception e){}
}
}
编辑后,该方法将不再检索任何数据
数据在我编辑时保存。只有当我在图形界面上显示它时,问题才会出现。除显示空白区域的图像外,所有数据都将显示
如何解决我遇到的问题?您正在尝试将图像放入SQL命令字符串中:
String sql="update ... set ... FrontCover='"+icon_image1+"' ...
这不适用于二进制数据
为确保二进制数据正确进入数据库,请使用参数,如
保存方法。能否发布重现问题的/minimal/代码?我不想编译所有内容只是为了查看是否有错误。假设如果我们要保存,只需使用ps.setBytes(16,icon_image1);但在编辑时,当我调回图标_image1时,它会显示我和空图像。一个简单的示例将非常有用。另请参阅首先验证重新加载的图像是否与您保存的相同字节(即未损坏)。也许你可以告诉我们你是如何显示图像的,因为你说这是不起作用的…你能给我举个例子吗。。。。如果我使用ps.setbytes(12,icon\u image1),它会给我一个错误空指针例外。你可以给我举个例子,因为ps.setbytes只是用于保存方法,我真的不知道如何在编辑方法ps.setbytes(12,icon\u image1)中编码它代码>应该是正确的。ps
在这一点上初始化了吗?是的,它已经初始化了…因此,如果我在sql语句@CL中包含这个ps,我需要更改什么。好的,您需要显示更多的代码。严格地说,这是一个新问题。
static void Edit(){
try{
String edit=txt_gid.getText();
String edit1=txt_title.getText().toUpperCase().trim();
String edit2=cmb_platform.getSelectedItem().toString();
if(edit2==null){edit2="";}
String edit3=cmb_format.getSelectedItem().toString();
if(edit3==null){edit3="";}
String edit4=list_genres.getSelectedValue().toString();
if(edit4==null){edit4="";}
String edit5=((JTextField)pck_releasedate.getDateEditor().getUiComponent()).getText();
String edit6=txt_desc.getText().toUpperCase().trim();
String edit7=txt_developer.getText().toUpperCase().trim();
String edit8=txt_publisher.getText().toUpperCase().trim();
String edit9=txt_series.getText().toUpperCase().trim();
String edit10=txt_barcode.getText().toUpperCase().trim();
String edit11=collection;
String edit12=cmb_esrb.getSelectedItem().toString();
if(edit12==null){edit12="";}
String edit13=cmb_pegi.getSelectedItem().toString();
if(edit13==null){edit13="";}
String edit14 = Integer.toString(rating_slider.getValue());
byte[] e = icon_image1;
String sql="update GamesCollector set Title=='"+edit1+"',Platform='"+edit2+"',Format='"+edit3+"',Genres='"+edit4+"',ReleaseDate='"+edit5+"',Description='"+edit6+"',Developer='"+edit7+"',Publisher='"+edit8+"',Series='"+edit9+"',Barcode='"+edit10+"',Collection='"+edit11+"',ESRB='"+edit12+"',PEGI='"+edit13+"',Rating='"+edit14+"',FrontCover='"+icon_image1+"' where GameId='"+edit+"' ";
ps=conn.prepareStatement(sql);
ps.execute();
JOptionPane.showMessageDialog(null, "Data Edited");
Browse.UpdateTable();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
finally {
try{
rs.close();
ps.close();
}
catch(Exception e){}
}
}
String sql="update ... set ... FrontCover='"+icon_image1+"' ...