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

java從mysql導出數(shù)據(jù)的具體實例

 更新時間:2013年12月17日 16:31:50   作者:  
這篇文章主要介紹了java從mysql導出數(shù)據(jù)的具體實例,有需要的朋友可以參考一下

復制代碼 代碼如下:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

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();  // 生成測試數(shù)據(jù)
  //Exp();
  Exp(0);
  //System.out.println(readText("/opt/id.txt"));
 }

 /**
  * 導出數(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/test?characterEncoding=GBK";
    //String jdbcUsername = "root";
    //String jdbcPassword = "mysql";
    Conn = DriverManager.getConnection(jdbcUrl, "root", "mysql");

    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/test?characterEncoding=GBK";
    String jdbcUsername = "root";
    String jdbcPassword = "mysql";
    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();
    }
   }

  }

  /**
   * 導出從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", "D:\\","log.txt",true);

       System.out.println("i="+counter+"--id--"+id+"--title-"+title);

      }
     rs.close();
     stmt.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }

   writeContent(""+startid, "D:\\","id.txt",false);

  }

  /**
   * 導出一小時內的數(shù)據(jù)
   * @param conn
   */

  public static void Exp(Connection conn) {

   int counter = 0;
   //一小時內的數(shù)據(jù)
   Long timestamp = System.currentTimeMillis() - (600 * 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/test?characterEncoding=GBK";
    String jdbcUsername = "root";
    String jdbcPassword = "mysql";
    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();
    }
   }

     }

  /**
   * 寫入內容到文件
   *
   * @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;
  }

 
  /**
   * 從文件讀取內容
   *
   * @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;
  }
}

相關文章

  • Java一些常見的出錯異常處理方法總結

    Java一些常見的出錯異常處理方法總結

    下面小編就為大家?guī)硪黄狫ava一些常見的出錯異常處理方法總結。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • 淺談springboot自動裝配原理

    淺談springboot自動裝配原理

    作為Spring Boot的精髓,自動配置原理首當其沖.今天就帶大家了解一下springboot自動裝配的原理,文中有非常詳細的代碼示例,對正在學習java的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • mybatis插入與批量插入返回ID的原理詳解

    mybatis插入與批量插入返回ID的原理詳解

    這篇文章主要給大家介紹了關于mybatis插入與批量插入返回ID的原理的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用mybatis具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-07-07
  • 大模型chat/completions和completions區(qū)別解析

    大模型chat/completions和completions區(qū)別解析

    OpenAI的completions和chat/completions是兩個不同的端點,completions用于單次文本補全,而chat/completions用于多輪對話生成,選擇哪個端點取決于你的具體需求,本文介紹大模型chat/completions和completions區(qū)別,感興趣的朋友一起看看吧
    2025-03-03
  • java并發(fā)無鎖多線程單線程示例詳解

    java并發(fā)無鎖多線程單線程示例詳解

    這篇文章主要為大家介紹了java并發(fā)無鎖多線程單線程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • Java對象的XML序列化與反序列化實例解析

    Java對象的XML序列化與反序列化實例解析

    這篇文章主要介紹了Java對象的XML序列化與反序列化實例解析,小編覺得還是挺不錯的,這里分享給大家。
    2017-10-10
  • 如何在springboot中引入?yún)?shù)校驗

    如何在springboot中引入?yún)?shù)校驗

    一般我們判斷前端傳過來的參數(shù),需要對某些值進行判斷,是否滿足條件,而springboot相關的參數(shù)校驗注解,可以解決我們這個問題,本文給大家介紹如何在springboot中引入?yún)?shù)校驗,感興趣的朋友一起看看吧
    2023-12-12
  • SpringBoot+MyBatis實現(xiàn)MD5加密數(shù)據(jù)庫用戶密碼的方法

    SpringBoot+MyBatis實現(xiàn)MD5加密數(shù)據(jù)庫用戶密碼的方法

    MD5技術主要用于對用戶密碼加密,增加賬戶的安全性,他具有不可逆的特性,不會被輕易解密,這篇文章給大家介紹SpringBoot+MyBatis實現(xiàn)MD5加密數(shù)據(jù)庫用戶密碼的方法,感興趣的朋友跟隨小編一起看看吧
    2024-03-03
  • 深入理解Java注解類型(@Annotation)

    深入理解Java注解類型(@Annotation)

    這篇文章主要介紹了深入理解Java注解類型(@Annotation),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • 使用Java進行圖像處理的一些基礎操作

    使用Java進行圖像處理的一些基礎操作

    這篇文章主要介紹了使用Java進行圖像處理的一些基礎操作,就載入和輸出相關的知識進行了講解,需要的朋友可以參考下
    2015-10-10

最新評論