JAVA中把ResultSet转换成LIST
项目中老是遇到数据库异常关闭的情况,真烦,
想用hibernate呢,那个玩意儿又太笨重,感慨C#和PHP的舒适方便性,模拟TP写了个数据处理层,将就用着先
代码里有很多项目中的东西,不要直接COPY了。。。了解实现方法就行了 /**
* @模拟TP数据操作基类框架
* @author 牛牛 Q 184377367
* @20131218
*/ package Model; import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import xqlbsw.DB;
import xqlbsw.DataFactory;
import lib.DBC; public class ModelBase { public String table;
public DBC db = null;
public String where = "";
public String Field = ""; public Boolean Delete() throws Exception { String dbChar = "delete " + this.table + " where " + this.where;
DataFactory.GDO().Update(dbChar);
DataFactory.GDO().free();
return true; } public ModelBase table(String t) {
this.table = t;
return this;
} public ModelBase Field(String s) {
this.Field = s;
return this;
} public ModelBase Where(String s) {
this.where = s;
return this;
} public List Select() throws SQLException { if (this.Field == "") {
this.Field = "*";
}
String dbChar = "select " + this.Field + " from " + this.table;
if (this.where != "") {
dbChar += " where " + this.where + "";
} List list = this.toList(DataFactory.GDO().getCrmResult(dbChar));
DataFactory.GDO().free();
return list; } public List toList(ResultSet rs) {
List list = new ArrayList();
try {
// 获取数据库表结构
ResultSetMetaData meta = rs.getMetaData();
Object obj = null;
String clsName = this.getClass().getName();
String aryClassName[] = clsName.split("\\."); while (rs.next()) {
// 获取formbean实例对象
obj = Class.forName(
"Domain." + aryClassName[(aryClassName.length) - 1])
.newInstance();
// 循环获取指定行的每一列的信息
for (int i = 1; i <= meta.getColumnCount(); i++) {
// 当前列名
String colName = meta.getColumnName(i);
// 将列名第一个字母大写(为什么加+""呢?为了把char类型转换为String类型。replace的参数是String类型。)
colName = colName.replace(colName.charAt(0) + "",
new String(colName.charAt(0) + "").toUpperCase());
// 设置方法名
String methodName = "set" + colName;
// 获取当前位置的值,返回Object类型
Object value = rs.getObject(i);
// 利用反射机制,生成setXX()方法的Method对象并执行该setXX()方法。
Method method = obj.getClass().getMethod(methodName,
value.getClass());
method.invoke(obj, value);
}
list.add(obj);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
return list;
}
} }
JAVA中把ResultSet转换成LIST的更多相关文章
- java中汉字自动转换成拼音
java中汉字自动转换成拼音 1.需要下载jar包 pinyin4j.2.5.0.jar ,加入到WEB-INF下的lib里边,右键add to bulid path. 2.[代码]PinYinUti ...
- Java中windows路径转换成linux路径等工具类
项目中发现别人写好的操作系统相关的工具类: 我总结的类似相关博客:http://www.cnblogs.com/DreamDrive/p/4289860.html import java.net.In ...
- Java中的List转换成JSON报错(五)
1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/beanu ...
- Java中的List转换成JSON报错(四)
1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/colle ...
- Java中的List转换成JSON报错(二)
1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/loggi ...
- Java中的List转换成JSON报错(一)
1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/ ...
- Java中的List转换成JSON报错(三)
1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher a ...
- java中的时区转换
目录 java中的时区转换 一.时区的说明 二.时间的表示 三.时间戳 四.Date类和时间戳 五.java中的时区转换 java中的时区转换 一.时区的说明 地球表面按经线从东到西,被划成一个个区域 ...
- Java将ip字符串转换成整数的代码
下面代码是关于Java将ip字符串转换成整数的代码,希望对各位有较大用途. public class IpUtil { public static int Ip2Int(String strIp){ ...
随机推荐
- collection系列用法-namedtuple()
namedtuple() 参考文章地址:http://www.cnblogs.com/herbert/p/3468294.html namedtuple是继承自tuple的子类.namedtuple和 ...
- golang 之 defer(统计函数执行时间)
package main import ( "fmt" "time" ) func sum(a ...int) int { defer trace(" ...
- <转>ASP.NET学习笔记之在ASP.NET MVC中使用DropDownList
看到一篇关于dropdownlist的用法很好的阐述,比较清楚,留着,防止以后自己不记得,还可以瞅瞅. 在ASP.NET MVC中,尽管我们可以直接在页面中编写HTML控件,并绑定控件的属性,但更方便 ...
- POJ 3356.AGTC
问题简述: 输入两个序列x和y,分别执行下列三个步骤,将序列x转化为y (1)插入:(2)删除:(3)替换: 要求输出最小操作数. 原题链接:http://poj.org/problem?id=335 ...
- 在Eclipse中使用Maven构建SpringMVC项目
环境搭建 安装JDK, Eclipse, Tomcat等 – 请参考网上常见攻略. 安装Maven: 下载需要的Maven 版本( http://maven.apache.org/download.c ...
- linux杂记(六)档案权限
[KANO@kelvin ~]$ ls -al 总用量 drwx------. KANO KANO 10月 : . drwxr-xr-x. root root 9月 : .. drwxrwxr-x. ...
- Streams Studio配置Build options
Defining build options You can change the build options of the internal builder for building an SPL ...
- 初识C(1)----与C基本无关的开篇
1.啥叫编程 编程乃编写程序的简称,所以要想知道啥叫编程,首先要清楚什么是程序(Program). 普及一点计算机小知识:从根本上说,计算机是由数字电路组成的运算机器,处理的数字也仅限于0和1组成的数 ...
- C# 基础中有关术语理解
一.栈vs堆 深入理解堆栈.堆在内存中的实现 二.Socket 深入探析c# Socket 三.多线程 c# 多线程 --Mutex(互斥锁)
- 解决android TextView多行文本(超过3行)使用ellipsize属性无效问题
布局文件中的TextView属性 <TextView android:id="@+id/businesscardsingle_content_abstract" androi ...