简单的jdbc封装


工作中经常用到jdbc,嫌每次创建连接麻烦。自己简单的封装了下,也没有测试,不知道能不能用。



package com.geap.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.sql.RowSet;
import javax.sql.rowset.CachedRowSet;

import com.sun.rowset.CachedRowSetImpl; //com.sun.rowset.jar包


/**
* jdbc工具类,提供了三个方法
* <ul>
* <li>execute(sql)</li>
* <li>executeQuery(sql)</li>
* <li>executeUpdate(sql)</li>
* </ul>
* @author QiuLu
*/
public class JdbcUtil {

/**
* 查询
* @param sql sql语句
* @return RowSet 离线结果集(CachedRowSet接口,CachedRowSetImpl实现)
* @throws Exception
*/
public static RowSet executeQuery(String sql) throws Exception{

Context ctx = null;
DataSource ds = null;
Connection cn = null;
Statement st = null;
ResultSet rs = null;
CachedRowSet crs = null;

try {
ctx = new InitialContext();
crs = new CachedRowSetImpl();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/OracleDS");
cn = ds.getConnection();
st = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = st.executeQuery(sql);
crs.populate(rs);
return crs;

} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if(rs != null)
try {
rs.close();
} catch ( Exception e) {
e.printStackTrace();
}
if(st != null)
try {
st.close();
} catch ( Exception e) {
e.printStackTrace();
}
if(cn != null)
try {
cn.close();
} catch ( Exception e) {
e.printStackTrace();
}
if(ctx != null)
try {
ctx.close();
} catch ( Exception e) {
e.printStackTrace();
}
}
}

/**
* 执行sql
* @param sql sql语句
* @return 如果第一个结果为 ResultSet 对象,则返回 true;如果其为更新计数或者不存在任何结果,则返回 false
* @throws SQLException
*/
public static boolean execute(String sql) throws SQLException{
Context ctx = null;
DataSource ds = null;
Connection cn = null;
Statement st = null;
ResultSet rs = null;

try {
ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/OracleDS");
cn = ds.getConnection();
st = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
return st.execute(sql);

} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if(rs != null)
try {
rs.close();
} catch ( Exception e) {
e.printStackTrace();
}
if(st != null)
try {
st.close();
} catch ( Exception e) {
e.printStackTrace();
}
if(cn != null)
try {
cn.close();
} catch ( Exception e) {
e.printStackTrace();
}
if(ctx != null)
try {
ctx.close();
} catch ( Exception e) {
e.printStackTrace();
}
}
}

/**
* 执行更新
* @param sql sql语句
* @return (1) 对于 SQL 数据操作语言 (DML) 语句,返回行计数 (2) 对于什么都不返回的 SQL 语句,返回 0
* @throws SQLException
*/
public static int executeUpdate(String sql) throws SQLException{
Context ctx = null;
DataSource ds = null;
Connection cn = null;
Statement st = null;
ResultSet rs = null;

try {
ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/OracleDS");
cn = ds.getConnection();
st = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
return st.executeUpdate(sql);

} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if(rs != null)
try {
rs.close();
} catch ( Exception e) {
e.printStackTrace();
}
if(st != null)
try {
st.close();
} catch ( Exception e) {
e.printStackTrace();
}
if(cn != null)
try {
cn.close();
} catch ( Exception e) {
e.printStackTrace();
}
if(ctx != null)
try {
ctx.close();
} catch ( Exception e) {
e.printStackTrace();
}
}
}

}




// 调用JdbcUtil.executeQuery()

public List getList() {
RowSet crs = null;
List<E> list = new ArrayList<E>();
try {
crs = JdbcUtil.executeQuery("select * from user");
while(crs.next()){
// todo
list.add();
}
return list;
} catch (Exception e) {
e.printStackTrace();
} finally{
if(crs != null)
try {
crs.close();
} catch ( Exception e) {
e.printStackTrace();
}
}
return null;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值