Set null vaue
In this chapter you will learn:
Set null value
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
//from ja v a 2 s .c o m
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
PreparedStatement pstmt = null;
conn = getConnection();
String query = "insert into nullable_table(id,string_column, int_column) values(?, ?, ?)";
// create PrepareStatement object
pstmt = conn.prepareStatement(query);
pstmt.setString(1, "0001");
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);
conn.close();
}
private static Connection getConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:mem:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
}
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Statements, ResultSet, Exception, Warning