从数据库中获取数据(数据量太大,select *会导致JVM溢出的情况)----工具类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class DbDataGenerate {
private String driver;
private String url;
private String username;
private String password;
private String sql;
private int offset;
private int skipLineCount;
private Connection conn;
private PreparedStatement pstmt;
private DbDataGenerate parentDbGen = null;
public DbDataGenerate(String driver, String url, String username,
String password, String sql, int offset, int skipLineCount) throws ClassNotFoundException, SQLException {
super();
this.driver = driver;
this.url = url;
this.username = username;
this.password = password;
this.sql = sql;
this.offset = offset;
this.skipLineCount = skipLineCount;
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
pstmt = (PreparedStatement) conn.prepareStatement(sql + " limit ?," + skipLineCount);
}
public void close(){
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ArrayList<Object> getNextSet(Object obj){
MemberAccess memberAccess = new MemberAccess();
memberAccess.init(obj);
Object object = null;
ResultSet resSet = null;
ArrayList<Object> resList = new ArrayList<Object>();
try {
pstmt.setInt(1, offset);
resSet = pstmt.executeQuery();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(resSet != null){
int lineCount = 0;
try {
while(resSet.next()){
++lineCount;
try {
object = obj.getClass().newInstance();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
break;
}
for(String field: memberAccess.getFields()){
MemberAccess.Write(object, field, resSet.getObject(field));
}
resList.add(object);
}
resSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
offset += lineCount;
}
return resList;
}
public static void main(String[] args) throws ClassNotFoundException, SQLException {
DbDataGenerate dbDataGenerate = new DbDataGenerate("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/test","root","security","SELECT id id,title title,abstract abst FROM paper",0,1000);
ArrayList<Object> list = null;
while((list = dbDataGenerate.getNextSet(new Article())) != null){
for(Object obj: list)
System.out.println(obj.toString());
}
dbDataGenerate.close();
}
}
package org.focus.util;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Set;
import org.focus.data.Article;
public class MemberAccess {
private HashMap<String, Object> fieldnameToValue;
private static HashMap<String,Boolean> fieldForSearch = new HashMap<String,Boolean>();
private static HashMap<String, Float> fieldBoosts = new HashMap<String, Float>();
public MemberAccess() {
fieldnameToValue = new HashMap<String, Object>();
}
public void init(Object obj) {
fieldnameToValue.clear();
Field[] fields = obj.getClass().getDeclaredFields();
if (fields != null) {
boolean defaultBoost = false,defaultSearch = false;
if(fieldBoosts.size() == 0)
defaultBoost = true;
if(fieldForSearch.size() == 0)
defaultSearch = true;
try {
for (Field field : fields) {
field.setAccessible(true);
fieldnameToValue.put(field.getName(), field.get(obj));
if(defaultBoost)
fieldBoosts.put(field.getName(), 1.0f);
if(defaultSearch && field.get(obj) instanceof String){
fieldForSearch.put(field.getName(), true);
}
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void Write(Object obj,String fieldname,Object value){
Field[] fields = obj.getClass().getDeclaredFields();
if (fields != null) {
try{
for (Field field : fields){
if(field.getName().compareTo(fieldname) == 0){
field.setAccessible(true);
field.set(obj, value);
break;
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
public static Class<?> GetFieldType(Object obj,String fieldname){
Field[] fields = obj.getClass().getDeclaredFields();
if (fields != null) {
try{
for (Field field : fields){
if(field.getName().compareTo(fieldname) == 0){
return field.getType();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
return null;
}
public static void clearBoostMap(){
fieldBoosts.clear();
}
public static void setBoost(String fieldname,float boost){
fieldBoosts.put(fieldname, boost);
}
public static float getBoost(String fieldname){
return fieldBoosts.get(fieldname);
}
public static void setNeedSearch(String fieldname,boolean value){
fieldForSearch.put(fieldname, value);
}
public static boolean NeedSearch(String fieldname){
return fieldForSearch.get(fieldname) == null ? false : fieldForSearch.get(fieldname);
}
public Object getFieldValue(String fieldname){
return fieldnameToValue.get(fieldname);
}
public Set<String> getFields(){
return fieldnameToValue.keySet();
}
public HashMap<String, Object> getFieldnameToValue() {
return fieldnameToValue;
}
public static void main(String[] args) {
Article art = new Article(10,"h","h");
MemberAccess macs = new MemberAccess();
macs.init(art);
System.out.println(macs.getFieldnameToValue());
System.out.println(macs.GetFieldType(art, "title").equals(String.class));
}
}
初步实现,没有细细考究类的性能,要想做好的话,我考虑需要借助数据库连接池工具,这样数据库连接就可以共享了。