博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA WEB新进展
阅读量:4961 次
发布时间:2019-06-12

本文共 5771 字,大约阅读时间需要 19 分钟。

哈哈,终于搞来页面来了,丑是丑,但是好的进展。

高手的思路,自己用库建连接池,确实利害。

最关键的一个HELPER代码:

package org.smart4j.chapter2.helper;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.Properties;import org.apache.commons.dbcp2.BasicDataSource;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.MapListHandler;import org.apache.log4j.helpers.ThreadLocalMap;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.BeanListHandler;import org.smart4j.chapter2.util.CollectionUtil;import org.smart4j.chapter2.util.PropsUtil;/** * Created by sahara on 2016/3/16. */public final class DatabaseHelper {    private static final Logger LOGGER = LoggerFactory.getLogger((DatabaseHelper.class));    private static final ThreadLocal
CONNECTION_HOLDER ; private static final QueryRunner QUERY_RUNNER; private static final BasicDataSource DATA_SOURCE; static { CONNECTION_HOLDER = new ThreadLocal
(); QUERY_RUNNER = new QueryRunner(); Properties conf = PropsUtil.loadProps("config.properties"); String driver = conf.getProperty("jdbc.driver"); String url = conf.getProperty("jdbc.url"); String username = conf.getProperty("jdbc.username"); String password = conf.getProperty("jdbc.password"); DATA_SOURCE = new BasicDataSource(); DATA_SOURCE.setDriverClassName(driver); DATA_SOURCE.setUrl(url); DATA_SOURCE.setUsername(username); DATA_SOURCE.setPassword(password); } public static Connection getConnection() { Connection conn = CONNECTION_HOLDER.get(); if (conn == null) { try { conn = DATA_SOURCE.getConnection(); }catch (SQLException e) { LOGGER.error("get connection failure", e); throw new RuntimeException(e); }finally { CONNECTION_HOLDER.set(conn); } } return conn; } public static
List
queryEntityList(Class
entityClass, String sql, Object...params) { List
entityList; try { Connection conn = getConnection(); entityList = QUERY_RUNNER.query(conn, sql, new BeanListHandler
(entityClass), params); } catch (SQLException e) { LOGGER.error("query entity list failure", e); throw new RuntimeException(e); } return entityList; } public static
T queryEntity(Class
entityClass, String sql, Object...params) { T entity; try { Connection conn = getConnection(); entity = QUERY_RUNNER.query(conn, sql, new BeanHandler
(entityClass), params); } catch (SQLException e) { LOGGER.error("query entity failure", e); throw new RuntimeException(e); } return entity; } public static List
> executeQuery(String sql, Object...params) { List
> result; try { Connection conn = getConnection(); result = QUERY_RUNNER.query(conn, sql, new MapListHandler(), params); } catch (Exception e) { LOGGER.error("execute query failure", e); throw new RuntimeException(e); } return result; } public static int executeUpdate(String sql, Object...params) { int rows = 0; try { Connection conn = getConnection(); rows = QUERY_RUNNER.update(conn, sql, params); } catch (SQLException e) { LOGGER.error("execute update failure", e); throw new RuntimeException(e); } return rows; } public static
boolean insertEntity(Class
entityClass, Map
fieldMap) { if (CollectionUtil.isEmpty(fieldMap)) { LOGGER.error("can not insert entity: fieldMap is empty"); return false; } String sql = "INSERT INTO " + getTableName(entityClass); StringBuilder columns = new StringBuilder("("); StringBuilder values = new StringBuilder("("); for (String fieldName : fieldMap.keySet()) { columns.append(fieldName).append(", "); values.append("?, "); } columns.replace(columns.lastIndexOf(", "), columns.length(), ")"); values.replace(values.lastIndexOf(", "), values.length(), ")"); sql += columns + " VALUES " + values; Object[] params = fieldMap.values().toArray(); return executeUpdate(sql, params) == 1; } public static
boolean updateEntity(Class
entityClass, long id, Map
fieldMap) { if (CollectionUtil.isEmpty(fieldMap)) { LOGGER.error("can not update entity: fieldMap is empty"); return false; } String sql = "UPDATE " + getTableName(entityClass) + " SET "; StringBuilder columns = new StringBuilder(); for (String fieldName : fieldMap.keySet()) { columns.append(fieldName).append("=?, "); } sql += columns.substring(0, columns.lastIndexOf(", ")) + " WHERE id=?"; List
paramList = new ArrayList(); paramList.addAll(fieldMap.values()); paramList.add(id); Object[] params = paramList.toArray(); return executeUpdate(sql, params) == 1; } public static
boolean deleteEntity(Class
entityClass, long id) { String sql = "DELETE FROM " + getTableName(entityClass) + " WHERE id=?"; return executeUpdate(sql, id) == 1; } public static String getTableName(Class
entityClass) { return entityClass.getSimpleName(); } public static void executeSqlFile(String filePath) { InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); try { String sql; while ((sql=reader.readLine()) != null) { DatabaseHelper.executeUpdate(sql); } } catch (Exception e) { LOGGER.error("execute sql file failure", e); throw new RuntimeException(e); } }}

嘿嘿,,可能你觉得烂,但我认为打个包是必要的,毕竟是我全手打啊。。

 http://files.cnblogs.com/files/aguncn/chapter2.rar

 

转载于:https://www.cnblogs.com/aguncn/p/5289652.html

你可能感兴趣的文章
linu学习第二天:文件系统相关操作
查看>>
执行了的程序,才是你的程序.
查看>>
国内使用Google Maps JavaScript API
查看>>
在AxureRP8中实现广告文字滚动效果
查看>>
javaScript 事件流---冒泡 && 捕获
查看>>
原型和继承 constructor、prototype、__proto__
查看>>
jQuery获取CSS样式中的颜色值的问题
查看>>
struts2.x + Tiles2.x读取多个xml 配置文件
查看>>
Sqlite文件在ubunut的查看
查看>>
表单校验之datatype
查看>>
python第六篇文件处理类型
查看>>
kettle 数据库连接失败
查看>>
ListView失去焦点选中行不能高亮显示的问题解决
查看>>
# jsp及servlet学习笔记
查看>>
Kconfig详解
查看>>
关于linux发行版i386/i686/x86-64/的区别
查看>>
SPListTemplateType 枚举 (Microsoft.SharePoint) 创建列表时的ListTemplate Type属性
查看>>
利用private font改变PDF文件的字体
查看>>
前端知识
查看>>
yii登录
查看>>