连接库操作:

 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. lodop打印设计

    <template> <div class="dashboard-container"> <form id="form1"> ...

  2. Win内核原理与实现学习笔记1-windows内核版本列表

  3. css定位中的百分比

    ----转载自自己在牛人部落中的相关文章--- 在前端css定位中经常面对的一个问题是,百分比定位究竟是针对于谁定位? 一.margin,padding的百分比 首先从css的设计意图说起,在浏览器默 ...

  4. ubuntu 无法访问windows使用的磁盘

    安装双系统的电脑,正常情况下Ubuntu是可以访问windows下使用的磁盘的, 当出现如下图所示问题时: Windows没有正常关闭. 解决方法: sudo apt-get install ntfs ...

  5. springboot自定义异常RESTful返回异常

    1.自定义异常类 package com.zhx.common.exception; import com.zhx.common.model.ErrorCode; /** * @Author: Sim ...

  6. app微信支付的集成步骤

    1.引用地址 //微信支付 compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+' 2.注册 private IWXAPI api ...

  7. ShapeDrawable

    形状的Drawable咯,定义基本的几何图形,如(矩形,圆形,线条等),根元素是<shape../> 节点比较多,相关的节点如下: ① <shape>: ~ visible:设 ...

  8. 文件上传使用FileUpload组件进行代码实现

    使用FileUpload组件进行代码实现 实现步骤 1. 获取解析器工厂: DiskFileItemFactory 2. 获取解析器对象: ServletFileUpload 3. 解析request ...

  9. 关于Jetson Kit开发相关资料

    首先是nVidia官方对于Jetson Kit的介绍: http://www.nvidia.com/object/jetson-tk1-embedded-dev-kit.html https://de ...

  10. Python在for循环中更改list值的方法

    一.在for循环中直接更改列表中元素的值不会起作用: 如: l = list(range(10)[::2]) print (l) for n in l: n = 0 print (l) 运行结果: [ ...