jdbc连接数据库进行数据查询,返回对象,返回值自定义 把 大概代码贴一下,自己连接,仅执行语句返回内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | /** * 查询sql * * @param: sql * @return: JSONArray */ public static JSONArray querySql(PostgreSQLConfig staticPostgreSQLConfig, String sql, Object... objects) { JSONArray resJson = null ; Connection conn = null ; PreparedStatement pstmt = null ; ResultSet reSet = null ; PGConnectionPool connPool = null ; try { // conn = getConnection(staticPostgreSQLConfig); connPool = PGConnectionPoolUtil.getPoolInstance(staticPostgreSQLConfig); conn = connPool.getConnection(); pstmt = conn.prepareStatement(sql); if (objects != null && objects.length > 0 ) { for ( int i = 0 ; i < objects.length; i++) { pstmt.setObject(i + 1 , objects[i]); } } logger.info( "执行SQL:{}" , sql); reSet = pstmt.executeQuery(); // 获取列数 ResultSetMetaData metaData = reSet.getMetaData(); int columnCount = metaData.getColumnCount(); // 遍历ResultSet中的每条数据 while (reSet.next()) { JSONObject jsonObj = new JSONObject(); // 获取每一列数据 for ( int i = 1 ; i <= columnCount; i++) { String value = "" ; String columnName = metaData.getColumnLabel(i); //列名称 if (reSet.getString(columnName) != null && !reSet.getString(columnName).equals( "" )) { value = new String(reSet.getBytes(columnName), "UTF-8" ); //列的值,有数据则转码 } jsonObj.put(columnName, value); } resJson.add(jsonObj); } } catch (Exception e) { e.printStackTrace(); } finally { // closeConnection(conn, pstmt, reSet); closePoolConnection(connPool, conn, pstmt, reSet); } return resJson; } public static <T> List<T> querySql(PostgreSQLConfig staticPostgreSQLConfig, Class<T> clazz, String sql, Object... objects) { List<T> resList = new ArrayList<>(); Connection conn = null ; PreparedStatement pstmt = null ; ResultSet reSet = null ; PGConnectionPool connPool = null ; try { // conn = getConnection(staticPostgreSQLConfig); connPool = PGConnectionPoolUtil.getPoolInstance(staticPostgreSQLConfig); conn = connPool.getConnection(); pstmt = conn.prepareStatement(sql); if (objects != null && objects.length > 0 ) { for ( int i = 0 ; i < objects.length; i++) { pstmt.setObject(i + 1 , objects[i]); } } logger.info( "执行SQL:{}" , sql); reSet = pstmt.executeQuery(); List<Map<String, Object>> tempList = new ArrayList<>(); Map<String, Object> tempMap = null ; // 获取列数 ResultSetMetaData metaData = reSet.getMetaData(); int columnCount = metaData.getColumnCount(); // 遍历ResultSet中的每条数据 while (reSet.next()) { tempMap = new HashMap<>(); for ( int i = 1 ; i <= columnCount; i++) { String columnName = metaData.getColumnLabel(i); //列名称 tempMap.put(columnName, reSet.getObject(i)); } tempList.add(tempMap); } //加载数据到对象 if (tempList.size() > 0 ) { for (Map<String, Object> map : tempList) { //解析对象 T bean = clazz.newInstance(); for (Map.Entry<String, Object> entry : map.entrySet()) { String propertyName = entry.getKey(); Object value = entry.getValue(); Field field = clazz.getDeclaredField(propertyName); String str = field.getName(); String s = str.substring( 0 , 1 ).toUpperCase() + str.substring( 1 ); Method method = clazz.getMethod( "set" + s, field.getType()); method.invoke(bean, value); } resList.add(bean); } } } catch (Exception e) { e.printStackTrace(); } finally { // closeConnection(conn, pstmt, reSet); closePoolConnection(connPool, conn, pstmt, reSet); } return resList; } /** * 关闭连接 */ public static void closePoolConnection(PGConnectionPool connPool, Connection conn, PreparedStatement pstmt, ResultSet reSet) { try { if (pstmt != null ) { pstmt.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { if (reSet != null ) { try { reSet.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (conn != null ) { connPool.returnConnection(conn); } } catch (Exception e) { e.printStackTrace(); } } } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端