欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)從數(shù)據(jù)庫(kù)導(dǎo)出大量數(shù)據(jù)記錄并保存到文件的方法

 更新時(shí)間:2015年11月03日 14:51:02   作者:5iasp  
這篇文章主要介紹了Java實(shí)現(xiàn)從數(shù)據(jù)庫(kù)導(dǎo)出大量數(shù)據(jù)記錄并保存到文件的方法,涉及Java針對(duì)數(shù)據(jù)庫(kù)的讀取及文件寫入等操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了Java實(shí)現(xiàn)從數(shù)據(jù)庫(kù)導(dǎo)出大量數(shù)據(jù)記錄并保存到文件的方法。分享給大家供大家參考,具體如下:

數(shù)據(jù)庫(kù)腳本:

-- Table "t_test" DDL
CREATE TABLE `t_test` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `title` varchar(255) DEFAULT NULL,
 `createTime` bigint(20) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

代碼:

package com.yanek.test; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.OutputStreamWriter; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
public class TestDB { 
 public static void main(String[] args) { 
  Test(); // 生成測(cè)試數(shù)據(jù) 
  //Exp(); 
  //Exp(0); 
  //System.out.println(readText("/opt/id.txt")); 
 } 
 /** 
  * 導(dǎo)出數(shù)據(jù) 
  */ 
  public static void Exp() { 
   Connection Conn=null; 
   try { 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/testcms?characterEncoding=GBK"; 
    String jdbcUsername = "root"; 
    String jdbcPassword = "root"; 
    Conn = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword); 
    System.out.println("conn"+Conn); 
    Exp(Conn); 
   } catch (SQLException e) { 
    e.printStackTrace(); 
   } 
   catch (InstantiationException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
   } catch (IllegalAccessException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
   } catch (ClassNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
   } 
   finally 
   { 
    try { 
     Conn.close(); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
   } 
  } 
  public static void Exp(int startid) { 
   Connection Conn=null; 
   try { 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/testcms?characterEncoding=GBK"; 
    String jdbcUsername = "root"; 
    String jdbcPassword = "root"; 
    Conn = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword); 
    System.out.println("conn"+Conn); 
    Exp(Conn,startid); 
   } catch (SQLException e) { 
    e.printStackTrace(); 
   } 
   catch (InstantiationException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
   } catch (IllegalAccessException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
   } catch (ClassNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
   } 
   finally 
   { 
    try { 
     Conn.close(); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
   } 
  } 
  /** 
  * 導(dǎo)出從startid開始的數(shù)據(jù) 
  * @param conn 
  * @param start_id 
  */ 
  public static void Exp(Connection conn,int start_id) { 
   int counter = 0; 
   int startid=start_id; 
   boolean flag = true; 
   while (flag) { 
    flag = false; 
    String Sql = "SELECT * FROM t_test WHERE id>" 
      + startid + " order by id asc LIMIT 50"; 
    System.out.println("sql===" + Sql); 
    try { 
     Statement stmt = conn.createStatement(); 
     ResultSet rs = stmt.executeQuery(Sql); 
      while (rs.next()) { 
       flag = true; 
       int id = rs.getInt("id"); 
       String title = rs.getString("title"); 
       startid = id ; 
       counter++; 
       writeContent(counter+"--id--"+id+"--title-"+title+"\r\n", "/opt/","log.txt",true); 
       System.out.println("i="+counter+"--id--"+id+"--title-"+title); 
      } 
     rs.close(); 
     stmt.close(); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
   } 
   writeContent(""+startid, "/opt/","id.txt",false); 
  } 
  /** 
  * 導(dǎo)出一小時(shí)內(nèi)的數(shù)據(jù) 
  * @param conn 
  */ 
  public static void Exp(Connection conn) { 
   int counter = 0; 
   //一小時(shí)內(nèi)的數(shù)據(jù) 
   Long timestamp = System.currentTimeMillis() - (60 * 60 * 1000); 
   boolean flag = true; 
   while (flag) { 
    flag = false; 
    String Sql = "SELECT * FROM t_test WHERE createTime>" 
      + timestamp + " LIMIT 50"; 
    System.out.println("sql===" + Sql); 
    try { 
     Statement stmt = conn.createStatement(); 
     ResultSet rs = stmt.executeQuery(Sql); 
     while (rs.next()) { 
      flag = true; 
      int id = rs.getInt("id"); 
      String title = rs.getString("title"); 
      Long lastmodifytime = rs.getLong("createTime"); 
      timestamp = lastmodifytime; 
      counter++; 
      System.out.println("i="+counter+"--id--"+id+"--title-"+title); 
     } 
     rs.close(); 
     stmt.close(); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
   } 
  } 
  public static void Test() { 
   Connection Conn=null; 
   try { 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/testcms?characterEncoding=GBK"; 
    String jdbcUsername = "root"; 
    String jdbcPassword = "root"; 
    Conn = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword); 
    System.out.println("conn"+Conn); 
    for(int i=1;i<=10000;i++) 
    { 
     add(Conn,"testTitle"+i+"-"+System.currentTimeMillis()); 
    } 
   } catch (SQLException e) { 
    e.printStackTrace(); 
   } 
   catch (InstantiationException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
   } catch (IllegalAccessException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
   } catch (ClassNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
   } 
   finally 
   { 
    try { 
     Conn.close(); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
   } 
  } 
  public static void add(Connection conn,String title) 
  { 
   PreparedStatement pstmt = null; 
   String insert_sql = "insert into t_test(title,createTime) values (?,?)"; 
   System.out.println("sql="+insert_sql); 
   try { 
    pstmt = conn.prepareStatement(insert_sql); 
    pstmt.setString(1,title); 
    pstmt.setLong(2,System.currentTimeMillis()); 
    int ret = pstmt.executeUpdate(); 
   } catch (SQLException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
   } 
   finally{ 
    try { 
     pstmt.close(); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }  
   } 
  } 
  /** 
   * 寫入內(nèi)容到文件 
   * 
   * @param number 
   * @param filename 
   * @return 
   */ 
  public static boolean writeContent(String c, String dirname,String filename,boolean isAppend) { 
   File f=new File(dirname); 
   if (!f.exists()) 
   { 
     f.mkdirs(); 
   } 
   try { 
    FileOutputStream fos = new FileOutputStream( dirname+File.separator+filename,isAppend); 
    OutputStreamWriter writer = new OutputStreamWriter(fos); 
    writer.write(c); 
    writer.close(); 
    fos.close(); 
   } catch (IOException e) { 
    e.printStackTrace(); 
    return false; 
   } 
   return true; 
  } 
  /** 
   * 從文件讀取內(nèi)容 
   * 
   * @param filename 
   * @return 
   */ 
  public static String readText(String filename) { 
   String content = ""; 
   try { 
    File file = new File(filename); 
    if (file.exists()) { 
     FileReader fr = new FileReader(file); 
     BufferedReader br = new BufferedReader(fr); 
     String str = ""; 
     String newline = ""; 
     while ((str = br.readLine()) != null) { 
      content += newline + str; 
      newline = "\n"; 
     } 
     br.close(); 
     fr.close(); 
    } 
   } catch (IOException e) { 
    e.printStackTrace(); 
   } 
   return content; 
  } 
} 

基本思想: 就是通過(guò)記錄開始記錄id,執(zhí)行多次sql來(lái)處理. 由于大數(shù)據(jù)量所以不能使用一條sql語(yǔ)句來(lái)輸出.否則會(huì)內(nèi)存不足導(dǎo)致錯(cuò)誤.

主要用途: 可以使用在做接口開發(fā)時(shí),給第三方提供數(shù)據(jù)增量輸出的場(chǎng)景使用.

希望本文所述對(duì)大家Java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • SpringBoot使用Jasypt對(duì)YML文件配置內(nèi)容加密的方法(數(shù)據(jù)庫(kù)密碼加密)

    SpringBoot使用Jasypt對(duì)YML文件配置內(nèi)容加密的方法(數(shù)據(jù)庫(kù)密碼加密)

    本文介紹了如何在SpringBoot項(xiàng)目中使用Jasypt對(duì)application.yml文件中的敏感信息(如數(shù)據(jù)庫(kù)密碼)進(jìn)行加密,通過(guò)引入Jasypt依賴、配置加密密鑰、加密敏感信息并測(cè)試解密功能,可以提高配置文件的安全性,減少因配置文件泄露導(dǎo)致的安全風(fēng)險(xiǎn),感興趣的朋友一起看看吧
    2025-03-03
  • java中以DES的方式實(shí)現(xiàn)對(duì)稱加密并提供密鑰的實(shí)例

    java中以DES的方式實(shí)現(xiàn)對(duì)稱加密并提供密鑰的實(shí)例

    這篇文章主要介紹了java中以DES的方式實(shí)現(xiàn)對(duì)稱加密并提供密鑰的實(shí)例的相關(guān)資料,這里提供實(shí)例幫助大家學(xué)習(xí)理解這部分知識(shí),需要的朋友可以參考下
    2017-08-08
  • java this引用逃逸詳解

    java this引用逃逸詳解

    這篇文章主要介紹了java this引用逃逸的相關(guān)資料,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-12-12
  • MyBatis執(zhí)行動(dòng)態(tài)SQL的方法

    MyBatis執(zhí)行動(dòng)態(tài)SQL的方法

    今天小編就為大家分享一篇關(guān)于MyBatis執(zhí)行動(dòng)態(tài)SQL的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-12-12
  • java多線程實(shí)現(xiàn)下載圖片并壓縮

    java多線程實(shí)現(xiàn)下載圖片并壓縮

    這篇文章主要為大家詳細(xì)介紹了java多線程實(shí)現(xiàn)下載圖片并壓縮,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • swagger中如何給請(qǐng)求添加header

    swagger中如何給請(qǐng)求添加header

    這篇文章主要介紹了swagger中如何給請(qǐng)求添加header,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Java中String類常用類型實(shí)例總結(jié)

    Java中String類常用類型實(shí)例總結(jié)

    在我們開發(fā)中經(jīng)常會(huì)用到很多的常用的工具類,這里做一個(gè)總結(jié),下面這篇文章主要給大家介紹了關(guān)于Java中String類常用類型的相關(guān)資料,String類代表字符串,需要的朋友可以參考下
    2021-12-12
  • JetBrains?產(chǎn)品輸入激活碼?Key?is?invalid?完美解決方案

    JetBrains?產(chǎn)品輸入激活碼?Key?is?invalid?完美解決方案

    JetBrains?系列產(chǎn)品(IDEA、Pycharm?等)使用本站破解教程?(opens?new?window),在輸入激活碼時(shí),部分小伙伴反應(yīng)說(shuō)提示?Key?is?invalid?無(wú)法激活,今天小編給大家分享完美解決方案,感興趣的朋友跟隨小編一起看看吧
    2022-11-11
  • Java中LocalDate的詳細(xì)方法舉例總結(jié)

    Java中LocalDate的詳細(xì)方法舉例總結(jié)

    這篇文章主要給大家介紹了關(guān)于Java中LocalDate詳細(xì)方法舉例的相關(guān)資料,LocalDate主要是用來(lái)處理日期的類,文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • Java中final關(guān)鍵字和final的四種用法實(shí)例

    Java中final關(guān)鍵字和final的四種用法實(shí)例

    final關(guān)鍵字代表最終的、不可改變的,下面這篇文章主要給大家介紹了關(guān)于Java中final關(guān)鍵字和final的四種用法實(shí)例,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02

最新評(píng)論