我正在使用此代码将图像插入数据库中,并且将图片插入数据库中,我不知道采用哪种格式,但是它将显示一些数据,例如0x122323435456576867,并且不会检索图像 尝试{
InputStream pic = new FileInputStream(new File(s));
Connection myConn = DriverManager.getConnection(url);
Statement statement = myConn.createStatement();
String sqlIsnert = ("INSERT INTO STUDENT VALUES ('" + admissno + "','" + name + "','" + religion
+ "',+ '" + admissdate + "','" + year + "','" + batch + "','" + sec + "','" + classs
+ "','" + category + "','" + GR + "','" + GN + "','" + GP + "','" + GO + "','null','"+ pic +"')");
System.out.println(sqlIsnert);
statement.executeUpdate(sqlIsnert);
JOptionPane.showMessageDialog(STD_REG.this, "Record Inserted", "ALERT",
JOptionPane.INFORMATION_MESSAGE);
private void open()
//此函数用于打开jfilechooser选择图像,然后将其绝对路径保存在Global变量s中,然后使用ImageIcon ResizeImage函数调整该图像的大小。 {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
FileNameExtensionFilter filter = new FileNameExtensionFilter("*.IMAGE", "jpg","gif","png");
fileChooser.addChoosableFileFilter(filter);
int result = fileChooser.showSaveDialog(null);
if(result == JFileChooser.APPROVE_OPTION)
{
File selectedFile = fileChooser.getSelectedFile();
String path = selectedFile.getAbsolutePath();
lbl.setIcon(ResizeImage(path));
s = path;
}
else if(result == JFileChooser.CANCEL_OPTION)
{
System.out.println("No Data");
}
}
public ImageIcon ResizeImage(String imgPath){
ImageIcon MyImage = new ImageIcon(imgPath);
Image img = MyImage.getImage();
Image newImage = img.getScaledInstance(lbl.getWidth(), lbl.getHeight(),Image.SCALE_SMOOTH);
ImageIcon image = new ImageIcon(newImage);
return image;
}