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);

R Vashi

Leave a Reply

Please log in using one of these methods to post your comment: Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.