您的位置:首页 > 数据库 > MySQL

mysql 百万级数据查找,并写入txt文件

2012-08-29 00:30 856 查看
1,建立mysql的查询类,里面包含查询方法(两个参数,为了分页查找)

package com.test.lihongxu;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


public class ConnectMysql {

public List<Instance> getTag(int start,int end){
List<Instance> intList=new ArrayList<Instance>();
Connection con=getConnetion();
try {
con.setAutoCommit(false);//此处开启事务
Statement stm=con.createStatement();
ResultSet set=stm.executeQuery("select * from tag_feed limit "+start+","+end);
while(set.next()){
Instance instance=new Instance();
instance.setFeed_id(set.getInt(2));
instance.setTag_id(set.getInt(3));
intList.add(instance);
}
con.commit();//事务提交
con.close();
} catch (SQLException e) {
try {
con.rollback();
System.out.println("not Ok");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();

}
return intList;
}
public Connection getConnetion(){
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
con = DriverManager.getConnection("jdbc:mysql://localhost/minisite?useUnicode=true&characterEncoding=GBK", "root", "admin");
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}

}



2,采取算法规则如下:

1)先设定一个无限循环,

2)循环内嵌套一个循环,每次从数据库查500条,查四次 放入List,然后遍历List 写入txt

3)在内层循环设置一个flag ,当查的数据小于500条时,则false.

4)在外层循环判断false的值,若为false 跳出循环


package com.test.lihongxu;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class WriteTag implements Runnable {
public void run() {
//设置标记
boolean flag=true;
ConnectMysql cmysql = new ConnectMysql();
//外层循环,每次加4
for (int j = 0;;j += 4) {
List<Instance> intList = new ArrayList<Instance>();//储存两千条后清空,重新储存
for (int i = j; i < j + 4; i++) {
List<Instance> selectList = new ArrayList<Instance>();//储存五百条后清空,重新储存
int h = i * 500;
selectList = cmysql.getTag(h, 500);
intList.addAll(selectList);
//设置循环终止条件
if(selectList.size()<500){
flag=false;
}
}
//将两千条数据写入txt中
try {
FileWriter output = new FileWriter("D:\\tag.txt", true);
BufferedWriter bf = new BufferedWriter(output);
for (Instance l : intList) {
bf.write("http://zhan.renren.com/" + l.getFeed_id()
+ "?gid=" + l.getTag_id() + "\r\n");
}
bf.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//若查询终止,则跳出循环,结束查询和写入
if(!flag){
break;
}
}

}

public static void main(String[] args) {
WriteTag rt = new WriteTag();
Thread demo1 = new Thread(rt);
demo1.start();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: