使用JDBC将null插入Integer列 [英] Inserting null to an Integer column using JDBC

查看:1051
本文介绍了使用JDBC将null插入Integer列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL列 PROTOCOL ,它可以为空并且对表有约束

I have an sql column PROTOCOL, it is nullable and has a constraint on the table

PROTOCOL IN (1, 2, 3)

此外,因为它可以为空,我想设置并获取表中的空值

Also since it is nullable, I want to set and get null values into table

但我不能 setInt getInt 为null。如何使用JDBC将null设置为null

But I cannot do setInt and getInt as null. How to set a null to column as null using JDBC

setInt(4,null);


推荐答案

尝试使用。

   pst.setNull(4, java.sql.Types.INTEGER);  //pst is prepared statement instance.

Interface PreparedStatement.setNull API

将java.sql.Types映射到SQL类型

PS :编辑以反映Java 8更新。

P.S. : Edited to reflect Java 8 updates.

这篇关于使用JDBC将null插入Integer列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆