当先锋百科网

首页 1 2 3 4 5 6 7

public static void upload() throws Exception{
  Class.forName("com.mysql.jdbc.Driver");
  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
  String sql = "insert into testfile(files) values(?)";
  PreparedStatement pst = con.prepareStatement(sql);
  pst.setBinaryStream(1, new FileInputStream("F://项目//image//pubBack.png"));
  int c = pst.executeUpdate();
  System.out.println(c);
  System.out.println("finish");
 }
 public static void downLoad()throws Exception{
  Class.forName("com.mysql.jdbc.Driver");
  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
  String sql = "select files from testfile where id = 1";
  PreparedStatement pst = con.prepareStatement(sql);
  ResultSet rs = pst.executeQuery();
  if(rs.next()){
   BufferedInputStream bis = new BufferedInputStream(rs.getBinaryStream("files"));
   BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D://t.png"));
   int i;
   while ((i = bis.read())!= -1) {
    bos.write(i);
   }
   bis.close();
   bos.close();
  }
  System.out.println("finish");

 }

///数据库中类型

MySQL的四种BLOB类型
类型 大小(单位:字节)
TinyBlob 最大 255
Blob 最大 65K
MediumBlob 最大 16M
LongBlob 最大 4G
sql server中、、、、、、、、、、、、

二进制字符串:

              binary       image

              varbinary