Using PreparedStatement to set Null for char/string columns JDBC

Hi All,

sometime we need to pass null values to DB procedure parameters, or query columns. There is a very simple way to achieve that.

conn = getConnection(); // gets connection form DB factory
String query = “insert into table(id,string_column, int_column) values(?, ?, ?)”;

pstmt = conn.prepareStatement(query); // create PrepareStatement object
pstmt.setString(1, id);
pstmt.setNull(2, java.sql.Types.VARCHAR);
pstmt.setNull(3, java.sql.Types.INTEGER);

// execute query, and return number of rows created
int rowCount = pstmt.executeUpdate();
System.out.println(“rowCount=” + rowCount);


Thanks
R Vashi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s