package com.utils; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class DBUtil { // 四大金刚 String dname = Utils.getPeoperties("dbname"); String driver = "com.mysql.jdbc.Driver";// 驱动名称 String url = "jdbc:mysql://"+Utils.getPeoperties("dburl")+":"+Utils.getPeoperties("dbport")+"/"+dname+"?useUnicode=false&characterEncoding=UTF-8&useOldAliasMetadataBehavior=true&autoReconnect=true&useSSL=false";// 连接 String username = Utils.getPeoperties("dbusername");// 用户名 String password = Utils.getPeoperties("dbpassword");// 密码 // 三剑客 Connection con = null;// 连接对象 PreparedStatement pstmt = null;// 语句对象 ResultSet rs = null;// 结果集对象 public DBUtil(String dbname){ if(dbname!=null&&dbname.trim().length()>0) this.dname = dbname; } public DBUtil(){ } /** * 获得连接对象 * * @return 连接对象 * @throws ClassNotFoundException * @throws SQLException */ public Connection getConnection() { try { Class.forName(driver); con = DriverManager.getConnection(url, username, password); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } /** * 关闭三剑客 * * @throws SQLException */ public void close(ResultSet rs, PreparedStatement pstmt, Connection con) { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (con != null) con.close(); } catch (SQLException e) { // TODO: handle exception e.printStackTrace(); } } /** * 执行更新 * * @param sql * 传入的预设的 sql语句 * @param params * 问号参数列表 * @return 影响行数 */ public int execUpdate(String sql, Object[] params) { try { this.getConnection();// 获得连接对象 this.pstmt = this.con.prepareStatement(sql);// 获得预设语句对象 if (params != null) { // 设置参数列表 for (int i = 0; i < params.length; i++) { // 因为问号参数的索引是从1开始,所以是i+1,将所有值都转为字符串形式,好让setObject成功运行 this.pstmt.setObject(i + 1, params[i] + ""); } } return this.pstmt.executeUpdate(); // 执行更新,并返回影响行数 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { this.close(this.rs, this.pstmt, this.con); } return 0; } /** * 执行查询sql,返回记录条数 * * @param sql * @param params * @return */ public int execSql(String sql, Object[] params) { try { this.getConnection();// 获得连接对象 this.pstmt = this.con.prepareStatement(sql);// 获得预设语句对象 if (params != null) { // 设置参数列表 for (int i = 0; i < params.length; i++) { // 因为问号参数的索引是从1开始,所以是i+1,将所有值都转为字符串形式,好让setObject成功运行 this.pstmt.setObject(i + 1, params[i] + ""); } } int i = 0; rs = this.pstmt.executeQuery(); while (rs.next()) { i++; } return i; // 执行更新,并返回影响行数 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { this.close(this.rs, this.pstmt, this.con); } return 0; } /** * 执行查询 * * @param sql * 传入的预设的 sql语句 * @param params * 问号参数列表 * @return 查询后的结果 */ public List> execQuery(String sql, Object[] params) { try { this.getConnection();// 获得连接对象 this.pstmt = this.con.prepareStatement(sql);// 获得预设语句对象 if (params != null) { // 设置参数列表 for (int i = 0; i < params.length; i++) { // 因为问号参数的索引是从1开始,所以是i+1,将所有值都转为字符串形式,好让setObject成功运行 this.pstmt.setObject(i + 1, params[i] + ""); } } // 执行查询 ResultSet rs = pstmt.executeQuery(); List> al = new ArrayList>(); // 获得结果集元数据(元数据就是描述数据的数据,比如把表的列类型列名等作为数据) ResultSetMetaData rsmd = rs.getMetaData(); // 获得列的总数 int columnCount = rsmd.getColumnCount(); // 遍历结果集 while (rs.next()) { Map hm = new HashMap(); for (int i = 0; i < columnCount; i++) { // 根据列索引取得每一列的列名,索引从1开始 String columnName = rsmd.getColumnName(i + 1); // 根据列名获得列值 Object columnValue = rs.getObject(columnName); // 将列名作为key,列值作为值,放入 hm中,每个 hm相当于一条记录 hm.put(columnName, columnValue); } // 将每个 hm添加到al中, al相当于是整个表,每个 hm是里面的一条记录 al.add(hm); } return al; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { this.close(this.rs, this.pstmt, this.con); } return null; } /** * 获取对象的属性,返回键值对 * @param obj * @return */ public static HashMap reflect(Object obj) { if (obj == null) return null; Field[] fields = obj.getClass().getDeclaredFields(); String name=""; Object[] value=new Object[fields.length]; for (int j = 0; j < fields.length; j++) { fields[j].setAccessible(true); // 字段名 name+="`"+fields[j].getName() + "`,"; // 字段值 if (fields[j].getType().getName().equals(String.class.getName())) {//String try { value[j]=(fields[j].get(obj)); } catch (Exception e) {e.printStackTrace(); } } else if (fields[j].getType().getName().equals(Long.class.getName()) || fields[j].getType().getName().equals("long")) {//int/long try { value[j]=(fields[j].getLong(obj)); } catch (Exception e) {e.printStackTrace();} }else if (fields[j].getType().getName().equals(Float.class.getName()) || fields[j].getType().getName().equals("float")) {//float/double try { value[j]=(fields[j].getFloat(obj)); } catch (Exception e) {e.printStackTrace();} }else{// 其他类型。。。 try { value[j]=(fields[j].get(obj)); } catch (Exception e) {e.printStackTrace();} } } HashMap m=new HashMap<>(); m.put("name", new Object[]{name}); m.put("value", value); return m; } /** * 检查某个表是否存在 * @param name * @return */ public boolean cktable(String name){ boolean is = false; try { this.getConnection();// 获得连接对象 ResultSet tables = this.con.getMetaData().getTables(null, null, name, null); if (tables.next()) is = true; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { this.close(this.rs, this.pstmt, this.con); } return is; } }