JDBC读出blob

import java.awt.event.ActionEvent;
import java.sql.*;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * 
 * @author mustaq
 */
public class ImagePanel extends JPanel implements ActionListener {

	JButton browse;
	Connection con = null;

	public ImagePanel() {
		con = this.getConnection();
		browse = new JButton("Browse");
		browse.addActionListener(this);
		this.add(browse);
	}

	public Connection getConnection() {
		try {
			// Creating connection to DB
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/appdb";
			Connection c = DriverManager.getConnection(url, "root", "HuaDi5");
			return c;
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
			return null;
		}

	}

	public void imageWrite(File file) {
		try {

			FileInputStream io = new FileInputStream(file);
			String query = "insert into image(IMG) values(?)";
			java.sql.PreparedStatement stmt = con.prepareStatement(query);
			stmt.setBinaryStream(1, (InputStream) io, (int) file.length());
			stmt.executeUpdate();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}

	public BufferedImage getImageById(int id) {
		String query = "select IMG from image where IMG_ID = ?";
		BufferedImage buffimg = null;
		try {
			PreparedStatement stmt = con.prepareStatement(query);
			stmt.setInt(1, id);
			ResultSet result = stmt.executeQuery();
			result.next();
			InputStream img = result.getBinaryStream(1); // reading image as
															// InputStream
			buffimg = ImageIO.read(img); // decoding the inputstream as
											// BufferedImage

		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
		return buffimg;
	}

	@Override
	public void paint(Graphics g) {

		BufferedImage img = this.getImageById(2); // pass valid IMG_ID
		if (img != null)
			g.drawImage(img, 70, 20, this);

	}

	public static void main(String[] args) {
		JFrame frame = new JFrame("ImagePanel Demo");
		ImagePanel imgPanel = new ImagePanel();
		frame.setVisible(true);
		frame.setSize(600, 400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(imgPanel);

	}

	public void actionPerformed(ActionEvent e) {
		JFileChooser chooser = new JFileChooser();
		int returnVal = chooser.showOpenDialog(null);
		File file = null;
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = chooser.getSelectedFile(); // path to image
			this.imageWrite(file); // inserting image into database
			JOptionPane.showMessageDialog(this, "Image inserted.", "ImageDemo",
					JOptionPane.PLAIN_MESSAGE);
			this.repaint();

		}
	}

}

原文:http://codeglobe.blogspot.com/2009/03/readwrite-blob-fromto-mysql-in-java_21.html

源代码:http://pan.baidu.com/share/link?shareid=422360&uk=3878681452

第一次运行此程序,会有空指针提示。
添加图片的browse按钮要调整窗口才能出现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值