JDBC读出blob
来源:互联网 发布:炉石传输数据遇到问题 编辑:程序博客网 时间:2024/06/14 01:39
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 DBClass.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// InputStreambuffimg = ImageIO.read(img); // decoding the inputstream as// BufferedImage} catch (Exception ex) {System.out.println(ex.getMessage());}return buffimg;}@Overridepublic void paint(Graphics g) {BufferedImage img = this.getImageById(2); // pass valid IMG_IDif (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 imagethis.imageWrite(file); // inserting image into databaseJOptionPane.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按钮要调整窗口才能出现
- JDBC读出blob
- BLOB对象的读出
- 关于 oracle blob 写入读出修改
- jdbc操作blob,clob
- JDBC CLOB\BLOB操作
- jdbc操作blob,clob
- JDBC ORACLE BLOB处理
- jdbc操作数据库blob
- JDBC:LOB---BLOB、CLOB
- jdbc+hibernate操纵blob数据
- JDBC操作Clob,Blob字段
- jdbc操作BLOB类型数据
- Jdbc处理oracle中的Blob
- JDBC操作Oracle BLOB对象
- JDBC 使用 mysql Blob类型
- jdbc大数据 CLOB BLOB
- Oracle JDBC存入图片Blob
- orcl jdbc 存放图片blob
- UTF-8编码规则和java编码格式总结(转载从网络)
- UVA 10397
- Android MediaPlayer使用方法简单介绍
- 使用pull解析器操作xml文件
- ASP.NET中26个常用性能优化方法
- JDBC读出blob
- archlinux无法联网解决方法
- java的PropertyChangeListener
- ASP.NET操作XML
- Code First Entity Framework基本与最佳添加(add/create),删除(delete/remove),修改(update/modify)操作
- C#数组学习实例
- 三种SQL分页法
- fedora 17 安装 skyeye1.3.5
- C#模拟http 发送post或get请求