Home  >  Article  >  Database  >  java从mysql导出数据例子

java从mysql导出数据例子

WBOY
WBOYOriginal
2016-06-07 15:14:11978browse

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入 * 导出一小时内的数据 * @param conn */ public static void Exp(Connection conn) { int counter = 0; //一小时内的数据 Long timestamp = System.currentTimeMillis() - (600 * 60 * 1000);

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

 

  * 导出一小时内的数据

  * @param conn

  */

  public static void Exp(Connection conn) {

  int counter = 0;

  //一小时内的数据

  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

  {

  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();

  }

  }

  }

  [1] [2] [3] 

java从mysql导出数据例子

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:jTDS驱动兼容性问题Next article:java连接mysql的写法