JDBC数据库操作的封装方法参考(之前写的这篇文章):Java(23):Java对于jdbc对数据库的封装[1]_fen_fen的专栏-CSDN博客
下面接着说的是,加上判断查询结果是否正确。
package com.ciphergateway.asserts; import org.apache.commons.lang3.StringUtils; import java.sql.SQLException; import java.util.List; import java.util.Map; /** *Author:HMF *@create 2021-09-25 **/ public class DatabaseAssert { /** * 断言(入参Map_input) * @param listMaps_out * @param Map_input * @throws SQLException */ public static int assertData(List<Map<String, Object>> listMaps_out,Map<String, String> Map_input){ int num=0; for (Map<String, Object> map : listMaps_out) { for (String key : map.keySet()) { boolean flag=false; for (String key_input : Map_input.keySet()) { //key值相同再对比value值 if(key.equals(key_input)){ //System.out.println(Map_input.get(key_input)); flag =StringUtils.endsWith(String.valueOf(map.get(key)), String.valueOf(Map_input.get(key_input))); if (flag){ num += 1; } else { System.out.println("未找到匹配" + key); } } } } } return num; } /** * 断言(入参,标识符) * @param listMaps_out * @param cipher * @throws SQLException */ public static int assertData(List<Map<String, Object>> listMaps_out,String cipher){ int num=0; for (Map<String, Object> map : listMaps_out) { for (String key : map.keySet()) { boolean flag = false; if (key.equals("aoe_id") == false) { //System.out.println(String.valueOf(map.get(key))); //System.out.println(cipher); flag = StringUtils.endsWith(String.valueOf(map.get(key)), cipher); if (flag) { num += 1; } else { System.out.println("未找到匹配" + key); } } } } return num; } }
数据库db.propeties
#mysql原库 driverClass =com.mysql.jdbc.Driver url=jdbc:mysql://10.1.1.191:3306/aoe_auto?useSSL=false&useUnicode=yes&characterEncoding=UTF-8 userName=root passWord=123456 |
断言调用:
int num= DatabaseAssert.assertData(listMaps_1,Map_input);
import com.ciphergateway.utils.DataBaseUtil; import com.ciphergateway.asserts.DatabaseAssert; import com.ciphergateway.utils.Common; import org.apache.commons.lang3.StringUtils; import java.io.*; import java.sql.*; import java.util.*; public class mysqlTest2{ private static Map<String, String> Map_input = new LinkedHashMap<String,String>(); public static void main(String[] args) throws Exception { //获取配置文件数据 String filename="db.properties"; Properties pro=Common.getProperty(filename); String tableName=pro.getProperty("tableName"); //定义插入数据 Map_input.put("aoe_aes", "吴秀梅"); Map_input.put("aoe_sm4", "Beijing Refining Network Technology Co.Ltd."); Map_input.put("aoe_sm4_a", "北京市海淀区北三环西路32号楼7层0710-1"); Map_input.put("aoe_email", "qianxiulan@yahoo.com"); Map_input.put("aoe_phone", "15652996964"); Map_input.put("aoe_id_card", "210302199608124861"); Map_input.put("aoe_officer_card", "武水电字第3632734号"); Map_input.put("aoe_passport", "BWP018930705"); Map_input.put("aoe_general_id_card", "0299233902");//智能识别证件号 Map_input.put("aoe_credit_card", "6212262502009182455");//银行卡号 DataBaseUtil database=new DataBaseUtil(); //操作数据库 Connection conn=database.getConnection(); //插入数据 insertTest(conn,database,tableName); //查询最大的id(最后插入的数据,用来做查询条件) String sql="select max(aoe_id) maxId from "+tableName; int aoe_id=database.getMaxId(conn,sql); System.out.println(aoe_id); //查询数据 List<Map<String, Object>> listMaps_1=selectTest(conn,database,tableName,aoe_id); //断言 int num= DatabaseAssert.assertData(listMaps_1,Map_input); if (num ==10){ System.out.println("====================================="); System.out.println("插入数据后,查询,10个字段数据都正确"); } else { System.out.println("====================================="); System.out.println("插入数据后,查询,10个字段部分正确"+num); } } /** * 插入数据 * @param database */ public static void insertTest(Connection conn,DataBaseUtil database,String tableName){ List<Object> params = new ArrayList<Object>(); //params.add("1"); params.add(Map_input.get("aoe_aes")); //aoe_aes params.add(Map_input.get("aoe_sm4")); //aoe_sm4 params.add(Map_input.get("aoe_sm4_a"));//aoe_sm4_a params.add(Map_input.get("aoe_email"));//aoe_email params.add(Map_input.get("aoe_phone"));//aoe_phone params.add(Map_input.get("aoe_id_card"));//aoe_id_card params.add(Map_input.get("aoe_officer_card"));//aoe_officer_card params.add(Map_input.get("aoe_passport"));//aoe_passport params.add(Map_input.get("aoe_general_id_card"));//aoe_general_id_card,智能识别证件号 params.add(Map_input.get("aoe_credit_card"));//aoe_credit_card银行卡 System.out.println(params); //String sql ="insert into aoe_auto (aoe_aes, aoe_sm4) values (?,?)"; //'"+ tableName +"' String sql="INSERT INTO "+ tableName +" (aoe_aes, aoe_sm4, aoe_sm4_a, aoe_email, aoe_phone, aoe_id_card, aoe_officer_card, aoe_passport, aoe_general_id_card, aoe_credit_card) VALUES(?,?,?,?,?,?,?,?,?,?)"; try { boolean flag = database.updateByPreparedStatement(conn,sql, params); System.out.println(flag); } catch (SQLException e) { e.printStackTrace(); } } /** * 通过主键查询 * @param database * @param aoe_id * @throws SQLException */ public static List<Map<String, Object>> selectTest(Connection conn,DataBaseUtil database,String tableName,int aoe_id) throws SQLException { String sql="select * from "+ tableName +" where aoe_id='"+aoe_id+"'"; List<Map<String, Object>> listMaps = new ArrayList<Map<String, Object>>(); listMaps=database.executeQuery(conn,sql); System.out.println(listMaps); return listMaps; } }