项目中老是遇到数据库异常关闭的情况,真烦,
想用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的更多相关文章

  1. java中汉字自动转换成拼音

    java中汉字自动转换成拼音 1.需要下载jar包 pinyin4j.2.5.0.jar ,加入到WEB-INF下的lib里边,右键add to bulid path. 2.[代码]PinYinUti ...

  2. Java中windows路径转换成linux路径等工具类

    项目中发现别人写好的操作系统相关的工具类: 我总结的类似相关博客:http://www.cnblogs.com/DreamDrive/p/4289860.html import java.net.In ...

  3. Java中的List转换成JSON报错(五)

    1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/beanu ...

  4. Java中的List转换成JSON报错(四)

    1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/colle ...

  5. Java中的List转换成JSON报错(二)

    1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/loggi ...

  6. Java中的List转换成JSON报错(一)

    1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/ ...

  7. Java中的List转换成JSON报错(三)

    1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher a ...

  8. java中的时区转换

    目录 java中的时区转换 一.时区的说明 二.时间的表示 三.时间戳 四.Date类和时间戳 五.java中的时区转换 java中的时区转换 一.时区的说明 地球表面按经线从东到西,被划成一个个区域 ...

  9. Java将ip字符串转换成整数的代码

    下面代码是关于Java将ip字符串转换成整数的代码,希望对各位有较大用途. public class IpUtil { public static int Ip2Int(String strIp){ ...

随机推荐

  1. 达内TTS6.0课件oop_day04

  2. dataset 用法(1)

    DataSet是表和列结构在内存中的表示方式,DataSet支持多表.表间关系.数据约束等,和关系数据库的模型基本一致.(本质上是微型的数据库.包含一组DataTable对象和DataTable之间的 ...

  3. Server.MapPath(string sFilePath) 报未将对象引用到实例异常

    System.Web.HttpContext.Current.Server.MapPath(string sfilePath)将虚拟路径转换成物理路径.这个必须在aspx或者MVC中Action调用才 ...

  4. PHP去除Notice警告提示

    最近刚接触PHP,开发过程中可能会遇到Notice: Use of undefined ……这样的警告提示,可能是代码写的不太规范, 有两种解决途径:关闭 PHP 提示的方法, 搜索php.ini:e ...

  5. 转发年浩大神的spfa算法

    http://www.cnblogs.com/superxuezhazha/p/5426624.html #include<iostream> #include<stdio.h> ...

  6. localhost简介、localhost与 127.0.0.1 及 本机IP 的区别

    localhost是什么意思? 相信有人会说是本地ip,曾有人说,用127.0.0.1比localhost好,可以减少一次解析. 看来这个入门问题还有人不清楚,其实这两者是有区别的. localhos ...

  7. 几个SQL

    select sum(`value`) from testtable where value != 'error' AND type ='b' in (select DISTINCT(type) fr ...

  8. 小鱼提问2 属性访问器中get,set再用public修饰行吗,private呢?

    /// <summary> /// 是否有一个用户正在连接服务器中 /// </summary> public bool IsConnectting { get { retur ...

  9. BZOJ 1565: [NOI2009]植物大战僵尸( 最小割 )

    先拓扑排序搞出合法的, 然后就是最大权闭合图模型了.... --------------------------------------------------------------------- ...

  10. Spring总结——控制反转,注入(配置和注解两种方式)

    一.Spring的容器: 1.什么是控制反转:传统的方法,当某个java对象A需要调用对象B时,是由调用者(对象A)通过new关键字来创建对象B的(也可以说类A依赖类B),而在Spring中,则是由s ...