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 (); Generating test data
Exp ();
EXP (0);
System.out.println (ReadText ("/opt/id.txt"));
}
/**
* Export Data
*/
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 ();
}
}
}
/**
* Export data starting from 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+ "\ r \ n", "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);
}
/**
* Export data within one hour
* @param conn
*/
Public static void Exp (Connection conn) {
int counter = 0;
//within one hour of data
Long timestamp = System.currenttimemillis ()-(* * 1000);
Boolean flag = true;
while (flag) {
flag = false;
String Sql = "SELECT * from T_test WHERE createtime>"
+ timestamp + "LIMIT";
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<=10000;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 ();
}
}
}
/**
* Write content to file
*
* @param number
* @param filename
* @return
*/
public static Boolean writecontent (string C, string dirname,string Filename,boolean isappend) {
File F=new file (dirname);
if (!f.exists ())
{
F.mkdirs ();
}
try {
FileOutputStream fos = new FileOutputStream (dirname+file.separator+filename,isappend);
OutputStreamWriter writer = new OutputStreamWriter (FOS);
Writer.write (c);
Writer.close ();
Fos.close ();
catch (IOException e) {
E.printstacktrace ();
return false;
}
return true;
}
/**
* Read content from File
*
* @param filename
* @return
*/
public static string ReadText (string filename) {
String content = "";
try {
File File = new file (filename);
if (file.exists ()) {
FileReader FR = new FileReader (file);
BufferedReader br = new BufferedReader (FR);
String str = "";
String newline = "";
while (str = Br.readline ())!= null) {
Content + + newline + str;
newline = "\ n";
}
Br.close ();
Fr.close ();
}
catch (IOException e) {
E.printstacktrace ();
}
return content;
}
}