博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从数据库中获取数据(数据量太大,select *会导致JVM溢出的情况)----工具类
阅读量:5251 次
发布时间:2019-06-14

本文共 5376 字,大约阅读时间需要 17 分钟。

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<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));
}
}

初步实现,没有细细考究类的性能,要想做好的话,我考虑需要借助数据库连接池工具,这样数据库连接就可以共享了。

转载于:https://www.cnblogs.com/zz-boy/archive/2012/08/29/2662019.html

你可能感兴趣的文章
乌镇互联网大会——中国最成功的商人花一辈子才悟到的道理(转自知乎)
查看>>
主流开源协议树——区分各种开源许可证
查看>>
numpy的基本操作
查看>>
Python中获取路径的方法区别
查看>>
Pycharm连接MySQL步骤及注意点
查看>>
栅格系统实现原理
查看>>
Lode's Computer Graphics Tutorial Image Filtering
查看>>
一、win10 64位搭建apache2.4+php 7.x环境
查看>>
2019杭电多校第四场hdu6623 Minimal Power of Prime
查看>>
决策树与随机森林Adaboost算法
查看>>
最近学Shader有点心得,做了一个SLG画线模块
查看>>
《从零开始学Swift》学习笔记(Day 20)——函数中参数的传递引用
查看>>
系统管理员资源大全,学习学习学习(转载)
查看>>
select 相关 获取当前项以及option js选定
查看>>
java小记
查看>>
3902-luogu 最长不下降子区间
查看>>
unique
查看>>
linux数据库copy方法
查看>>
旺财速啃H5框架之Bootstrap(三)
查看>>
pycharm 主题修改
查看>>