mysql数据的批次转移

来源:互联网 发布:表提交给两个php 编辑:程序博客网 时间:2024/05/16 11:54
 


import java.sql.*;
 
 
public class  Lib2 extends Thread{
 
private Connection con=null;
Connection conn=null;
private int Row=0;
public Lib2(){
try{
String driver="com.mysql.jdbc.Driver";
String sqlurl="jdbc:mysql://192.168.24.130:3306/spidera";
Class.forName(driver);
con=DriverManager.getConnection(sqlurl,"liuqianqian","qian");

}catch(Exception e){
}
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost/test","root","123");
System.out.println("链接数据库成功");
}catch(ClassNotFoundException e){
System.out.println("没有找到驱动");
e.printStackTrace();
}catch(SQLException e){
System.out.println("链接数据库失败");
e.printStackTrace();
}
}


public void run(){
while(true){
GetElements();
 
try {
Thread.sleep(12);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
}
}
public void GetElements(){
 
try{                                                       
String sql="select * from searchmess1 LIMIT "+Row+",200";
Statement  st=con.createStatement();
ResultSet rs=st.executeQuery(sql);
PreparedStatement pre=conn.prepareStatement("insert into mytest(id,url,time) values(?,?,?)");

while(rs.next()){
int ids=rs.getInt(1);
String url=rs.getString(2);
   String title=rs.getString(3);
String date=rs.getString(4);
String source=rs.getString(5);
String readCount=rs.getString(6);
String text_xj1=rs.getString(7);
System.out.println(url);
 pre.setInt(1, ids);
    pre.setString(2,date);
     pre.setString(3,url);


   
     pre.addBatch();
   
}
pre.executeBatch();
     
rs.previous();


Row=Row+rs.getRow();
System.out.println(Row);
st.close();
}catch(Exception e){
System.out.println(e) ;}
}
public static void main(String[] agrs){

new  Lib2().start();
}
}
 
 



0 0