当前位置: 首页 > news >正文

jdbc工具类

jdbc 工具类,具体见下面代码,直接可以用。

/*** @version 1.0* @descpription: jdbc工具类* @date 2024/4/6*/
public class JDBCUtils {private static final String URL = "jdbc:mysql://127.0.0.1:3306/mybatis";private static final String USER = "root";private static final String PASSWORD = "root";private static final String DRIVER = "com.mysql.cj.jdbc.Driver";/*** 批量插入的最大数量*/private static final int BATCH_SIZE = 2;private static final String  INSERT_SQL = "INSERT INTO user (name, age) VALUES (?, ?)";private static final String  UPDATE_SQL = "UPDATE user SET name=?, age=? WHERE id=?";private static final String DELETE_SQL = "DELETE FROM user WHERE id=?";public static final String SELECT_SQL = "SELECT * FROM user WHERE id=?";/***  获取数据库连接* @return* @throws Exception*/public static Connection getConnection() throws Exception {//注册驱动Class.forName(DRIVER);return DriverManager.getConnection(URL, USER, PASSWORD);}/***  关闭数据库连接* @param connection*/public static void closeConnection(Connection connection) {try {if (connection != null) {connection.close();}} catch (SQLException e) {throw new RuntimeException(e);}}/*** 关闭closeResultSet* @param resultSet*/public static void closeResultSet(ResultSet resultSet) {try {if (resultSet != null) {resultSet.close();}} catch (SQLException e) {throw new RuntimeException(e);}}/***   关闭PreparedStatement* @param preparedStatement*/public static void closeStatement(PreparedStatement preparedStatement){try {if (preparedStatement != null) {preparedStatement.close();}} catch (SQLException e) {throw new RuntimeException(e);}}/*** 新增* @param paramMap*/public static void insert(Map<String,Object> paramMap){Connection connection = null;PreparedStatement preparedStatement = null;try {connection = JDBCUtils.getConnection();preparedStatement = connection.prepareStatement(INSERT_SQL);preparedStatement.setString(1, (String) paramMap.get("name"));preparedStatement.setInt(2, paramMap.get("age") == null ? 0 : (int) paramMap.get("age"));preparedStatement.executeUpdate();} catch (Exception e) {throw new RuntimeException(e);}finally {JDBCUtils.closeConnection(connection);JDBCUtils.closeStatement(preparedStatement);}}/***  批量新增* @param paramMapList* @param size*/public static void batchInsert(List<Map<String,Object>> paramMapList,int size){// 批量插入Connection connection = null;PreparedStatement preparedStatement = null;long start = System.currentTimeMillis();try {connection = JDBCUtils.getConnection();preparedStatement = connection.prepareStatement(INSERT_SQL);//设置手动提交,关闭自动提交connection.setAutoCommit(false);for (int i = 1; i <= size; i++) {Map<String, Object> paramMap = paramMapList.get(i-1);preparedStatement.setString(1, (String) paramMap.get("name"));preparedStatement.setInt(2, paramMap.get("age") == null ? 0 : (int) paramMap.get("age"));// 添加到批处理中preparedStatement.addBatch();if (i % BATCH_SIZE == 0) {// 执行批处理preparedStatement.executeBatch();// 提交事务connection.commit();// 清空批处理preparedStatement.clearBatch();}}} catch (Exception e) {throw new RuntimeException(e);} finally {JDBCUtils.closeConnection(connection);JDBCUtils.closeStatement(preparedStatement);long end = System.currentTimeMillis() - start;System.out.println(end);}}/***  删除* @param paramMap*/public static void delete(Map<String,Object> paramMap){Connection connection = null;PreparedStatement preparedStatement = null;try {connection = JDBCUtils.getConnection();preparedStatement = connection.prepareStatement(DELETE_SQL);preparedStatement.setInt(1, (int) paramMap.get("id"));preparedStatement.executeUpdate();}catch (Exception e){throw new RuntimeException(e);}finally {JDBCUtils.closeConnection(connection);JDBCUtils.closeStatement(preparedStatement);}}/***  更新* @param paramMap*/public static void update(Map<String,Object> paramMap){Connection connection = null;PreparedStatement preparedStatement = null;try {connection = JDBCUtils.getConnection();preparedStatement = connection.prepareStatement(UPDATE_SQL);preparedStatement.setString(1, (String) paramMap.get("name"));preparedStatement.setInt(2, paramMap.get("age") == null ? 0 : (int) paramMap.get("age"));preparedStatement.setInt(3, (int) paramMap.get("id"));preparedStatement.executeUpdate();}catch (Exception e){throw new RuntimeException(e);}finally {JDBCUtils.closeConnection(connection);JDBCUtils.closeStatement(preparedStatement);}}/*** 根据id查询* @param paramMap*/public static void selectById(Map<String,Object> paramMap) {Connection connection = null;PreparedStatement preparedStatement = null;ResultSet resultSet = null;try {connection = JDBCUtils.getConnection();preparedStatement = connection.prepareStatement(SELECT_SQL);preparedStatement.setInt(1, (int) paramMap.get("id"));resultSet = preparedStatement.executeQuery();while (resultSet.next()) {int id = resultSet.getInt("id");String name = resultSet.getString("name");int age = resultSet.getInt("age");System.out.println(id + " " + name + " " + age);}}catch (Exception e){throw new RuntimeException(e);} finally {// 关闭资源JDBCUtils.closeResultSet(resultSet);JDBCUtils.closeConnection(connection);JDBCUtils.closeStatement(preparedStatement);}}/***  查询所有*/public static void selectAll(){Connection connection = null;PreparedStatement preparedStatement = null;ResultSet resultSet = null;try {connection = JDBCUtils.getConnection();preparedStatement = connection.prepareStatement("SELECT * FROM user ");resultSet = preparedStatement.executeQuery();while (resultSet.next()) {int id = resultSet.getInt("id");String name = resultSet.getString("name");int age = resultSet.getInt("age");System.out.println(id + " " + name + " " + age);}}catch (Exception e){throw new RuntimeException(e);} finally {// 关闭资源JDBCUtils.closeResultSet(resultSet);JDBCUtils.closeConnection(connection);JDBCUtils.closeStatement(preparedStatement);}}
}
/*** @descpription:* @date 2024/4/2*/
public class JdbcTest {public static void main(String[] args) throws ClassNotFoundException{
//        insertTest();
//        updateTest();
//        deleteTest();batchInsertTest();}public static void batchInsertTest(){List<Map<String,Object>> paramMapList = new ArrayList<>();Map<String,Object> paramMap = new HashMap<>();paramMap.put("name","姜科");paramMap.put("age",18);Map<String,Object> paramMap2 = new HashMap<>();paramMap2.put("name","老朱");paramMap2.put("age",20);paramMapList.add(paramMap);paramMapList.add(paramMap2);JDBCUtils.batchInsert(paramMapList,2);JDBCUtils.selectAll();}public static void insertTest(){Map<String,Object> paramMap = new HashMap<>();paramMap.put("name","测试");paramMap.put("age",18);JDBCUtils.insert(paramMap);JDBCUtils.selectAll();}public static void updateTest(){Map<String,Object> paramMap = new HashMap<>();paramMap.put("name","zhangtao");paramMap.put("age",100);paramMap.put("id",1);JDBCUtils.update(paramMap);JDBCUtils.selectAll();}public static void deleteTest(){Map<String,Object> paramMap = new HashMap<>();paramMap.put("id",9999004);JDBCUtils.delete(paramMap);JDBCUtils.selectAll();}
}

在这里插入图片描述

相关文章:

jdbc工具类

jdbc 工具类&#xff0c;具体见下面代码&#xff0c;直接可以用。 /*** version 1.0* descpription: jdbc工具类* date 2024/4/6*/ public class JDBCUtils {private static final String URL "jdbc:mysql://127.0.0.1:3306/mybatis";private static final String …...

Svelte Web 框架介绍

Svelte 是一个用于构建网络应用程序的现代框架&#xff0c;它与其他用户界面框架&#xff08;如React和Vue&#xff09;有着本质的不同。Svelte 的核心理念是在构建应用程序时&#xff0c;将大部分工作转移到编译步骤中&#xff0c;而不是在用户的浏览器中运行时处理。这种方法…...

IP地址获取不到的原因是什么?

在数字化时代的今天&#xff0c;互联网已成为我们日常生活和工作中不可或缺的一部分。而IP地址&#xff0c;作为互联网通信的基础&#xff0c;其重要性不言而喻。然而&#xff0c;有时我们可能会遇到IP地址获取不到的问题&#xff0c;这会给我们的网络使用带来诸多不便。那么&a…...

Android APP加固利器:深入了解混淆算法与混淆配置

Android APP 加固是优化 APK 安全性的一种方法&#xff0c;常见的加固方式有混淆代码、加壳、数据加密、动态加载等。下面介绍一下 Android APP 加固的具体实现方式。 混淆代码 使用 ipaguard工具可以对代码进行混淆&#xff0c;使得反编译出来的代码很难阅读和理解&#xff…...

蓝桥杯真题Day47 倒计时6天:6道真题+回溯递归问题

[蓝桥杯 2019 省 A] 糖果 题目描述 糖果店的老板一共有M种口味的糖果出售。为了方便描述&#xff0c;我们将M 种口味编号 1∼ M。小明希望能品尝到所有口味的糖果。遗憾的是老板并不单独出售糖果&#xff0c;而是K 颗一包整包出售。 幸好糖果包装上注明了其中 K 颗糖果的口味…...

通过UDP实现参数配置

来讲讲UDP的一种常见应用 我们知道UDP是一种无连接的网络传输协议&#xff0c;在发送数据时指定目标IP及端口就可以将数据发送出去&#xff0c;因此特别适合用作网络设备发现。 我们可以自定义一个通信端口&#xff0c;假设为55555。我们再制定一个协议用于查询目标设备&#x…...

解析Apache Kafka:在大数据体系中的基本概念和核心组件

关联阅读博客文章&#xff1a;探讨在大数据体系中API的通信机制与工作原理 关联阅读博客文章&#xff1a;深入解析大数据体系中的ETL工作原理及常见组件 关联阅读博客文章&#xff1a;深度剖析&#xff1a;计算机集群在大数据体系中的关键角色和技术要点 关联阅读博客文章&a…...

独角数卡对接码支付收款教程

1、到码支付后台找到支付配置。2、将上面的复制依次填入&#xff0c;具体看下图&#xff0c;随后点立即添加 商户ID商户PID 商户KEY异步不能为空 商户密钥商户密钥...

vuepress-theme-hope 添加谷歌统计代码

最近做了个网站,从 cloudflare 来看访问量,过去 30 天访问量竟然有 1.32k 给我整懵逼了,我寻思不应该呀,毕竟这个网站内容还在慢慢补充中,也没告诉别人,怎么就这么多访问?搜索了下, cloudflare 还会把爬虫的请求也就算进来,所以数据相对来说就不是很准确 想到了把 Google An…...

LabVIEW太赫兹波扫描成像系统

LabVIEW太赫兹波扫描成像系统 随着科技的不断发展&#xff0c;太赫兹波成像技术因其非电离性、高穿透性和高分辨率等特点&#xff0c;在生物医学、材料质量无损检测以及公共安全等领域得到了广泛的应用。然而&#xff0c;在实际操作中&#xff0c;封闭性较高的信号采集软件限制…...

什么是stable diffusion?

&#x1f31f; Stable Diffusion&#xff1a;一种深度学习文本到图像生成模型 &#x1f31f; Stable Diffusion是2022年发布的深度学习文本到图像生成模型&#xff0c;主要用于根据文本的描述产生详细图像。它还可以应用于其他任务&#xff0c;如内补绘制、外补绘制&#xff0…...

KeyguardClockSwitch的父类

KeyguardClockSwitch 定义在KeyguardStatusView中, mClockView findViewById(R.id.keyguard_clock_container);KeyguardClockSwitch的父类为&#xff1a; Class Name: LinearLayout Class Name: KeyguardStatusView Class Name: NotificationPanelView Class Name: Notificat…...

Gradle系列(二):Groovy基础

Gradle系列(二)&#xff1a;Groovy基础 本篇文章继续讲下Groovy一些基础的语法。 1&#xff1a;Map map与List的用法很像&#xff0c;只不过值是一个K:V的键值对。 下面是是Groovy中Map的定义&#xff1a; task testMap { def map [‘width’:1280,‘height’:1960] prin…...

PW1503限流芯片:可达3A限流,保障USB电源管理安全高效

在电源管理领域&#xff0c;开关的性能直接关系到设备的稳定性和安全性。今天&#xff0c;我们将详细解析一款备受关注的超低RDS&#xff08;ON&#xff09;开关——PW1503。它不仅具有可编程的电流限制功能&#xff0c;还集成了多项保护机制&#xff0c;为各类电子设备提供了高…...

深挖苹果Find My技术,伦茨科技ST17H6x芯片赋予产品功能

苹果发布AirTag发布以来&#xff0c;大家都更加注重物品的防丢&#xff0c;苹果的 Find My 就可以查找 iPhone、Mac、AirPods、Apple Watch&#xff0c;如今的Find My已经不单单可以查找苹果的设备&#xff0c;随着第三方设备的加入&#xff0c;将丰富Find My Network的版图。产…...

Web3 革命:揭示区块链技术的全新应用

随着数字化时代的不断发展&#xff0c;区块链技术作为一项颠覆性的创新正在改变着我们的世界。而在这一技术的进步中&#xff0c;Web3正逐渐崭露头角&#xff0c;为区块链技术的应用带来了全新的可能性。本文将探讨Web3革命所揭示的区块链技术全新应用&#xff0c;并展望其未来…...

[实战经验]Mybatis的mapper.xml参数#{para}与#{para, jdbcType=BIGINT}有什么区别?

在MyBatis框架中&#xff0c;传入参数使用#{para}和#{para, jdbcTypeBIGINT}的有什么区别呢&#xff1f; #{para}&#xff1a;这种写法表示使用MyBatis自动推断参数类型&#xff0c;并根据参数的Java类型自动匹配数据库对应的类型。例如&#xff0c;如果参数para的Java类型是Lo…...

高并发下的linux优化

针对高并发服务&#xff0c;对 Linux 内核和网络进行优化可以提高系统的性能和稳定性。本文将深入探讨如何对 Linux 内核和网络进行优化&#xff0c;包括调整内核参数、调整网络性能参数、使用 TCP/IP 协议栈加速技术、下面将介绍一些可用于优化Linux内核和网络的技术&#xff…...

不同设备使用同一个Git账号

想要在公司和家里的电脑上用同一个git账号来pull, push代码 1. 查看原设备的用户名和邮箱 第1种方法&#xff0c; 依次输入 git config user.name git config user.email第2种方法&#xff0c; 输入 cat ~/.gitconfig2. 配置新设备的用户名和邮箱 用户名和邮箱与原设备保持…...

蓝桥杯算法题:区间移位

题目描述 数轴上有n个闭区间&#xff1a;D1,...,Dn。 其中区间Di用一对整数[ai, bi]来描述&#xff0c;满足ai < bi。 已知这些区间的长度之和至少有10000。 所以&#xff0c;通过适当的移动这些区间&#xff0c;你总可以使得他们的“并”覆盖[0, 10000]——也就是说[0, 100…...

Unity3D中Gfx.WaitForPresent优化方案

前言 在Unity中&#xff0c;Gfx.WaitForPresent占用CPU过高通常表示主线程在等待GPU完成渲染&#xff08;即CPU被阻塞&#xff09;&#xff0c;这表明存在GPU瓶颈或垂直同步/帧率设置问题。以下是系统的优化方案&#xff1a; 对惹&#xff0c;这里有一个游戏开发交流小组&…...

循环冗余码校验CRC码 算法步骤+详细实例计算

通信过程&#xff1a;&#xff08;白话解释&#xff09; 我们将原始待发送的消息称为 M M M&#xff0c;依据发送接收消息双方约定的生成多项式 G ( x ) G(x) G(x)&#xff08;意思就是 G &#xff08; x ) G&#xff08;x) G&#xff08;x) 是已知的&#xff09;&#xff0…...

dedecms 织梦自定义表单留言增加ajax验证码功能

增加ajax功能模块&#xff0c;用户不点击提交按钮&#xff0c;只要输入框失去焦点&#xff0c;就会提前提示验证码是否正确。 一&#xff0c;模板上增加验证码 <input name"vdcode"id"vdcode" placeholder"请输入验证码" type"text&quo…...

华为OD机试-食堂供餐-二分法

import java.util.Arrays; import java.util.Scanner;public class DemoTest3 {public static void main(String[] args) {Scanner in new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextLine()) { // 注意 while 处理多个 caseint a in.nextIn…...

tree 树组件大数据卡顿问题优化

问题背景 项目中有用到树组件用来做文件目录&#xff0c;但是由于这个树组件的节点越来越多&#xff0c;导致页面在滚动这个树组件的时候浏览器就很容易卡死。这种问题基本上都是因为dom节点太多&#xff0c;导致的浏览器卡顿&#xff0c;这里很明显就需要用到虚拟列表的技术&…...

html-<abbr> 缩写或首字母缩略词

定义与作用 <abbr> 标签用于表示缩写或首字母缩略词&#xff0c;它可以帮助用户更好地理解缩写的含义&#xff0c;尤其是对于那些不熟悉该缩写的用户。 title 属性的内容提供了缩写的详细说明。当用户将鼠标悬停在缩写上时&#xff0c;会显示一个提示框。 示例&#x…...

音视频——I2S 协议详解

I2S 协议详解 I2S (Inter-IC Sound) 协议是一种串行总线协议&#xff0c;专门用于在数字音频设备之间传输数字音频数据。它由飞利浦&#xff08;Philips&#xff09;公司开发&#xff0c;以其简单、高效和广泛的兼容性而闻名。 1. 信号线 I2S 协议通常使用三根或四根信号线&a…...

基于Java+VUE+MariaDB实现(Web)仿小米商城

仿小米商城 环境安装 nodejs maven JDK11 运行 mvn clean install -DskipTestscd adminmvn spring-boot:runcd ../webmvn spring-boot:runcd ../xiaomi-store-admin-vuenpm installnpm run servecd ../xiaomi-store-vuenpm installnpm run serve 注意&#xff1a;运行前…...

Proxmox Mail Gateway安装指南:从零开始配置高效邮件过滤系统

&#x1f49d;&#x1f49d;&#x1f49d;欢迎莅临我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐&#xff1a;「storms…...

毫米波雷达基础理论(3D+4D)

3D、4D毫米波雷达基础知识及厂商选型 PreView : https://mp.weixin.qq.com/s/bQkju4r6med7I3TBGJI_bQ 1. FMCW毫米波雷达基础知识 主要参考博文&#xff1a; 一文入门汽车毫米波雷达基本原理 &#xff1a;https://mp.weixin.qq.com/s/_EN7A5lKcz2Eh8dLnjE19w 毫米波雷达基础…...