惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
量子位
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Jina AI
Jina AI
V
Visual Studio Blog
C
Check Point Blog
博客园 - 聂微东
博客园 - 叶小钗
Microsoft Security Blog
Microsoft Security Blog
E
Exploit-DB.com RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
Netflix TechBlog - Medium
Recorded Future
Recorded Future
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
Spread Privacy
Spread Privacy
Cisco Talos Blog
Cisco Talos Blog
C
Comments on: Blog
N
News and Events Feed by Topic
L
Lohrmann on Cybersecurity
小众软件
小众软件
H
Heimdal Security Blog
云风的 BLOG
云风的 BLOG
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
The GitHub Blog
The GitHub Blog
Security Latest
Security Latest
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
阮一峰的网络日志
阮一峰的网络日志
H
Hacker News: Front Page
D
Docker
N
News and Events Feed by Topic
Application and Cybersecurity Blog
Application and Cybersecurity Blog
P
Privacy & Cybersecurity Law Blog
S
Schneier on Security
T
Troy Hunt's Blog
MyScale Blog
MyScale Blog
The Register - Security
The Register - Security
Simon Willison's Weblog
Simon Willison's Weblog
L
LangChain Blog
T
The Exploit Database - CXSecurity.com
D
Darknet – Hacking Tools, Hacker News & Cyber Security
NISL@THU
NISL@THU
TaoSecurity Blog
TaoSecurity Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Privacy International News Feed
Blog — PlanetScale
Blog — PlanetScale

Sunshine

SpringBoot定时任务@Scheduled注解详解 | Sunshine 大端模式、小端模式解析 | Sunshine 机器数、原码、反码、补码解析 | Sunshine Spring事件驱动模型实践 | Sunshine IDEA好用的几款插件 | Sunshine Linux使用yum安装MySQL详细步骤(CentOS7.3) | Sunshine Windows下MySQL解压版安装配置详细步骤 | Sunshine Java使用JDBC连接SQLServer数据库(二) | Sunshine Java使用JDBC连接SQLServer数据库(一) | Sunshine Git基本操作整理 | Sunshine Oracle序列创建和使用 | Sunshine Git批量清理本地分支 | Sunshine GitPages+Hexo搭建个人博客 | Sunshine
Statement.RETURN_GENERATED_KEYS获取自增id踩坑记录 | Sunshine
DongyangHu · 2019-11-29 · via Sunshine

问题背景

项目中多处使用原生JDBC进行数据插入,进行数据insert时会预先生成一个id主键再进行插入,并通过Statement.RETURN_GENERATED_KEYS的方式返回主键id,都能获取正确结果。偶然一次使用该方式对返回值进行处理时,获取到的返回值是受影响的rows,故进行排查。

相关代码

1
PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

问题原因

经过排查,发现能够成功获取主键id的代码中,对应的表结构主键是自增的,设有AUTO_INCREMENT属性,而不能返回主键id的代码,相应表结构并没有自增属性。

源码中说明

总结

在使用Statement.RETURN_GENERATED_KEYS时,需要特别注意,被操作的数据表主键id必须设置AUTO_INCREMENT属性。数据库自增的id和手动插入的id均可正常返回。

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Sunshine