java 捕获数据溢出_从数据库中获取数据(数据量太大,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 getNextSet(Object obj){

MemberAccess memberAccess = new MemberAccess();

memberAccess.init(obj);

Object object = null;

ResultSet resSet = null;

ArrayList resList = new ArrayList();

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 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 fieldnameToValue;

private static HashMap fieldForSearch = new HashMap();

private static HashMap fieldBoosts = new HashMap();

public MemberAccess() {

fieldnameToValue = new HashMap();

}

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 getFields(){

return fieldnameToValue.keySet();

}

public HashMap 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));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值