一、前言
定义SQLUtil语句工具类,实现sql语句execute执行插入、更新、删除dml操作,查询queryList数据库单列List数据,查询queryObject单记录对象数据及数据关闭close重载实现等,详情参见代码示例。
二、代码示例
import java.sql.Connection;@b@import java.sql.PreparedStatement;@b@import java.sql.ResultSet;@b@import java.sql.SQLException;@b@import java.sql.Statement;@b@import java.util.ArrayList;@b@import java.util.List;@b@@b@import javax.sql.DataSource;@b@@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@@b@public final class SQLUtil {@b@@b@ private static Log logger = LogFactory.getLog(SQLUtil.class);@b@@b@ /**@b@ * 执行插入、更新、删除操作@b@ * @b@ * @param dataSource@b@ * @param sql@b@ * @param params@b@ * @throws SQLException@b@ */@b@ public static int execute(DataSource dataSource, String sql, Object... params) throws SQLException {@b@@b@ logger.debug("start execute sql:" + sql);@b@ long startTime = System.currentTimeMillis();@b@ Connection conn = null;@b@ Statement stat = null;@b@ int updateCount = 0;@b@@b@ try {@b@ conn = dataSource.getConnection();@b@@b@ if (params == null || params.length == 0) {@b@@b@ stat = conn.createStatement();@b@ stat.execute(sql);@b@@b@ } else {@b@@b@ stat = conn.prepareStatement(sql);@b@@b@ PreparedStatement pstat = (PreparedStatement) stat;@b@@b@ int parameterIndex = 1;@b@ for (Object param : params) {@b@ pstat.setObject(parameterIndex, param);@b@ parameterIndex++;@b@ }@b@@b@ pstat.execute();@b@ }@b@@b@ updateCount = stat.getUpdateCount();@b@@b@ logger.debug("end execute sql:" + sql + " UpdateCount:" + updateCount + " "@b@ + (System.currentTimeMillis() - startTime) + "ms");@b@ } finally {@b@ close(stat);@b@ close(conn);@b@ }@b@@b@ return updateCount;@b@ }@b@@b@ /**@b@ * 查询数据库单列List数据@b@ * @b@ * @param dataSource@b@ * @param sql@b@ * @param params@b@ * @return@b@ * @throws SQLException@b@ */@b@ public static List<Object> queryList(DataSource dataSource, String sql, Object... params) throws SQLException {@b@ logger.debug("start execute sql:" + sql);@b@@b@ long startTime = System.currentTimeMillis();@b@@b@ Connection conn = null;@b@ Statement stat = null;@b@ List<Object> resultList = new ArrayList<Object>();@b@@b@ try {@b@@b@ conn = dataSource.getConnection();@b@ stat = conn.createStatement();@b@@b@ if (params == null || params.length == 0) {@b@@b@ stat = conn.createStatement();@b@ ResultSet rs = stat.executeQuery(sql);@b@@b@ int i = 1;@b@ while (rs.next()) {@b@ resultList.add(rs.getObject(i));@b@ i++;@b@ }@b@@b@ return resultList;@b@@b@ } else {@b@@b@ stat = conn.prepareStatement(sql);@b@@b@ PreparedStatement pstat = (PreparedStatement) stat;@b@@b@ int parameterIndex = 1;@b@ for (Object param : params) {@b@ pstat.setObject(parameterIndex, param);@b@ parameterIndex++;@b@ }@b@@b@ ResultSet rs = pstat.executeQuery();@b@@b@ int i = 1;@b@ while (rs.next()) {@b@ resultList.add(rs.getObject(i));@b@ i++;@b@ }@b@@b@ }@b@@b@ logger.debug("end query sql:" + sql + " Result:" + resultList + " "@b@ + (System.currentTimeMillis() - startTime) + "ms");@b@@b@ } finally {@b@ stat.close();@b@ conn.close();@b@ }@b@@b@ return resultList;@b@ }@b@@b@ public static Object queryObject(DataSource dataSource, String sql, Object... params) throws SQLException {@b@ List<Object> resultList = queryList(dataSource, sql, params);@b@@b@ if (resultList == null || resultList.size() == 0)@b@ return null;@b@@b@ return resultList.get(0);@b@@b@ }@b@@b@ public static void close(Connection con) {@b@ if (con != null)@b@ try {@b@ con.close();@b@ } catch (SQLException ex) {@b@ logger.debug("Could not close JDBC Connection", ex);@b@ } catch (Throwable ex) {@b@ logger.debug("Unexpected exception on closing JDBC Connection", ex);@b@ }@b@ }@b@@b@ public static void close(Statement stmt) {@b@ if (stmt != null)@b@ try {@b@ stmt.close();@b@ } catch (SQLException ex) {@b@ logger.trace("Could not close JDBC Statement", ex);@b@ } catch (Throwable ex) {@b@ logger.trace("Unexpected exception on closing JDBC Statement", ex);@b@ }@b@ }@b@@b@ public static void close(ResultSet rs) {@b@ if (rs != null)@b@ try {@b@ rs.close();@b@ } catch (SQLException ex) {@b@ logger.trace("Could not close JDBC ResultSet", ex);@b@ } catch (Throwable ex) {@b@ logger.trace("Unexpected exception on closing JDBC ResultSet", ex);@b@ }@b@ }@b@@b@}