java从mysql导出数据例子

WBOY
发布: 2016-06-07 15:14:11
原创
1018 人浏览过

欢迎进入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导出数据例子

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板