前言:jdbc写的繁琐,mybatis要映射,hiberante配置太烦,好吧,就简单写个增删改查的jdbcUtil,以供学习,可以学到类的加载顺序,ResourceBundle加载资源文件,数据库三大核心接口,类的反射,泛型(特别是泛型方法),可变形参的使用,以及集合中的List,ArrayList等。当然还有很多不足,比如每次程序拿连接都要new,这样就给系统加大了负担,没有事务,没有dataSource等等,望大神补充。。。
util类的代码如下:
package com.yanhui.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
/**
* 数据库连接
* @author colorbin
*
*/
public class _jdbcUtil {
private static String url = null;
private static String user = null;
private static String password = null;
private static Connection conn;
public static void main(String[] args) throws Exception {
System.out.println(getConnection());
}
private _jdbcUtil(){}
static {
try{
ResourceBundle res = ResourceBundle.getBundle("config");
Class.forName(res.getString("driverName"));
url = res.getString("url");
user = res.getString("user");
password = res.getString("password");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
if(null == conn) {
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
}
return conn;
}
public static void closeResource(ResultSet rs, Statement st, Connection conn){
if(null != rs) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if(null != st) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if(null != conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
}
}
}
/**
* 从数据库取得一个
*
* @param sql 执行的sql语句
* @param row 对象赋值方法
* @param params 填充sql中的?
* @return
*/
public static Object getOne(String sql, RowMapper row, Object... params){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Object obj = null;
try {
conn = _jdbcUtil.getConnection();
ps = conn.prepareStatement(sql);
if(params != null){
for (int i = 1; i <= params.length; i++) {
ps.setObject(i, params[i - 1]);
}
}
rs = ps.executeQuery();
if (rs.next()) {
obj = row.mapRow(rs);
}
return obj;
} catch (SQLException e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
_jdbcUtil.closeResource(rs, ps, conn);
}
}
/**
* 取出所有sql语句查询的结果
*
* @param sql sql语句
* @param row 对象赋值方法
* @param params 用来填充sql中的?
* @return
*/
@SuppressWarnings("unchecked")
public static <T> List<T> getSome(String sql, RowMapper row, Object... params){
List<T> arrays = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
T obj = null;
try {
conn = _jdbcUtil.getConnection();
ps = conn.prepareStatement(sql);
if(params != null){
for (int i = 1; i <= params.length; i++) {
ps.setObject(i, params[i - 1]);
}
}
rs = ps.executeQuery();
arrays = new ArrayList<T>();
while (rs.next()) {
obj = (T) row.mapRow(rs);
arrays.add(obj);
}
} catch (SQLException e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
_jdbcUtil.closeResource(rs, ps, conn);
}
return arrays;
}
/**
* 执行更新操作
* @param sql 执行的sql语句
* @param params 用来填充sql语句中的?(若没有参数需要给null)
*/
public static void update(String sql, Object... params){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = _jdbcUtil.getConnection();
conn.setAutoCommit(false);
ps = conn.prepareStatement(sql);
if(params != null){
for (int i = 1; i <= params.length; i++) {
ps.setObject(i, params[i - 1]);
}
}
ps.executeUpdate();
conn.commit();
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
throw new RuntimeException(e1.getMessage(), e1);
}
throw new RuntimeException(e.getMessage(), e);
} finally {
_jdbcUtil.closeResource(rs, ps, conn);
}
}
}
源码下载地址:
链接:http://pan.baidu.com/s/1hrGyJkC 密码:yrma
别怕太快乐,别怕失去jdbc~~ 如果jdbc说用我没有如果 错过就过你是不是会难过 ~~
全CSDN的丰(好)色(se)博客,这里有书本看不到的Java技术,电影院禁播的电影,欢迎关注QQ群494808400