如何在SQL Server 2008中以图像格式存储图像并在jlabel中检索图像

时间:2018-09-06 05:51:44

标签: java sql-server-2008 jtable jlabel

我正在使用此代码将图像插入数据库中,并且将图片插入数据库中,我不知道采用哪种格式,但是它将显示一些数据,例如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;
}

0 个答案:

没有答案