多谢大家,我已经弄好了,就是用的楼上那为仁兄的方法!太感谢了 !
表是:
id int
imageS image
[code]
public void save_actionPerformed(ActionEvent e) { //保存到数据库中
try{
File tmpFile = new File(path);
FileInputStream in = new FileInputStream(tmpFile);
BufferedInputStream inFile = new BufferedInputStream(in);
//获取图片大小
int a = Integer.parseInt(String.valueOf(tmpFile.length()));
System.out.println(a / 1000 + "KB");
byte len[] = new byte[a];
//读字节
while (inFile.read(len) != -1) {
}
int pos=1;
//设置id号码
String sql = "select count(*) as total from product";
pst = con.prepareStatement(sql);
rs = pst.executeQuery();
if (rs.next()) {
pos=rs.getInt(1);
pos++;
}
//插入记录
sql = "insert into product values(?,?)";
pst = con.prepareStatement(sql);
pst.setInt(1, pos);
pst.setBytes(2, len);
pst.executeUpdate();
JOptionPane.showMessageDialog(this, "插入记录完毕!", "消息",
JOptionPane.INFORMATION_MESSAGE);
}
catch(IOException ex){
System.out.println("I/O错误!"+ex.getMessage());
ex.printStackTrace();
}
catch(Exception ex){
System.out.println("插入记录错误!"+ex.getMessage());
ex.printStackTrace();
}
}
[/code]
我使用了一个下拉彩旦
[code]
public void item_itemStateChanged(ItemEvent e) { //提取图片
if(item.getSelectedItem().toString().equals("------ID------")){
}
else{
int tmpId = Integer.parseInt(item.getSelectedItem().toString());
try {
String sql = "select * from product where id=?";
pst = con.prepareStatement(sql);
pst.setInt(1, tmpId);
rs = pst.executeQuery();
if (rs.next()) {
int id = rs.getInt("id");
//提取图片字节
byte src[] = rs.getBytes("imageS");
//还原
ImageIcon i = new ImageIcon(src);
ima.setIcon(i);
}
}
catch (SQLException ex) {
System.out.println("从数据库读取图片错误!");
System.out.println(ex.getMessage());
}
}
}
[/code]
posted on 2006-10-28 10:13
无声 阅读(278)
评论(0) 编辑 收藏 所属分类:
职场生活