激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

香港云服务器
服務(wù)器之家 - 編程語(yǔ)言 - JAVA教程 - java從mysql導(dǎo)出數(shù)據(jù)的具體實(shí)例

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

2019-10-25 13:24java技術(shù)網(wǎng) JAVA教程

這篇文章主要介紹了java從mysql導(dǎo)出數(shù)據(jù)的具體實(shí)例,有需要的朋友可以參考一下

代碼如下:


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();  // 生成測(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/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();
    }
   }

  }

  /**
   * 導(dǎo)出從startid開(kāi)始的數(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);

  }

  /**
   * 導(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() - (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();
    } 
   }

     }

  /**
   * 寫(xiě)入內(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;
  }

 

延伸 · 閱讀

精彩推薦
454
主站蜘蛛池模板: 天天都色| 国产精品久久久免费 | 久久久在线 | 精品国产视频一区二区三区 | 99久在线视频 | 污视频在线免费 | 久久久久亚洲美女啪啪 | 毛片免费一区二区三区 | 国产一区二区在线观看视频 | 亚洲国产网址 | 毛片视频播放 | 黄色网址进入 | 国产毛毛片一区二区三区四区 | 日日噜噜噜噜久久久精品毛片 | 男女牲高爱潮免费视频男女 | 羞羞答答tv | 狼网| 成人免费乱码大片a毛片视频网站 | 欧美一级黄 | 国产91丝袜在线播放0 | 九九热免费观看 | 久久久久91视频 | 中文字幕亚洲一区二区三区 | 夜添久久精品亚洲国产精品 | 91福利社在线 | 羞羞网站在线看 | 毛片118极品美女写真 | av国产免费| 日本在线观看视频网站 | 巨根插入| 自拍偷拍999 | 在线中文日韩 | 日韩视频一区在线 | 黄色免费电影网址 | 成人毛片在线免费观看 | 黄污视频在线看 | 中文字幕在线不卡视频 | 一区二区免费网站 | 在线播放亚洲精品 | 亚洲一区成人 | 国产亚洲欧美视频 |