连接库操作:

 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;
}
} }

Java Mysql--链接数据库,数据库字段比较的更多相关文章

  1. 关于在Java中链接SQLServer数据库中失败的原因分析

    首先声明:笔者是Java的初学者,并且一值是走在自学的道路上,长久以来只有“度娘”相伴.(加入了各种Java学习群,基本没有热心帮人解决问题的.可以理解-_-!!!)大神级的人物就不必看拙文了,没有什 ...

  2. Java JDBC链接Oracle数据库

    package com.test.test; import java.io.FileInputStream;import java.io.FileNotFoundException;import ja ...

  3. Java语言 链接Oracle数据库

    package com.tao.pojo; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Pre ...

  4. Java MySql 批量插入数据库addBatch

    //addBatch批量插入数据库 public static void insertCommentToMySql(Set<String> commentList) { Iterator& ...

  5. java mysql 链接高版本出现SSL验证

    key1: String url="jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8 ...

  6. java面试题:数据库mysql

    Web后端数据库一般用mysql. 数据库基础 Q:数据库三范式是什么? 第一范式:列不可再分 第二范式:行可以唯一区分,主键约束 第三范式:表的非主属性不能依赖与其他表的非主属性 外键约束 且三大范 ...

  7. mysql 关键字于数据库字段于关键字冲突的问题

    如果数据库存储字段 为MySQL关键字,那么在查询或者其他操作时会出错.那么我们应该怎么办, 可能有些人会说,换个字段不就好了啊.当然这样也是可以的,完全没问题. 然而,如果是在无法对数据库进行修改和 ...

  8. Java 通过JDBC查询数据库表结构(字段名称,类型,长度等)

    Java 通过JDBC查询数据库表结构(字段名称,类型,长度等) 发布者:唛唛家的豆子   时间:2012-11-20 17:54:02   Java 通过JDBC查询数据库表结构(字段名称,类型,长 ...

  9. java.sql.Types,数据库字段类型,java数据类型的对应关系

    以下转自:http://kummy.itpub.net/post/17165/172850 本文在原文基础上有增减. 本概述是从<JDBCTM Database Access from Java ...

  10. Java -- JDBC 学习--获取数据库链接

    数据持久化 持久化(persistence): 把数据保存到可掉电式存储设备中以供之后使用.大多数情况下,特别是企业级应用,数据持久化意味着将内存中的数据保存到硬盘上加以”固化”,而持久化的实现过程大 ...

随机推荐

  1. iptables防火墙--------基本概念

    iptables按照规则进行处理,而iptables的规则存储在内核空间的信息包过滤表中,这些规则分别指定了源地址.目的地址.传输协议(TCP.UDP.ICMP)和服务类型(如HTTP.FTP和SMT ...

  2. sqlserver 存储过程的新建与执行

    if Exists(select * from sysobjects where NAME = 'insert_custominfo' and type='P') drop procedure ins ...

  3. OpenJudge1.5.6:整数序列的元素最大跨度值

    描述 给定一个长度为n的非负整数序列,请计算序列的最大跨度值(最大跨度值 = 最大值减去最小值). 输入一共2行,第一行为序列的个数n(1 <= n <= 1000),第二行为序列的n个不 ...

  4. JAVA RPC (九) netty服务端解析

    源码地址:https://gitee.com/a1234567891/koalas-rpc 企业生产级百亿日PV高可用可拓展的RPC框架.理论上并发数量接近服务器带宽,客户端采用thrift协议,服务 ...

  5. docker数据持久化

    转载/参考: https://www.jianshu.com/p/ef0f24fd0674 Docker的数据持久化主要有两种方式: bind mount docker managed volume ...

  6. CISCO实验记录九:NAT地址转换

    1.静态NAT地址转换 #ip nat inside source static 192.168.12.1 192.168.23.4 //将12.1转为23.4 必须精确到主机IP 而不能是某个网段 ...

  7. pwn学习日记Day22 《程序员的自我修养》读书笔记

    知识杂项 软连接 命令: ln -s 原文件 目标文件 特征: 1.相当于windows的快捷方式 2.只是一个符号连接,所以软连接文件大小都很小 3.当运行软连接的时候,会根据连接指向找到真正的文件 ...

  8. HTTP之缓存

    1. 保持副本的新鲜 HTTP 有一些简单的机制可以在不要求服务器记住有哪些缓存拥有其文档副本的情况下,保持已缓存数据与服务器数据之间充分一致.HTTP 将这些简单的机制称为文档过期(document ...

  9. The problem is now the wait_for_fds() example function: it will call something like select(), poll() or the more modern epoll() and kqueue().

    小结: 1.线程与惊群效应 Serializing accept(), AKA Thundering Herd, AKA the Zeeg Problem — uWSGI 2.0 documentat ...

  10. export default {} 和new Vue()区别?

    export default 的用法:相当于提供一个接口给外界,让其他文件通过 import 来引入使用. 而对于 new Vue({})部分, 只是创建一个Vue的实例 就是相当于创建一个根组件 h ...