我在创建用于编辑 BLOB 图像的方法时遇到问题。您可以在下面找到我的代码(所有代码均已缩短);
String edit=txt_gid.getText();
**byte[] e = icon_image1;**
String sql="update GamesCollector set FrontCover='"+e+"'where GameId='"+edit+"' ";
ps=conn.prepareStatement(sql);
ps.execute();
JOptionPane.showMessageDialog(null, "Data Edited");
Browse.UpdateTable();
这就是我在 JLabel(Swing 组件)上显示图像的方式;
public static ImageIcon pic1=null;
String sql="select * from GamesCollector where Title=?";
ps=conn.prepareStatement(sql);
ps.setString(1, txt_search.getText().toUpperCase().trim());
rs=ps.executeQuery();
if(rs.next()){
byte[] datapic = rs.getBytes("FrontCover");
pic1 = new ImageIcon(datapic);
front.setIcon(pic1);}
这是导致问题的代码;
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){}
}
}
编辑后,该方法将不再检索任何数据。
数据在我编辑时保存。仅当我在图形界面上显示它时才会出现此问题。除了显示空白区域的图像外,将显示所有数据。
如何解决我遇到的问题?