1 package cn.com.ecinc.ecoa.todo; 2 3 import java.io.InputStream; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.PreparedStatement; 7 import java.sql.ResultSet; 8 import java.sql.ResultSetMetaData; 9 import java.sql.SQLException; 10 import java.util.ArrayList; 11 import java.util.HashMap; 12 import java.util.List; 13 import java.util.Map; 14 import java.util.Properties; 15 16 17 18 public class DBUtil { 19 20 21 22 // 表示定义数据库的用户名 23 private static String USERNAME ; 24 25 // 定义数据库的密码 26 private static String PASSWORD; 27 28 // 定义数据库的驱动信息 29 private static String DRIVER; 30 31 // 定义访问数据库的地址 32 private static String URL; 33 34 // 定义数据库的链接 35 private Connection connection; 36 37 // 定义sql语句的执行对象 38 private PreparedStatement pstmt; 39 40 // 定义查询返回的结果集合 41 private ResultSet resultSet; 42 43 static{ 44 //加载数据库配置信息,并给相关的属性赋值 45 loadConfig(); 46 } 47 48 /** 49 * 加载数据库配置信息,并给相关的属性赋值 50 */ 51 public static void loadConfig() { 52 try { 53 InputStream inStream = DBUtil.class 54 .getResourceAsStream("/application.properties"); 55 Properties prop = new Properties(); 56 prop.load(inStream); 57 USERNAME = prop.getProperty("jdbc.username_nw"); 58 PASSWORD = prop.getProperty("jdbc.password_nw"); 59 DRIVER= prop.getProperty("jdbc.driverClassName"); 60 URL = prop.getProperty("jdbc.url_nw"); 61 } catch (Exception e) { 62 throw new RuntimeException("读取数据库配置文件异常!", e); 63 } 64 } 65 66 /** 67 * 获取数据库连接 68 * 69 * @return 数据库连接 70 */ 71 public Connection getConnection() { 72 try { 73 Class.forName(DRIVER); // 注册驱动 74 connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); // 获取连接 75 } catch (Exception e) { 76 throw new RuntimeException("get connection error!", e); 77 } 78 return connection; 79 } 80 81 public Connection getConnection(String URL, String USERNAME, String PASSWORD) { 82 try { 83 Class.forName(DRIVER); // 注册驱动 84 connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); // 获取连接 85 } catch (Exception e) { 86 throw new RuntimeException("get connection error!", e); 87 } 88 return connection; 89 } 90 91 /** 92 * 执行更新操作 93 * 94 * @param sql 95 * sql语句 96 * @param params 97 * 执行参数 98 * @return 执行结果 99 * @throws SQLException100 */101 private boolean updateByPreparedStatement(String sql, List params)102 throws SQLException {103 boolean flag = false;104 int result = -1;// 表示当用户执行添加删除和修改的时候所影响数据库的行数105 pstmt = connection.prepareStatement(sql);106 int index = 1;107 // 填充sql语句中的占位符108 if (params != null && !params.isEmpty()) {109 for (int i = 0; i < params.size(); i++) {110 pstmt.setObject(index++, params.get(i));111 }112 }113 result = pstmt.executeUpdate();114 flag = result > 0 ? true : false;115 return flag;116 }117 118 119 120 121 /**122 * 执行查询操作123 * 124 * @param sql125 * sql语句126 * @param params127 * 执行参数128 * @return129 * @throws SQLException130 */131 public List
1 String sql =" SELECT COUNT(WF_TASK_ITEM.ID) FROM WF_TASK_ITEM left join WF_INSTANCE on WF_TASK_ITEM.WF_INSTANCE_ID=WF_INSTANCE.ID " +2 " WHERE OWNER_ACCOUNT='"+SystemUtil.getLoginObject().getPersonAccount()+"' AND WF_TASK_ITEM.STATUS='0' AND ITEM_TYPE='TODO' and (WF_INSTANCE.MODULE='shouwen' or WF_INSTANCE.MODULE='fawen' or WF_INSTANCE.MODULE='hytz' or WF_INSTANCE.MODULE='dzgg' or WF_INSTANCE.MODULE='qsbg')";3 String resurn = "";4 Listlist = DBUtil.excuteSql(sql);5 if(list!=null && list.size()>0){6 resurn = "("+list.get(0)+")";7 }