From 273332c72021e0b76febe973f1e7be723832ec48 Mon Sep 17 00:00:00 2001 From: admin <1782158860@qq.com> Date: Fri, 11 Nov 2022 12:19:06 +0800 Subject: [PATCH] db --- src/sc545/pay/utils/DBUtil.java | 284 ++++++++++++++++++++++++++++++++ 1 file changed, 284 insertions(+) create mode 100644 src/sc545/pay/utils/DBUtil.java diff --git a/src/sc545/pay/utils/DBUtil.java b/src/sc545/pay/utils/DBUtil.java new file mode 100644 index 0000000..62e7e7f --- /dev/null +++ b/src/sc545/pay/utils/DBUtil.java @@ -0,0 +1,284 @@ +package sc545.pay.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 = ReadTxt.getSetting(null, "setting.ini", "MySqlName","test"); + String driver = "com.mysql.jdbc.Driver";// 驱动名称 + String url = "jdbc:mysql://"+ReadTxt.getSetting(null, "setting.ini", "MySqlIP","127.0.0.1")+":"+ReadTxt.getSetting(null, "setting.ini", "MySqlPort","3306")+"/"+dname+"?useUnicode=true&characterEncoding=UTF-8&useOldAliasMetadataBehavior=true&autoReconnect=true&useSSL=false";// 连接 + String username = ReadTxt.getSetting(null, "setting.ini", "MySqlUserName","root");// 用户名 + String password = ReadTxt.getSetting(null, "setting.ini", "MySqlPassWord","cheng355217");// 密码 + + // 三剑客 + 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 search + * @return + */ + public boolean searchCount(String search) { + String resid = "0"; + List> res = execQuery( + "select id from byx_count where searchstr =?", + new String[] { search }); + if (res != null && res.size() > 0) { + resid = res.get(0).get("id").toString(); + int i = execUpdate( + "update byx_count set countsum = countsum+1 where id = " + + resid, null); + return i > 0 ? true : false; + } else { + int i1 = execUpdate( + "insert into byx_count(countsum,searchstr) values(1,?)", + new String[] { search }); + return i1 > 0 ? true : false; + } + } + + /** + * 获取对象的属性,返回键值对 + * @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(java.lang.String.class.getName())) {//String + + try { + value[j]=(fields[j].get(obj)); + } catch (Exception e) {e.printStackTrace(); + } + + } else if (fields[j].getType().getName().equals(java.lang.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(java.lang.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; + } + +}