当前位置:网站首页>如何使用JDBC CallableStatement.wasNull()方法调用来查看最后一个OUT参数的值是否为 SQL NULL
如何使用JDBC CallableStatement.wasNull()方法调用来查看最后一个OUT参数的值是否为 SQL NULL
2022-04-23 10:51:00 【allway2】
此示例向您展示如何使用CallableStatement.wasNull()
方法调用来查看最后一个OUT
参数的值是否为 SQL NULL
。
package org.kodejava.jdbc; import java.math.BigDecimal; import java.sql.Connection; import java.sql.CallableStatement; import java.sql.DriverManager; import java.sql.Types; import java.sql.SQLException; public class WasNullExample { private static final String URL = "jdbc:mysql://localhost/kodejava"; private static final String USERNAME = "root"; private static final String PASSWORD = ""; public static void main(String[] args) { try (Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD)) { // Prepares a call to the sored procedure String query = "call Get_Product_By_Price2(?, ?)"; CallableStatement cb = connection.prepareCall(query); // Sets the input parameter cb.setBigDecimal(1, new BigDecimal("50")); // Registers the OUT parameter cb.registerOutParameter(2, Types.VARCHAR); // Executes the query cb.executeQuery(); // Gets the OUT parameter value cb.getString(2); // Checks if the last OUT parameter has value of SQL NULL. // This method should be called only after calling a // getter method; otherwise, there is no value to use in // determining whether it is null or not. if (cb.wasNull()) { System.out.println("Product has an SQL NULL value"); } else { System.out.println("Product: " + cb.getString(2)); } } catch (SQLException e) { e.printStackTrace(); } } }
这是我们在上面的示例中调用的存储过程脚本。
DELIMITER ;; DROP PROCEDURE IF EXISTS Get_Product_By_Price2;; CREATE PROCEDURE Get_Product_By_Price2( IN product_price DECIMAL(10, 2), OUT product_name VARCHAR(50)) BEGIN SELECT NAME INTO product_name FROM products WHERE price = product_price; END;; DELIMITER ;
Maven 依赖项
<!-- https://search.maven.org/remotecontent?filepath=mysql/mysql-connector-java/8.0.28/mysql-connector-java-8.0.28.jar --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency>
版权声明
本文为[allway2]所创,转载请带上原文链接,感谢
https://blog.csdn.net/allway2/article/details/124359956
边栏推荐
- 期货开户哪个公司好?安全靠谱的期货公司谁能推荐几家?
- VIM usage
- 精彩回顾 | DEEPNOVA x Iceberg Meetup Online《基于Iceberg打造实时数据湖》
- 242. Valid Letter ectopic words (hash table)
- Strongest date regular expression
- Idea - indexing or scanning files to index every time you start
- Anaconda3 installation
- 454. Sum of four numbers (hash table)
- Dirichlet prefix sum (number theory optimization formula sub complexity weapon)
- Image processing - Noise notes
猜你喜欢
Yarn resource scheduler
【leetcode】102.二叉树的层序遍历
Download and installation steps of xshell + xftp
Example of pop-up task progress bar function based on pyqt5
Reading integrity monitoring techniques for vision navigation systems - 5 Results
SQL Server 游标循环表数据
Intuitive understanding entropy
Solution architect's small bag - 5 types of architecture diagrams
SQL Server 递归查询上下级
Net start MySQL MySQL service is starting MySQL service failed to start. The service did not report any errors.
随机推荐
Windows installs redis and sets the redis service to start automatically
Reading integrity monitoring techniques for vision navigation systems - 5 Results
SQLServer 查询数据库死锁
STM32接电机驱动,杜邦线供电,然后反烧问题
242. Valid Letter ectopic words (hash table)
SWAT—Samba WEB管理工具介绍
What are the system events of Jerry's [chapter]
206. Reverse linked list (linked list)
Esp32 learning - use and configuration of GPIO
997. Square of ordered array (array)
定义链表(链表)
Precautions for latex formula
MySQL common statements
mysql同一个表中相同数据怎么合并
Idea - indexing or scanning files to index every time you start
/etc/shadow可以破解吗?
JVM - common parameters
59. Spiral matrix (array)
Solution architect's small bag - 5 types of architecture diagrams
Solution architect's small bag - 5 types of architecture diagrams