1. 程式人生 > >JDBC(6)mysql中的大資料處理

JDBC(6)mysql中的大資料處理

免費錄播jdbc視訊

JDBC操作

驅動可以不註冊

這裡寫圖片描述

        * 可以省略 Class.forName(driver); (高版本)
        * 原因:mysql-connector-java-5.1.22-bin.jar/META-INF/services/java.sql.Driver 內容com.mysql.jdbc.Driver ,程式將自動載入(SPI機制)


        mysql-connector-java-5.1.5-bin.jar不行

大資料處理

    * PreparedStatement物件
        * sql注入:防止實際引數作為sql語句的一部分。
        * 大資料處理
            * mysql大資料型別:
                * blob :Binary
Large Object,二進位制大物件。 mysql提供blob(64k),longblob(4G) * java物件api : * psmt.setBinaryStream(parameterIndex, x) 二進位制資料或者 * psmt.setBlob(parameterIndex, inputStream) blob物件 * clob :Character Large Object字元大物件。mysql提供text
(64k),longtext(4G)

大資料表準備

create table bigimg(
  id int primary key auto_increment,
  img blob
);


create table bigtext(
  id int primary key auto_increment,
  content longtext
);
@Test
    public void sendImg() {
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null
; try { conn = JdbcUtil.getConnection(); //1 準備sql語句 String sql = "insert into bigimg(img) values(?)"; //2準備預處理物件 psmt = conn.prepareStatement(sql); // * 圖片的資料流 FileInputStream is = new FileInputStream(new File("臺灣.jpg")); //3設定內容 psmt.setBinaryStream(1, is); //4執行sql int r = psmt.executeUpdate(); //不需要sql語句 System.out.println(r); } catch (Exception e) { e.printStackTrace(); } finally { JdbcUtil.closeResource(conn, psmt, rs); } } @Test public void getImg() { Connection conn = null; PreparedStatement psmt = null; ResultSet rs = null; try { conn = JdbcUtil.getConnection(); //1 準備sql String sql = "select img from bigimg where id = ?"; //2預處理物件 psmt = conn.prepareStatement(sql); //3設定引數 psmt.setInt(1, 1); //4執行sql rs = psmt.executeQuery(); //不需要sql語句 //5處理結果 if (rs.next()) { System.out.println("下載中"); //獲得流並儲存 InputStream is = rs.getBinaryStream("img"); FileOutputStream out = new FileOutputStream(new File("bak.jpg")); byte[] buf = new byte[1024]; int len = -1; while ((len = is.read(buf)) != -1) { out.write(buf, 0, len); } out.close(); is.close(); } else { System.out.println("沒有資料"); } } catch (Exception e) { e.printStackTrace(); } finally { JdbcUtil.closeResource(conn, psmt, rs); } } @Test public void getText() { Connection conn = null; PreparedStatement psmt = null; ResultSet rs = null; try { conn = JdbcUtil.getConnection(); //1 -- 使用的欄位的別名 String sql = "select content c from bigtext where id = ?"; //2 psmt = conn.prepareStatement(sql); //3 psmt.setInt(1, 2); //4執行 rs = psmt.executeQuery(); if (rs.next()) { System.out.println("下載中"); Reader reader = rs.getCharacterStream("c"); //獲得內容時,必須使用別名 Writer writer = new OutputStreamWriter(new FileOutputStream("d:/1.txt"), "UTF-8"); char[] buf = new char[1024]; int len = -1; while ((len = reader.read(buf)) != -1) { writer.write(buf, 0, len); } writer.close(); reader.close(); } else { System.out.println("沒有"); } } catch (Exception e) { e.printStackTrace(); } finally { JdbcUtil.closeResource(conn, psmt, rs); } } @Test public void sendText() { //java.lang.OutOfMemoryError: Java heap space Connection conn = null; PreparedStatement psmt = null; ResultSet rs = null; try { conn = JdbcUtil.getConnection(); //1 String sql = "insert into bigtext(content) values(?)"; //2 psmt = conn.prepareStatement(sql); // * 提供字串流 Reader reader = new InputStreamReader(new FileInputStream(new File("d:/鬥破蒼穹.txt")), "UTF-8"); //3 引數 psmt.setCharacterStream(1, reader); //4 int r = psmt.executeUpdate(); System.out.println(r); } catch (Exception e) { e.printStackTrace(); } finally { JdbcUtil.closeResource(conn, psmt, rs); } }

批處理

Statement物件
1. addBatch(String sql) 給批處理快取中,新增sql語句
2. clearBatch() 清空快取
3. executeBatch() 將快取中的sql語句都執行

* PreparedStatement物件*
1. 獲得物件,提供sql
2. addBatch() ,將指定的引數新增到批處理快取

sql指令碼

create table batch(id int primary key , content varchar(30));
@Test
    public void statement(){ //語句執行
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;

        try {
            conn = JdbcUtil.getConnection();

            st = conn.createStatement();

            //批處理
            st.addBatch("create database day12_batch");
            st.addBatch("use day12_batch");
            st.addBatch("create table batch(id int primary key auto_increment , content varchar(30))");
            st.addBatch("insert into batch(content) values('jack')");
            st.addBatch("insert into batch(content) values('rose')");


            //執行
            int[] arr = st.executeBatch();
            System.out.println(Arrays.toString(arr));

        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            JdbcUtil.closeResource(conn, st, rs);
        }
    }

    @Test
    public void prepared(){ //預處理
        //String url = "jdbc:mysql:///day12_batch?useUnicode=true&characterEncoding=UTF-8";
        long start = System.currentTimeMillis();
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;

        try {
            conn = JdbcUtil.getConnection();

            String sql = "insert into batch(content) values(?)";
            psmt = conn.prepareStatement(sql);

            //批處理--設定引數
            for(int i = 0 ; i< 100 ; i++){
                psmt.setString(1, i + "");
                // 新增到快取
                psmt.addBatch(); //沒有sql語句
            }

            //執行
            int[] arr = psmt.executeBatch();
            System.out.println(Arrays.toString(arr));

        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            JdbcUtil.closeResource(conn, psmt, rs);
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);

    }

時間處理

 Statement物件,字串的形式處理或mysql提供時間函式now()
 PreparedStatement物件 ,getDate(java.sql.Date)  getTime(java.sql.Time) getTimeStamp(java.sql.TimeStamp)
                * new java.sql.Date(new java.util.Date().getTime())

sql語句


CREATE TABLE dt(

  d DATE,

  t TIME,

  ts TIMESTAMP

)

這裡寫圖片描述
這裡寫圖片描述

事務

create table account(
  id int primary key auto_increment,
  username varchar(50),
  money int
);

insert into account(username,money) values('jack',500);
insert into account(username,money) values('rose',500);