public static List queryList(String preSql, String className,

Connection con, int offset, int pagelength) throws PortalException

{

List beanbasket = new ArrayList();

String[] columns = null;

if (con == null)

{

con = getConnection();

}

//查询出的结果集

ResultSet rs = null;

PreparedStatement pstmt = null;

try

{

pstmt = con.prepareStatement(preSql);

if (pagelength != 0)

{

int endnum = offset+pagelength;

int stratnum = offset+1;

pstmt.setInt(1, endnum);

pstmt.setInt(2, stratnum);

}

LogFactory.debug("will query the data!!");

rs = pstmt.executeQuery();

LogFactory.debug("query the data success!!!");

ResultSetMetaData metedata = rs.getMetaData();

//获取查询出的字段总数

int i = metedata.getColumnCount();

//获取数据库表中所有的字段名

columns = new String[i];

for (int j = 1; j < i+1; j++)

{

columns[j-1] = metedata.getColumnName(j);

}

Class clazz = Class.forName(className);

Field[] fields = clazz.getDeclaredFields();

LogFactory.debug("the fields length is:"

+ fields.length);

while (rs.next())

{

Object obj = clazz.newInstance();

for (Field f : fields)

{

//如果该字段在数据库中有存储

if(isContainField(f.getName(),columns)){

f.setAccessible(true);

String strType = f.getType().toString();

if (strType.indexOf("Long") != -1)

{

f.set(obj, rs.getLong(f.getName()));

}

else if (strType.indexOf("Integer") != -1

|| strType.indexOf("int") != -1)

{

f.set(obj, rs.getInt(f.getName()));

}

else if (strType.indexOf("String") != -1)

{

String s = rs.getString(f.getName());

if(s!=null&&!s.equals("null"))

{

s =s.trim();

}

else

{

s = "";

}

f.set(obj, s);

}

else if (strType.indexOf("Date") != -1)

{

f.set(obj, rs.getDate(f.getName()));

}

else if (strType.indexOf("Double") != -1)

{

f.set(obj, rs.getDouble(f.getName()));

}

else if (strType.indexOf("Float") != -1)

{

f.set(obj, rs.getFloat(f.getName()));

}

else

{

LogFactory.debug(

"this data type not support yet!");

LogFactory.debug("this date type is"

+ f.getName());

}

}

beanbasket.add(obj);

}

}

catch (Exception ex)

{

}

LogFactory.info("Exit DBProxy.querylist()");

return beanbasket;

}

分享到:

2011-10-27 21:28

浏览 3596

评论

jdbc封装mysql_实用JDBC数据库查询封装相关推荐

  1. jdbc存储过程mysql_使用JDBC操作存储过程

    使用JDBC操作存储过程,可以借助于一个借口CallableStatement实现. 此时调用存储过程的sql语句为:{call procedure_name [(arg1),(arg2)]} 而Ca ...

  2. 数据库封装 sql server mysql_sqlserver数据库操作封装

    1 public classSQLServerDatabase2 {3 private static SqlConnection m_Connection = null;4 5 public SQLS ...

  3. 【JDBC】实现对JDBC 连接的简单封装

    package util;import java.sql.Connection; import java.sql.DriverManager;/**** 实现对JDBC 的封装* @author mq ...

  4. JDBC之用元数据将结果集封装为List对象

    JDBC的方法里面有着一套对数据库元数据操作的API.通过这些API我们可以获得数据库以及驱动等信息,也可以获得结果集的一些信息.而有些信息平时用的也比较少,这里就不一一介绍. 在这里主要给大家演示一 ...

  5. Java学习——JDBC之从导Jar包到封装

    书籍就像一盏神灯,它照亮人们最遥远.最黯淡的生活道路.--乌皮特 前言:下面内容是本人Java初学者的学习内容 前提准备:已经安装好mysql数据库,若没有请看Mysql8.0的下载安装 需要学习SQ ...

  6. 原始JDBC连接与封装后的JDBC

    JDBC连接数据库 操作步骤: 准备jar包mysql-connector-java-5.1.0-bin.jar 加载驱动Class.forName("com.mysql.jdbc.Driv ...

  7. jdbc mysql select_java连接mysql数据库并使用jdbc进行查询详解

    public Connection getCon() { //数据库连接名称 String username="root"; //数据库连接密码 String password=& ...

  8. DBUtils开源JDBC类库,对JDBC简单封装(作用是:简化编码工作量,同时不会影响程序的性能)...

    DBUtils:提高了程序的性能,编程更加简便 架包 mysql-connector-java-jar commons-dbcp-1.4jar commons-pool-1.5.5jar common ...

  9. JDBC数据库查询操作

    以下是按正常的数据库查询操作(没有做优化),代码如下: package com.test.main;import java.sql.Connection; import java.sql.Result ...

最新文章

  1. 解决:angularjs radio默认选中失效问题
  2. dede使用方法----如何自定义字段
  3. 网络编程学习笔记(获取所有网络接口)
  4. VTK:图片之MarkKeypoints
  5. 【小工匠聊Modbus】05-数据类型
  6. 面向对象设计思想-C语言
  7. linux扫描工具之nmap
  8. 收藏 eclipse 最常用的10个快捷键
  9. python collection 和 heapq 模块使用说明
  10. Android,App 常用图标尺寸规范
  11. HTML课题背景怎么写,课题研究的背景和意义怎么写
  12. java中png转索引图压缩图片
  13. 和合信诺牵手招行私人银行,构建智能合规知识引擎
  14. linux-一条命令,找出包含指定字符串的进程并杀死
  15. 新版PF使用说明文档
  16. 用STC12C5A60S2的P4接口点亮LED
  17. BootCamp Intel Mac上安装Windows全教程
  18. 关于web中的颜色表示方法,你知道多少?
  19. [梁山好汉说IT] 用实例来深入理解容器概念
  20. STM32F410xx与STM32F411xE时钟修改(标准库)

热门文章

  1. windows命令行下访问linux,Windows支持直接访问Linux子系统文件:你的下一台Linux何必是Linux...
  2. 教程:12、文件存储结构
  3. c语言 打砖块,打砖块
  4. C语言打印1000以内的完数
  5. afn原理 ios_iOS开发:AFN的基本使用
  6. opencv cv2 视频延迟
  7. oracle 12c java 驱动,oracle12c驱动jar包
  8. c语言中gets函数可以输入空格吗_C语言中printf和gets函数的实用技巧
  9. 【团队】 冲刺一(10/10)
  10. linux 查看日志信息--less命令