|
非常简单的mysql的BlobTest实例
|
|
来源: 作者: |
|
- package org.mmc.dao.impl;
-
- import java.io.*;
- import java.sql.*;
-
-
-
- public class BlobTest
- {
- Connection con=null;
-
-
-
- public void addBlob()
- {
- try
- {
-
-
- PreparedStatement pstmt=con.prepareStatement("insert into blobtest values(1,?)");
- File file = new File("d:\\other\\apple.gif") ;
- FileInputStream fis = new FileInputStream(file);
-
- pstmt.setBinaryStream(1, fis, (int)file.length());
-
- pstmt.executeUpdate();
- pstmt.close();
- fis.close();
-
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
-
-
-
- public void createTable()
- {
- try
- {
- con.createStatement().execute("create table blobtest (id int ,pic blob,"+
- "constraint pk_blobtest primary key(id));");
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
-
-
-
- public void getBlob()
- {
-
- try
- {
-
- Statement stmt=con.createStatement();
- ResultSet rst=stmt.executeQuery("select * from blobtest where id=1");
- rst.next();
-
- Blob blob = rst.getBlob("pic") ;
- FileOutputStream out=new FileOutputStream(new File("d:/other/apple1.gif"));
- InputStream in=blob.getBinaryStream();
- int i;
- while((i=in.read())!=-1)
- out.write(i);
-
- in.close();
- out.close();
-
- }
- catch(Exception e){
- e.printStackTrace();
-
- }
- }
- public static void main(String[] args)throws Exception
- {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- BlobTest test=new BlobTest();
- test.con=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","12345");
- test.createTable();
- test.addBlob();
- test.getBlob();
- }
- }
|
| 上一篇:j2se 6 的新特性 下一篇:URL中的特殊字符 |
|
[ 收藏]
[ 推荐]
[ 评论(0条)]
[返回顶部] [打印本页]
[关闭窗口] |
|
|
| |
|
|
 |
|
相关信息 |
|
|
|
|
|