当前位置:网站首页>How to use JDBC callablestatement The wasnull () method is called to check whether the value of the last out parameter is SQL null
How to use JDBC callablestatement The wasnull () method is called to check whether the value of the last out parameter is SQL null
2022-04-23 10:56:00 【allway2】
This example shows you how to use CallableStatement.wasNull() Method is called to view the last OUT Whether the value of the parameter is 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(); } } }
This is the stored procedure script we called in the above example. .
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 Dependencies
<!-- 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://yzsam.com/2022/04/202204231051098115.html
边栏推荐
猜你喜欢

Promise详解

Introduction to data analysis 𞓜 kaggle Titanic mission (III) - > explore data analysis

Visual common drawing (V) scatter diagram

Net start MySQL MySQL service is starting MySQL service failed to start. The service did not report any errors.

Visual common drawing (III) area map

C language - custom type

SQL Server 游标循环表数据

A diary of dishes | 238 Product of arrays other than itself

Notes on concurrent programming of vegetables (V) thread safety and lock solution

Visualization Road (11) detailed explanation of Matplotlib color
随机推荐
Qinglong panel pull library command update [April 20, 2022] collection is not lost
Introduction to data analysis 𞓜 kaggle Titanic mission (IV) - > data cleaning and feature processing
Gets the current time in character format
remote: Support for password authentication was removed on August 13, 2021.
454. Sum of four numbers (hash table)
Visualized common drawing (II) line chart
The courses bought at a high price are open! PHPer data sharing
26. 删除有序数组中的重复项
期货开户哪个公司好?安全靠谱的期货公司谁能推荐几家?
Ueditor -- limitation of 4m size of image upload component
1. Sum of two numbers (hash table)
Read integrity monitoring techniques for vision navigation systems - 4 multiple faults in vision system
Arbitrary file reading vulnerability exploitation Guide
CentOS/Linux安装MySQL
Visualization Road (11) detailed explanation of Matplotlib color
Visual common drawing (III) area map
Learning Notes 6 - Summary of several deep learning convolutional neural networks
A diary of dishes | 238 Product of arrays other than itself
Deploy jar package
202. Happy number