当前位置:网站首页>JDBC – PreparedStatement – 如何设置 Null 值?
JDBC – PreparedStatement – 如何设置 Null 值?
2022-04-23 10:51:00 【allway2】
介绍
Java 数据库连接 (JDBC) API 允许 Java 程序员访问不同的关系和 NoSQL 数据库,如 Oracle、MySQL、SQL Server 等。它有助于存储、访问和操作存储在这些数据库中的数据。
在这篇文章中,我们将探讨如何在 PreparedStatement 中设置 NULL 值。
空字符串数据类型
其他数据类型,如 Integer、Float、Double、Date 等。
例子
String name = null;
Long id = null;
PreparedStatement ps = connection.prepareStatement("select * from person where first_name = ? and id = ?");
ps.setString(1,name);
ps.setLong(2, id);
解决方案
if( id == null ) {
ps.setNull(2, Types.INTEGER);
}else {
ps.setLong(2, id);
}
完整代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;
public class JDBCNullExample {
public static void main(String[] args) throws Exception{
Connection connection = null;
try {
connection = openConnection();
String name = null;
Long id = null;
PreparedStatement ps = connection.prepareStatement("select * from person
where first_name = ? and id = ?");
ps.setString(1,name);
if(id == null) {
ps.setNull(2, Types.INTEGER);
}else {
ps.setLong(2, id);
}
ResultSet rs = ps.executeQuery();
System.out.println(rs.getFetchSize());
rs.close();
}catch(Exception exception) {
exception.printStackTrace();
}finally {
if(connection != null) {
connection.close();
}
}
}
private static Connection openConnection() throws Exception{
Class.forName("oracle.jdbc.OracleDriver");
return DriverManager.getConnection("jdbc:oracle:thin:@IP:orcl", "user","password");
}
}
结论
版权声明
本文为[allway2]所创,转载请带上原文链接,感谢
https://blog.csdn.net/allway2/article/details/124359797
边栏推荐
- [Niuke challenge 47] C. conditions (BitSet acceleration Floyd)
- The difference between restful and soap
- SWAT—Samba WEB管理工具介绍
- 使用zerotier让异地设备组局域网
- VIM usage
- Yarn resource scheduler
- Idea - indexing or scanning files to index every time you start
- ID number verification system based on visual structure - Raspberry implementation
- Source insight 4.0 FAQs
- 主流手机分辨率与尺寸
猜你喜欢
How to quickly download vscode
Jerry's more accurate determination of abnormal address [chapter]
Introduction to wechat applet, development history, advantages of applet, application account, development tools, initial knowledge of wxml file and wxss file
【leetcode】199.二叉树的右视图
得到知识服务app原型设计比较与实践
Charles 功能介绍和使用教程
【leetcode】102. Sequence traversal of binary tree
How can swagger2 custom parameter annotations not be displayed
How does the swagger2 interface import postman
SQL Server cursor circular table data
随机推荐
Full stack cross compilation x86 completion process experience sharing
209、长度最小的子数组(数组)
【leetcode】199.二叉树的右视图
Xshell+Xftp 下载安装步骤
Let the LAN group use the remote device
Dirichlet prefix sum (number theory optimization formula sub complexity weapon)
一个微博数据库设计带来的简单思考
Leetcode22: bracket generation
Swagger2 自定义参数注解如何不显示
Notes on concurrent programming of vegetables (V) thread safety and lock solution
Deploy jar package
软件测试人员,如何优秀的提Bug?
203、移出链表元素(链表)
HuggingFace
Detailed explanation of MapReduce calculation process
微信小程序简介、发展史、小程序的优点、申请账号、开发工具、初识wxml文件和wxss文件
Charles function introduction and use tutorial
使用zerotier让异地设备组局域网
Qinglong panel pull library command update [April 20, 2022] collection is not lost
部署jar包