JDBC扩展---JDBC处理大数据

 

JDBC扩展---JDBC处理大数据

一  使用目的

    在实际开发中,程序需要把大文本或二进制数据保存到数据库。

二  概念

1.大数据也称LOB(Loarg Objects)  LOB又称clob 和blob

    (1)Clob用于存储大文本 用Text表示

    (2)Blob用于存储二进制数据 例如图像。声音 二进制文等

注:对MySQL而言只有blob,而没有clob,mysql存储大文本采用的是Text,Text和blob分别又分为:

(1)TINYTEXT、TEXT、MEDIUMTEXT和LONGTEXT

(2)TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB

三  代码分析

(1)向数据库中插入大文本,下面是插入数据库的方法

    public void insert() {

       Connection con = null;

       PreparedStatement st = null;

       ResultSet rs = null;

       try {

           //获取连接

           con = DBManager.getConnection();

           //定义sql语句

           String sql = "insert into testclob (resume) value(?)";

           //创建预处理对象

           st = con.prepareStatement(sql);

           //为占位符赋值  ,给输入流对象做参数

           //得到指定资源url对象

           File f = new File("1.txt");

           //系统会自动的读取1.txt文件中的内容并放到流中

           st.setCharacterStream(1, new FileReader(f),f.length());

           //执行更新语句

           int result = st.executeUpdate();

           if(result>0){

              System.out.println("插入成功");

           }else{

              System.out.println("插入失败");

           }

           DBManager.release(con, st, rs);

       } catch (SQLException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       } catch (FileNotFoundException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

    }

      注:要想向数据库中插入文本,就要用到的是输入流,先指定要向数据库中插入的数据

       File f = new File("1.txt");

       然后调用下面这个方法把文件写入到流中

       st.setCharacterStream(1, new FileReader(f),f.length());

(2)从数据库总读取文件

    public void find(){

       Connection con = null;

       PreparedStatement st = null;

       ResultSet rs = null;

      

       try {

           //获取连接

           con = DBManager.getConnection();

           //定义sql语句

           String sql = "select resume from testclob where id=1";

           //创建预处理对象

           st = con.prepareStatement(sql);

           //执行更新

           rs = st.executeQuery();

           if(rs.next()){

              //放到流中

              Reader reader = rs.getCharacterStream("resume");

              //创建缓冲区

              char buff[] = new char[1024];

              //通过循环的方法读取

              int len = 0;

              FileWriter writer = new FileWriter("3.txt");

              while((len = reader.read(buff))>0){

                  //读取后放到文件中

                  writer.write(buff,0,len);

              }

writer.close();

              reader.close();

           }

           DBManager.release(con, st, rs);

       } catch (SQLException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

  }

注:在读取的时候,可能有人会这样写:

        String str  = rs.getString("resume");

     但是这样只是适合于文本小的,如果是大的文本,则很可能会死机,所有用这个方法的时候要慎重。

四   附加上配置文件  dp.properties   和DBManager.java

   db.properties文件

#\u8FDE\u63A5\u8BBE\u7F6E

driverClassName=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/jdbc

username=root

password=qiao

 

#<!-- \u521D\u59CB\u5316\u8FDE\u63A5 -->

initialSize=5

 

#\u6700\u5927\u8FDE\u63A5\u6570\u91CF

maxActive=5

 

#<!-- \u6700\u5927\u7A7A\u95F2\u8FDE\u63A5 -->

maxIdle=20

 

#<!-- \u6700\u5C0F\u7A7A\u95F2\u8FDE\u63A5 -->

minIdle=5

 

#<!-- \u8D85\u65F6\u7B49\u5F85\u65F6\u95F4\u4EE5\u6BEB\u79D2\u4E3A\u5355\u4F4D 60000\u6BEB\u79D2/1000\u7B49\u4E8E60\u79D2 -->

maxWait=3000

 

 

#JDBC\u9A71\u52A8\u5EFA\u7ACB\u8FDE\u63A5\u65F6\u9644\u5E26\u7684\u8FDE\u63A5\u5C5E\u6027\u5C5E\u6027\u7684\u683C\u5F0F\u5FC5\u987B\u4E3A\u8FD9\u6837\uFF1A[\u5C5E\u6027\u540D=property;]

#\u6CE8\u610F\uFF1A"user" \u4E0E "password" \u4E24\u4E2A\u5C5E\u6027\u4F1A\u88AB\u660E\u786E\u5730\u4F20\u9012\uFF0C\u56E0\u6B64\u8FD9\u91CC\u4E0D\u9700\u8981\u5305\u542B\u4ED6\u4EEC\u3002

connectionProperties=useUnicode=true;characterEncoding=UTF-8

 

#\u6307\u5B9A\u7531\u8FDE\u63A5\u6C60\u6240\u521B\u5EFA\u7684\u8FDE\u63A5\u7684\u81EA\u52A8\u63D0\u4EA4\uFF08auto-commit\uFF09\u72B6\u6001\u3002

defaultAutoCommit=true

 

#driver default \u6307\u5B9A\u7531\u8FDE\u63A5\u6C60\u6240\u521B\u5EFA\u7684\u8FDE\u63A5\u7684\u53EA\u8BFB\uFF08read-only\uFF09\u72B6\u6001\u3002

#\u5982\u679C\u6CA1\u6709\u8BBE\u7F6E\u8BE5\u503C\uFF0C\u5219\u201CsetReadOnly\u201D\u65B9\u6CD5\u5C06\u4E0D\u88AB\u8C03\u7528\u3002\uFF08\u67D0\u4E9B\u9A71\u52A8\u5E76\u4E0D\u652F\u6301\u53EA\u8BFB\u6A21\u5F0F\uFF0C\u5982\uFF1AInformix\uFF09

defaultReadOnly=

 

#driver default \u6307\u5B9A\u7531\u8FDE\u63A5\u6C60\u6240\u521B\u5EFA\u7684\u8FDE\u63A5\u7684\u4E8B\u52A1\u7EA7\u522B\uFF08TransactionIsolation\uFF09\u3002

#\u53EF\u7528\u503C\u4E3A\u4E0B\u5217\u4E4B\u4E00\uFF1A\uFF08\u8BE6\u60C5\u53EF\u89C1javadoc\u3002\uFF09NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE

defaultTransactionIsolation=READ_UNCOMMITTED

 

DBManager.java

 

import java.io.InputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Properties;

 

 

public class DBManager {

   

    private static String username;

    private static String password;

    private static String url;

    private static String driver;

   

    static{

       try{

           InputStream in = DBManager.class.getClassLoader().getResourceAsStream("db.properties");

           Properties prop = new Properties();

           prop.load(in);

          

           driver = prop.getProperty("driverClassName");

           url = prop.getProperty("url");

          

           username = prop.getProperty("username");

           password = prop.getProperty("password");

          

           Class.forName(driver);

          

       }catch (Exception e) {

           throw new ExceptionInInitializerError(e);

       }

    }

   

   

    public static Connection getConnection() throws SQLException{

       return DriverManager.getConnection(url, username, password);

    }

   

   

    public static void release(Connection conn,Statement st,ResultSet rs){

      

       if(rs!=null){

           try{

              rs.close();

           }catch (Exception e) {e.printStackTrace();}

            rs = null;

       }

       if(st!=null){

           try{

              st.close();

           }catch (Exception e) {e.printStackTrace();}

           st = null;

       }

       if(conn!=null){

           try{

              conn.close();

           }catch (Exception e) {e.printStackTrace();}

           conn = null;

        } 

    }

}

你可能感兴趣的:(JDBC扩展---JDBC处理大数据)