jdbc封装mysql_实用JDBC数据库查询封装
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数据库查询封装相关推荐
- jdbc存储过程mysql_使用JDBC操作存储过程
使用JDBC操作存储过程,可以借助于一个借口CallableStatement实现. 此时调用存储过程的sql语句为:{call procedure_name [(arg1),(arg2)]} 而Ca ...
- 数据库封装 sql server mysql_sqlserver数据库操作封装
1 public classSQLServerDatabase2 {3 private static SqlConnection m_Connection = null;4 5 public SQLS ...
- 【JDBC】实现对JDBC 连接的简单封装
package util;import java.sql.Connection; import java.sql.DriverManager;/**** 实现对JDBC 的封装* @author mq ...
- JDBC之用元数据将结果集封装为List对象
JDBC的方法里面有着一套对数据库元数据操作的API.通过这些API我们可以获得数据库以及驱动等信息,也可以获得结果集的一些信息.而有些信息平时用的也比较少,这里就不一一介绍. 在这里主要给大家演示一 ...
- Java学习——JDBC之从导Jar包到封装
书籍就像一盏神灯,它照亮人们最遥远.最黯淡的生活道路.--乌皮特 前言:下面内容是本人Java初学者的学习内容 前提准备:已经安装好mysql数据库,若没有请看Mysql8.0的下载安装 需要学习SQ ...
- 原始JDBC连接与封装后的JDBC
JDBC连接数据库 操作步骤: 准备jar包mysql-connector-java-5.1.0-bin.jar 加载驱动Class.forName("com.mysql.jdbc.Driv ...
- jdbc mysql select_java连接mysql数据库并使用jdbc进行查询详解
public Connection getCon() { //数据库连接名称 String username="root"; //数据库连接密码 String password=& ...
- DBUtils开源JDBC类库,对JDBC简单封装(作用是:简化编码工作量,同时不会影响程序的性能)...
DBUtils:提高了程序的性能,编程更加简便 架包 mysql-connector-java-jar commons-dbcp-1.4jar commons-pool-1.5.5jar common ...
- JDBC数据库查询操作
以下是按正常的数据库查询操作(没有做优化),代码如下: package com.test.main;import java.sql.Connection; import java.sql.Result ...
最新文章
- 解决:angularjs radio默认选中失效问题
- dede使用方法----如何自定义字段
- 网络编程学习笔记(获取所有网络接口)
- VTK:图片之MarkKeypoints
- 【小工匠聊Modbus】05-数据类型
- 面向对象设计思想-C语言
- linux扫描工具之nmap
- 收藏 eclipse 最常用的10个快捷键
- python collection 和 heapq 模块使用说明
- Android,App 常用图标尺寸规范
- HTML课题背景怎么写,课题研究的背景和意义怎么写
- java中png转索引图压缩图片
- 和合信诺牵手招行私人银行,构建智能合规知识引擎
- linux-一条命令,找出包含指定字符串的进程并杀死
- 新版PF使用说明文档
- 用STC12C5A60S2的P4接口点亮LED
- BootCamp Intel Mac上安装Windows全教程
- 关于web中的颜色表示方法,你知道多少?
- [梁山好汉说IT] 用实例来深入理解容器概念
- STM32F410xx与STM32F411xE时钟修改(标准库)
热门文章
- windows命令行下访问linux,Windows支持直接访问Linux子系统文件:你的下一台Linux何必是Linux...
- 教程:12、文件存储结构
- c语言 打砖块,打砖块
- C语言打印1000以内的完数
- afn原理 ios_iOS开发:AFN的基本使用
- opencv cv2 视频延迟
- oracle 12c java 驱动,oracle12c驱动jar包
- c语言中gets函数可以输入空格吗_C语言中printf和gets函数的实用技巧
- 【团队】 冲刺一(10/10)
- linux 查看日志信息--less命令