码迷,mamicode.com
首页 > 数据库 > 详细

图像,大文件的mysql导入与读取存放到本地

时间:2014-08-29 18:34:49      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:package   import   public   mysql   null   

package conn;


import java.io.*;

import java.sql.Blob;

import java.sql.Clob;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;


import com.mysql.jdbc.Driver;

import com.sun.corba.se.spi.orbutil.fsm.Input;


public class Conn {

public Connection getConn(){

  Connection conn = null; 

  try {

Class.forName("com.mysql.jdbc.Driver");

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "gjl");

  } catch (Exception e) {

e.printStackTrace();

}  

  return conn;

  }


   public void setlob(){

    

  Connection conn = null;

  PreparedStatement pre = null;

  try {

Class.forName("com.mysql.jdbc.Driver");

conn = getConn();

   String sql = "insert into t_lob value (?,?,?)";

pre = conn.prepareStatement(sql);

File txt = new File("E:\\项目\\说明文件\\信息.txt");

File img = new File("G:\\1.jpg");

InputStream inTxt = new FileInputStream(txt);

InputStream inImg = new FileInputStream(img);

   pre.setString(1, "1");

pre.setAsciiStream(2, inTxt, (int)txt.length());

pre.setBinaryStream(3, inImg, (int)img.length());

        pre.executeUpdate();

        

        conn.close();

pre.close();

System.out.println("read-ok");

} catch (Exception e) {

e.printStackTrace();

}

   }

   

   public void getlob(){

  Connection conn = null;

  Statement stmt = null;

  ResultSet rs = null;

  try {

conn = getConn();

stmt = conn.createStatement();

rs = stmt.executeQuery("select * from t_lob");

while(rs.next()){

Clob clob = rs.getClob(2);

Blob blob = rs.getBlob(3);

InputStream txt = clob.getAsciiStream();

InputStream img = blob.getBinaryStream();

FileOutputStream OutTxt= new FileOutputStream(new File("F:\\T.txt"));

FileOutputStream OutImg= new FileOutputStream(new File("F:\\I.jpg"));

   int c1 = -1;

   int c2 = -1;

while(( c1 = txt.read())!= -1)

OutTxt.write(c1);

OutTxt.flush();

OutTxt.close();

while(( c2 = img.read())!= -1)

OutImg.write(c2);

OutImg.flush();

OutImg.close();

}

System.out.println("write-ok");

} catch (Exception e) {

e.printStackTrace();

}

  

  

   }

   public  static void main(String[]args){

  Conn conn = new Conn();

  // conn.getlob();

        // conn.setlob();

   }


}



图像,大文件的mysql导入与读取存放到本地

标签:package   import   public   mysql   null   

原文地址:http://8035665.blog.51cto.com/8025665/1546591

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!