已解决
idea自动封装方法
来自网友在路上 160860提问 提问时间:2023-10-14 18:32:31阅读次数: 60
最佳答案 问答题库608位专家为你答疑解惑
例如
package com.utils;import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;/*** @author hrui* @date 2023/10/13 13:49*/
public class DBUtils {private static ResourceBundle bundle=ResourceBundle.getBundle("jdbc");private static String driver=bundle.getString("jdbc.driver");private static String url=bundle.getString("jdbc.url");private static String username=bundle.getString("jdbc.username");private static String password=bundle.getString("jdbc.password");static{try {Class.forName(driver);} catch (ClassNotFoundException e) {e.printStackTrace();}}//通用查询多个public static <T> List<T> selectList(Class<T> clazz, String sql, Object...args){Connection conn=null;PreparedStatement ps=null;ResultSet rs=null;try {conn=DBUtils.getConnection();ps=conn.prepareStatement(sql);for(int i=0;i<args.length;i++){ps.setObject(i+1, args[i]);}rs = ps.executeQuery();ResultSetMetaData metaData = rs.getMetaData();int columnCount = metaData.getColumnCount();List<T> list=new ArrayList<>();while(rs.next()){T t = clazz.newInstance();for(int i=0;i<columnCount;i++){Object object = rs.getObject(i + 1);//String columnName = metaData.getColumnName(i + 1); 这个方法返回实际列名String columnLabel = metaData.getColumnLabel(i + 1);//该方法返回别名,没有别名就返回列名if(columnLabel.contains("_")){int index = columnLabel.indexOf("_");String replace = columnLabel.replace("_", "");char c = Character.toUpperCase(replace.charAt(index));columnLabel=replace.substring(0, index)+c+replace.substring(index+1);}Field field = clazz.getDeclaredField(columnLabel);field.setAccessible(true);field.set(t,object);}list.add(t);}return list;} catch (Exception e) {e.printStackTrace();}finally {DBUtils.closed(conn,ps,rs);}return null;}//通用查询单个public static <T> T selectOne(Class<T> clazz,String sql,Object...args){Connection conn=null;PreparedStatement ps=null;ResultSet rs=null;try {conn=DBUtils.getConnection();ps=conn.prepareStatement(sql);for(int i=0;i<args.length;i++){ps.setObject(i+1, args[i]);}rs = ps.executeQuery();ResultSetMetaData metaData = rs.getMetaData();int columnCount = metaData.getColumnCount();if(rs.next()){T t = clazz.newInstance();for(int i=0;i<columnCount;i++){Object object = rs.getObject(i + 1);String columnLabel = metaData.getColumnLabel(i + 1);if(columnLabel.contains("_")){int index = columnLabel.indexOf("_");String replace = columnLabel.replace("_", "");char c = Character.toUpperCase(replace.charAt(index));columnLabel=replace.substring(0, index)+c+replace.substring(index+1);}Field field = clazz.getDeclaredField(columnLabel);field.setAccessible(true);field.set(t,object);}return t;}} catch (Exception e) {e.printStackTrace();}finally {DBUtils.closed(conn,ps,rs);}return null;}public static Connection getConnection() throws SQLException {Connection connection = DriverManager.getConnection(url, username, password);return connection;}//通用增删改方法public static int update(String sql,Object...args){Connection conn =null;PreparedStatement ps=null;int count=0;try {conn = DBUtils.getConnection();ps = conn.prepareStatement(sql);for(int i=0;i<args.length;i++){ps.setObject(i+1, args[i]);}count = ps.executeUpdate();//ps.execute();} catch (SQLException e) {e.printStackTrace();}finally {DBUtils.closed(conn,ps,null);}return count;}public static void closed(Connection conn, Statement st, ResultSet rs){if(rs!=null){try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if(st!=null){try {st.close();} catch (SQLException e) {e.printStackTrace();}}if(conn!=null){try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}
这段代码中
可以提取出来作为独立的一个方法
查看全文
99%的人还看了
相似问题
- Kotlin学习——kt里的集合,Map的各种方法之String篇
- Office文件在线预览大全-Word文档在线预览的实现方法-OFD文档在线预览-WPS文件在线预览
- composer切换全局镜像源的方法
- Python通过selenium调用IE11浏览器报错解决方法
- 测试用例的设计方法(全):正交实验设计方法|功能图分析方法|场景设计方发
- Java8新特性 ----- Lambda表达式和方法引用/构造器引用详解
- C#中抽象类、抽象方法和接口暨内联临时变量的精彩表达
- ChatGLM2 大模型微调过程中遇到的一些坑及解决方法(更新中)
- 类方法,静态方法和实例方法的区别及应用场景
- 【链表的说明、方法---顺序表与链表的区别】
猜你感兴趣
版权申明
本文"idea自动封装方法":http://eshow365.cn/6-19501-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!