java从mysql导出数据例子
欢迎进入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]
推荐阅读
-
从Web查询数据库之PHP与MySQL篇
-
详解MySQL导出指定表中的数据的实例
-
java实现批量导入.csv文件到mysql数据库
-
Python之csv文件从MySQL数据库导入导出的方法
-
java开发easypoi导出excel表格数据
-
mysql 导出数据报错: row must be in range 0-65535
-
php从memcache读取数据再批量写入mysql的方法
-
PHP导出MySQL数据到Excel文件(fputcsv)
-
谈一谈个人利用Java的mysql的知识完成的数据库的项目-----用户信息管理系统
-
面试|简单描述MySQL中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)