您好,欢迎访问一九零五行业门户网

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]
其它类似信息

推荐信息