如何将图像从MySQL检索到JavaSwing?
我想从MySQL中检索图像,并使用JavaSwing将其加载到如何将图像从MySQL检索到JavaSwing?,java,mysql,image,swing,Java,Mysql,Image,Swing,我想从MySQL中检索图像,并使用JavaSwing将其加载到JLabel 但我无法得到这份工作 下面是我的代码和我得到的错误: try { Class.forName("com.mysql.jdbc.Driver"); // MySQL database connection Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/image_db", "root", "root")
JLabel
但我无法得到这份工作
下面是我的代码和我得到的错误:
try {
Class.forName("com.mysql.jdbc.Driver"); // MySQL database connection
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/image_db", "root", "root");
PreparedStatement pst = conn.prepareStatement("Select * from image_tbl where id='"+jTextField1.getText()+"'");
ResultSet rs = pst.executeQuery();
byte b[] = null;
while(rs.next())
{
b= rs.getBytes(2);
}
jLabel1.setIcon(new ImageIcon (Toolkit.getDefaultToolkit().createImage(b)));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Wrong Data Detected! Please provide correct data");
}
例外情况:
sun.awt.image.ImageFormatException: JPEG datastream contains no image
at sun.awt.image.JPEGImageDecoder.readImage(Native Method)
at sun.awt.image.JPEGImageDecoder.produceImage(JPEGImageDecoder.java:141)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:269)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:205)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:169)
Premature end of JPEG file
这是一个在mysql blob中序列化图像(图标)的简单示例 创建表t1(id整数主键自动递增,img longblob不为空) 这是一个测试应用程序。从数据库中读取图像是使用等于1的固定图像ID执行的,只是为了演示。在实际实现中,压缩数据以节省空间是很好的。当然,对记录图像(对象)的提取应限制在绝对最小值
public class MainFrame extends JFrame {
private JLabel imageLabel = new JLabel();
private JButton loadImageFromFileButton = new JButton("Load image from file");
private JButton storeImageIntoDBButton = new JButton("Store image into DB");
private JButton loadImageFromDBButton = new JButton("Load image from DB");
public MainFrame() throws HeadlessException {
super("JDBC Test");
createGUI();
}
private void createGUI() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new BorderLayout(5, 20));
imageLabel.setPreferredSize(new Dimension(200, 200));
imageLabel.setHorizontalAlignment(JLabel.CENTER);
imageLabel.setVerticalAlignment(JLabel.CENTER);
loadImageFromFileButton.addActionListener(this::loadImageFromFile);
loadImageFromDBButton.addActionListener(this::loadImageFromDB);
storeImageIntoDBButton.addActionListener(this::storeImageIntoDB);
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
buttonsPanel.add(loadImageFromFileButton);
buttonsPanel.add(Box.createHorizontalStrut(25));
buttonsPanel.add(loadImageFromDBButton);
buttonsPanel.add(Box.createHorizontalStrut(5));
buttonsPanel.add(storeImageIntoDBButton);
add(imageLabel, BorderLayout.CENTER);
add(buttonsPanel, BorderLayout.PAGE_END);
pack();
setLocationRelativeTo(null);
}
private void loadImageFromFile(ActionEvent event) {
JFileChooser chooser = new JFileChooser();
if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
ImageIcon imageIcon = new ImageIcon(chooser.getSelectedFile().getAbsolutePath());
imageLabel.setIcon(imageIcon);
}
}
private void storeImageIntoDB(ActionEvent event) {
try {
Database db = new Database();
db.storeIcon(imageLabel.getIcon());
}
catch (SQLException | IOException e) {
e.printStackTrace();
}
}
private void loadImageFromDB(ActionEvent event) {
try {
Database db = new Database();
Icon icon = db.loadIcon(1L);
imageLabel.setIcon(icon);
}
catch (SQLException | IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new MainFrame().setVisible(true));
}
}
这是一个在mysql blob中序列化图像(图标)的简单示例 创建表t1(id整数主键自动递增,img longblob不为空) 这是一个测试应用程序。从数据库中读取图像是使用等于1的固定图像ID执行的,只是为了演示。在实际实现中,压缩数据以节省空间是很好的。当然,对记录图像(对象)的提取应限制在绝对最小值
public class MainFrame extends JFrame {
private JLabel imageLabel = new JLabel();
private JButton loadImageFromFileButton = new JButton("Load image from file");
private JButton storeImageIntoDBButton = new JButton("Store image into DB");
private JButton loadImageFromDBButton = new JButton("Load image from DB");
public MainFrame() throws HeadlessException {
super("JDBC Test");
createGUI();
}
private void createGUI() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new BorderLayout(5, 20));
imageLabel.setPreferredSize(new Dimension(200, 200));
imageLabel.setHorizontalAlignment(JLabel.CENTER);
imageLabel.setVerticalAlignment(JLabel.CENTER);
loadImageFromFileButton.addActionListener(this::loadImageFromFile);
loadImageFromDBButton.addActionListener(this::loadImageFromDB);
storeImageIntoDBButton.addActionListener(this::storeImageIntoDB);
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
buttonsPanel.add(loadImageFromFileButton);
buttonsPanel.add(Box.createHorizontalStrut(25));
buttonsPanel.add(loadImageFromDBButton);
buttonsPanel.add(Box.createHorizontalStrut(5));
buttonsPanel.add(storeImageIntoDBButton);
add(imageLabel, BorderLayout.CENTER);
add(buttonsPanel, BorderLayout.PAGE_END);
pack();
setLocationRelativeTo(null);
}
private void loadImageFromFile(ActionEvent event) {
JFileChooser chooser = new JFileChooser();
if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
ImageIcon imageIcon = new ImageIcon(chooser.getSelectedFile().getAbsolutePath());
imageLabel.setIcon(imageIcon);
}
}
private void storeImageIntoDB(ActionEvent event) {
try {
Database db = new Database();
db.storeIcon(imageLabel.getIcon());
}
catch (SQLException | IOException e) {
e.printStackTrace();
}
}
private void loadImageFromDB(ActionEvent event) {
try {
Database db = new Database();
Icon icon = db.loadIcon(1L);
imageLabel.setIcon(icon);
}
catch (SQLException | IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new MainFrame().setVisible(true));
}
}
您正在从表中选择每一列。您应该只选择包含图像字节的列;其次,我会验证读取的字节数与写入的字节数是否相同,但因为我们不知道图像是如何写入的,所以我们不知道使用这种方法是否是最好的方法;第三,我将使用<代码> IIGIOO过<代码> CREATIONIONE/CODE >,仅仅因为它不涉及任何线程类型,您可能只考虑存储图像的某种标识符,然后,当我必须这样做时,它可以用来查找和加载来自本地或远程源(如Web服务)的图像,我使用blob数据类型和JDBC提供的输入/输出流接口来处理该数据类型。如果图像存储在数据库中,最好考虑它们可能的压缩(java提供了一个方便的gzip接口)。您应该只选择包含图像字节的列;其次,我会验证读取的字节数与写入的字节数是否相同,但因为我们不知道图像是如何写入的,所以我们不知道使用这种方法是否是最好的方法;第三,我将使用<代码> IIGIOO过<代码> CREATIONIONE/CODE >,仅仅因为它不涉及任何线程类型,您可能只考虑存储图像的某种标识符,然后,当我必须这样做时,它可以用来查找和加载来自本地或远程源(如Web服务)的图像,我使用blob数据类型和JDBC提供的输入/输出流接口来处理该数据类型。如果图像存储在数据库中,最好考虑它们可能的压缩(java提供了一个方便的gzip接口)。它得到了一个例外:驱动程序找不到应该在程序中添加驱动程序的位置在应用程序的类路径中,必须添加mysql驱动程序jar文件。如果您使用的是JDBC 4.2之前的版本,则必须使用
Class.forName(“com.mysql.JDBC.Driver”)代码>。所有这些都是您在项目中完成的,因此您不应该对这段代码有任何问题。它的获取异常:未找到驱动程序。在应用程序的类路径中,必须添加mysql驱动程序jar文件。如果您使用的是JDBC 4.2之前的版本,则必须使用Class.forName(“com.mysql.JDBC.Driver”)代码>。所有这些都是您在项目中完成的,因此您不应该对这些代码有任何问题。