连接库操作:

 package com.qa.xxx;

 import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.List; @Component
public class MySQLUtil { private static final String MYSQL_DRIVER = "com.mysql.cj.jdbc.Driver"; private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>(); public static Connection getMysqlConnection(String url, String userName, String userPassword){
Connection connection = threadLocal.get();
if(null == connection){
try {
Class.forName(MYSQL_DRIVER);
connection = DriverManager.getConnection(url, userName, userPassword);
return connection;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
return connection;
} /**
* 查询定义的相应的数据库对象bean值
* @param url
* @param userName
* @param userPassword
* @param sql
* @param t
* @param objs
* @param <T>
* @return
*/
public static <T> List<T> excuteQuery(String url, String userName, String userPassword, String sql, T t, Object...objs){
List<T> list = new ArrayList<>();
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
connection = MySQLUtil.getMysqlConnection(url,userName,userPassword);
ps = connection.prepareStatement(sql);
//占位符赋值
if(null != objs){
for(int i=0; i<objs.length; i++){
ps.setObject((i+1), objs[i]);
}
}
rs = ps.executeQuery();
ResultSetMetaData rm = rs.getMetaData();
int columnCount = rm.getColumnCount();
while (rs.next()){
Class<? extends Object> clzss = t.getClass();
T newInstance = (T)clzss.newInstance();
for(int i=1; i<=columnCount; i++){
String columnName = rm.getColumnName(i);
String methodName = "set" + columnName.substring(0,1).toUpperCase() + columnName.substring(1);
String columnClassName = rm.getColumnClassName(i);
Method method = clzss.getDeclaredMethod(methodName, Class.forName(columnClassName));
method.invoke(newInstance, rs.getObject(columnName));
}
list.add(newInstance);
} }catch (Exception e){
e.printStackTrace();
}finally {
MySQLUtil.close(ps);
}
return list;
} /**
* 查询单个字段值
* @param url
* @param userName
* @param userPassword
* @param sql
* @param objs
* @return
*/
public static List<String> excuteOneFieldQuery(String url, String userName, String userPassword, String sql, Object...objs){
List<String> list = new ArrayList<>();
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
connection = MySQLUtil.getMysqlConnection(url,userName,userPassword);
ps = connection.prepareStatement(sql);
//占位符赋值
if(null != objs){
for(int i=0; i<objs.length; i++){
ps.setObject((i+1), objs[i]);
}
}
rs = ps.executeQuery();
ResultSetMetaData rm = rs.getMetaData();
int columnCount = rm.getColumnCount();
while (rs.next()){
list.add(rs.getString(1));
}
}catch (Exception e){
e.printStackTrace();
}finally {
MySQLUtil.close(ps);
}
return list;
} /**
* 增删改
* @param url
* @param userName
* @param userPassword
* @param sql
* @param objs
* @return
*/
public static Integer executeDML(String url, String userName, String userPassword, String sql, Object...objs){
Connection connection = null;
PreparedStatement ps = null;
Integer integer = 0;
try{
connection = MySQLUtil.getMysqlConnection(url,userName,userPassword);
ps = connection.prepareStatement(sql);
if(null != objs){
for(int i=0; i<objs.length; i++){
ps.setObject((i+1), objs[i]);
}
}
integer = ps.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}finally {
MySQLUtil.close(ps);
}
return integer;
} /**
* 关闭操作
* @param t
* @param <T>
*/
private static <T>void close(T...t){
//循环关流
for(T tmp:t) {
//关闭流对象
if(tmp instanceof AutoCloseable) {
try {
((AutoCloseable)tmp).close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
} }

数据库字段比对:

 package com.qa.xxx;

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map; @Component
public class DataCompareUtil { private final static Logger logger = LoggerFactory.getLogger(DataCompareUtil.class); /**
* 数据库表查询字段比对
* @param obj1 老查询获取的数据
* @param obj2 新查询获取的数据
* @param list 要对比的字段
* @return 返回<字段名称,原值x 新值x>
*/
public static Map<String, String> compareObject(Object obj1, Object obj2, List<String> list){
Map<String, String> map = new HashMap<>();
if(null != list && !list.isEmpty()){
for(String field : list){
String firstLetter = field.substring(0,1).toUpperCase();
String getter = "get" + firstLetter + field.substring(1);
try {
Method method1 = obj1.getClass().getMethod(getter, new Class[]{});
Method method2 = obj2.getClass().getMethod(getter, new Class[]{});
Object oldValue = method1.invoke(obj1, new Object[] {});
Object newValue = method2.invoke(obj2, new Object[] {});
map.put(field, "原值:" + oldValue.toString() + " 新值:" + newValue.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
return map;
}else {
Class clazz = obj2.getClass();
Field[] fields = clazz.getDeclaredFields();
for(Field field : fields){
String fieldName = field.getName();
String firstLetter = fieldName.substring(0,1).toUpperCase();
String getter = "get" + firstLetter + fieldName.substring(1);
try {
Method method1 = obj1.getClass().getMethod(getter, new Class[]{});
Method method2 = obj2.getClass().getMethod(getter, new Class[]{});
Object oldValue = method1.invoke(obj1, new Object[] {});
Object newValue = method2.invoke(obj2, new Object[] {});
map.put(fieldName, "原值:" + oldValue.toString() + " 新值:" + newValue.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
return map;
}
} }

最新文章

  1. &lt;!DOCTYPE&gt;标签的定义与用法
  2. 安装Linux Mint
  3. 总结-css编码规范
  4. windows下制作linux U盘启动盘或者安装优盘(转)
  5. js020-JSON
  6. 后一个div无法遮挡住前一个有img的div
  7. 安装配置OPENCMS的Replication cluster(从)详细过程
  8. appendChild()插入节点需注意的问题
  9. Android得到控件在屏幕中的坐标
  10. android app启动过程(转)
  11. HDU 1272 小希的迷宫(乱搞||并查集)
  12. iOS进阶推荐的书目
  13. (三)、Struts第三天
  14. Junit简单配置
  15. angularjs 学习小结
  16. json数据的处理和转化(loads/load/dump/dumps)
  17. 2015-10-07 jQuery2
  18. linux系统下安装redis及配置
  19. .NET Framework System.Array.Sort 数组类,加深对 IComparer、IComparable 以及泛型委托、匿名方法、Lambda 表达式的理解
  20. innodb crash

热门文章

  1. bit,byte,word,bps,Bps,比特,字节,字, 一图看懂
  2. 把字符串当做js代码执行的方法
  3. 一步一步配置AWS ELB Https证书
  4. JavaWeb_(Spring框架)认识Spring中的aop
  5. elasticsearch _all
  6. WINRAR弹窗堆栈
  7. python+socket+jq实现web页面实时输出结果
  8. java并发编程--第一章并发编程的挑战
  9. Colab 实用教程
  10. JS判断与过滤的表情符号表情的方法