22---数据库编程09(blob)

 

Blob和Clob相似,Clob存储海量文本信息;
Blob专门用于存放二进制数,如图片、电影等;
 import java.sql.DriverManager;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.util.*;
 import java.io.*;
 import java.text.*;
 public class Demo{
  //定义数据库的驱动程序
  public static final String DBDRIVER="org.gjt.mm.mysql.Driver";
  //定义数据库的连接地址:
  public static final String DBURL="jdbc:mysql://localhost:3306/lid?characterEncoding=utf8";
  //定义数据库的用户名
  public static final String DBUSER="root";
  //定义数据库的密码
  public static final String DBPASSWORD="123";
  public static void main(String args[]) throws Exception{
   Connection conn=null;
   PreparedStatement pstmt=null;//数据库操作
   ResultSet rs=null;
   //加载数据库驱动
   Class.forName(DBDRIVER);//创建一个该驱动类的Class对象;  
   conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD); 
   String name="lid";
   String sql="insert into userblob(names,photo) values(?,?)";
   pstmt=conn.prepareStatement(sql);//实例化PreparedStatement
   
   File f=new File("D:"+File.separator+"che.jpg");
   InputStream in=new FileInputStream(f);
   pstmt.setString(1,name);
   pstmt.setBinaryStream(2,in,(int)f.length());
   
   try{
    pstmt.executeUpdate();
    }
   catch(Exception e){
    System.out.println("11111111111");
    System.out.println(e);
    }
   pstmt.close();
   conn.close();
   }
  }

存入的图片是输出不出来的我们可以通过另存为们看看是否存入成功;

  import java.sql.DriverManager;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.util.*;
 import java.io.*;
 import java.text.*;
 public class Demo{
  //定义数据库的驱动程序
  public static final String DBDRIVER="org.gjt.mm.mysql.Driver";
  //定义数据库的连接地址:
  public static final String DBURL="jdbc:mysql://localhost:3306/lid?characterEncoding=utf8";
  //定义数据库的用户名
  public static final String DBUSER="root";
  //定义数据库的密码
  public static final String DBPASSWORD="123";
  public static void main(String args[]) throws Exception{
   Connection conn=null;
   PreparedStatement pstmt=null;//数据库操作
   ResultSet rs=null;
   //加载数据库驱动
   Class.forName(DBDRIVER);//创建一个该驱动类的Class对象;  
   conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD); 
   int id =1;
   String sql="select names , photo from userblob where id=?";
   pstmt=conn.prepareStatement(sql);//实例化PreparedStatement
   pstmt.setInt(1,id);
   rs=pstmt.executeQuery();
   InputStream in=null;
   if(rs.next()){
     String name=rs.getString(1);
     in=rs.getBinaryStream(2);
    }
  
   File f=new File("D:"+File.separator+"c.gif");
   OutputStream os=new FileOutputStream(f);
   while(in.read()!=-1){//边读边写
    os.write(in.read());
    }
   
   pstmt.close();
   conn.close();
   }
  }
 通过blob读取:
  import java.sql.DriverManager;
  import java.sql.Connection;
  import java.sql.SQLException;
  import java.sql.PreparedStatement;
  import java.sql.Blob;
  import java.sql.ResultSet;
  import java.util.*;
  import java.io.*;
  import java.text.*;
  public class Demo{
   //定义数据库的驱动程序
   public static final String DBDRIVER="org.gjt.mm.mysql.Driver";
   //定义数据库的连接地址:
   public static final String DBURL="jdbc:mysql://localhost:3306/lid?characterEncoding=utf8";
   //定义数据库的用户名
   public static final String DBUSER="root";
   //定义数据库的密码
   public static final String DBPASSWORD="123";
   public static void main(String args[]) throws Exception{
    Connection conn=null;
    PreparedStatement pstmt=null;//数据库操作
    ResultSet rs=null;
    //加载数据库驱动
    Class.forName(DBDRIVER);//创建一个该驱动类的Class对象;  
    conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD); 
    int id =1;
    String sql="select names , photo from userblob where id=?";
    pstmt=conn.prepareStatement(sql);//实例化PreparedStatement
    pstmt.setInt(1,id);
    rs=pstmt.executeQuery();
    Blob b=null;
    if(rs.next()){
     String name=rs.getString(1);
     b=rs.getBlob(2);
     }
    File f=new File("D:"+File.separator+"c.gif");
    OutputStream os=new FileOutputStream(f);
    os.write(b.getBytes(1,(int)b.length()));
    os.close();
    pstmt.close();
    conn.close();
    }
   }


 

你可能感兴趣的:(22---数据库编程09(blob))