java从mysql导出数据的具体实例
本文导语: 代码如下: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;impo...
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(); // 生成测试数据
//Exp();
Exp(0);
//System.out.println(readText("/opt/id.txt"));
}
/**
* 导出数据
*/
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();
}
}
}
/**
* 导出从startid开始的数据
* @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+"rn", "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);
}
/**
* 导出一小时内的数据
* @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