蟑螂恶霸的博客 蟑螂恶霸的博客
首页
  • Web自动化
  • 自动化测试框架
  • 接口自动化
  • 测试面试题
  • 技术文档
  • GitHub技巧
  • 博客搭建
  • Vue
  • JavaScript
  • Nginx
  • 自动化测试
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

蟑螂恶霸

一个爱折腾的程序员,什么都想试试!
首页
  • Web自动化
  • 自动化测试框架
  • 接口自动化
  • 测试面试题
  • 技术文档
  • GitHub技巧
  • 博客搭建
  • Vue
  • JavaScript
  • Nginx
  • 自动化测试
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 常用工具类

    • Session登录失败限制 & RSA密码加密
    • java配置双数据库
    • 原生jdbc工具类
      • jdbc工具类
      • 测试
    • orcal下划线转驼峰
    • RSA加密工具类
    • 实现定时任务数据库配置
  • 框架知识

  • 基础技巧

  • java知识集锦
  • 常用工具类
蟑螂恶霸
2022-11-01
目录

原生jdbc工具类

需求在不能配置双数据源的情况下你需要连接两个数据库进行手写jdbc来进行连接数据库操作数据

# jdbc工具类

这里为了方便我手写了一个工具类给你方便调用不用手写jdbc连接

package com.datanew.util;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 原生jdbc操作数据库工具类
 */
public class JdbcUtil {

    //数据库连接:地址、用户名、密码
    private final String url;
    private final String username;
    private final String password;

    public JdbcUtil(String url, String username, String password){
        this.url = url;
        this.username = username;
        this.password = password;
    }
    public JdbcUtil(String url, String username, String password, String driver){
        this.url = url;
        this.username = username;
        this.password = password;

        try {
            /*
                同时需要引入相关驱动依赖

                1、MySQL:
                com.mysql.cj.jdbc.Driver

                2、Oracle:
                oracle.jdbc.driver.OracleDriver

                3、pgsql:
                org.postgresql.Driver

             */
            //加载驱动
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取 Connection 连接
     */
    private Connection getConnection() {
        Connection conn = null;
        try {
            conn= DriverManager.getConnection(url, username, password);
            conn.setAutoCommit(true);
        } catch (SQLException e) {
            System.err.println("获取Connection连接异常...");
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 关闭 Connection 连接
     */
    private void close(Connection conn){
        try {
            conn.close();
        } catch (SQLException e) {
            System.err.println("关闭Connection连接异常...");
            e.printStackTrace();
        }
    }

    /**
     * 查询
     */
    public ArrayList<HashMap<String,Object>> find(String sql, Object[] params) {
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();

        //获取连接
        Connection conn = getConnection();
        PreparedStatement ps;
        ResultSet rs;

        try {
            //设置SQL、以及参数
            ps = conn.prepareStatement(sql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i + 1, params[i]);
                }
            }

            //执行查询
            rs = ps.executeQuery();

            //获取查询结果
            ResultSetMetaData rm = rs.getMetaData();
            int columnCount = rm.getColumnCount();

            //封装结果集
            while (rs.next()) {
                HashMap<String, Object> map = new HashMap<>(columnCount);
                for (int i = 1; i <= columnCount; i++) {
                    String name = rm.getColumnName(i).toLowerCase();
                    Object value = rs.getObject(i);

                    map.put(name,value);
                }
                list.add(map);
            }

            //关闭连接
            close(conn);
        } catch (Exception e) {
            System.err.println("执行 jdbcUtil.find() 异常...");
            e.printStackTrace();
        }

        return list;
    }
    public HashMap<String,Object> findOne(String sql, Object[] params){
        ArrayList<HashMap<String, Object>> list = find(sql, params);
        return list.size() > 0 ? list.get(0) : null;
    }

    /**
     * 更新
     */
    private boolean update(String sql, Object[] params){
        boolean flag = false;

        //获取连接
        Connection conn = getConnection();
        PreparedStatement ps;

        try {
            //设置SQL、以及参数
            ps = conn.prepareStatement(sql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i + 1, params[i]);
                }
            }

            //执行
            flag = ps.execute();
        } catch (SQLException e) {
            System.err.println("执行 jdbcUtil.update() 异常...");
            e.printStackTrace();
        }

        //关闭连接
        close(conn);

        return flag;
    }
}
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

# 测试

你可以使用一个main函数来进行测试

public static void main(String[] args) {
        //获取连接
        jdbcUtil jdbcUtil = new jdbcUtil("jdbc:mysql://localhost/jfinal_demo","root","123456");
 
        // find查询
        ArrayList<HashMap<String, Object>> list = jdbcUtil.find("select * from user", null);
        for (HashMap<String, Object> map : list) {
            System.out.println(map);
        }
 
        System.out.println("----------------------------");
 
        //update执行、findOne查询
        jdbcUtil.update("delete from user where user_id = ?", new Object[]{"4"});
        jdbcUtil.update("insert into user values (?,?)", new Object[]{"4","王麻子"});
        jdbcUtil.update("update user set user_name = ? where user_id = ?", new Object[]{"王麻子子","4"});
 
        HashMap<String, Object> map = jdbcUtil.findOne("select * from user where user_id = ?", new Object[]{"4"});
        System.out.println(map);
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
上次更新: 2022/11/01, 14:37:50
java配置双数据库
orcal下划线转驼峰

← java配置双数据库 orcal下划线转驼峰→

最近更新
01
实现定时任务数据库配置
06-09
02
SQL Server连接
02-22
03
RSA加密工具类
02-22
更多文章>
Theme by Vdoing | Copyright © 2022-2023 蟑螂恶霸 | Blog
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式