How do I know if a table column can have a null value or not?

In this example we’ll show how to use ResultSetMetaData.isNullable() method to know if a column can be null or not. This method return an integer which values defined in the constants of ResultSetMetaData.columnNullable, ResultSetMetaData.columnNoNulls and ResultSetMetaData.columnNullableUnknown.

package org.kodejava.example.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class IsNullableExample {
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost/testdb";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "";
    
    public static void main(String[] args) throws Exception {
	Connection connection = null;
	try {
	    Class.forName(DRIVER);	    
	    connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
	    
	    Statement statement = connection.createStatement();
	    ResultSet resultSet = statement.executeQuery("SELECT id, username FROM users");
	    
	    //
	    // The ResultSetMetaData is where all metadata related information
	    // for a result set is stored.
	    //
	    ResultSetMetaData metadata = resultSet.getMetaData();	    	   
	    int nullability = metadata.isNullable(1);
	    
	    //
	    // Check the nullability status of a column (ID)
	    //
	    if (nullability == ResultSetMetaData.columnNullable) {
		System.out.println("Columns ID can have a null value");
	    } else if (nullability == ResultSetMetaData.columnNoNulls) {
		System.out.println("Columns ID does not allowed to have a null value");
	    } else if (nullability == ResultSetMetaData.columnNullableUnknown) {
		System.out.println("Nullability unknown");		
	    }
	} catch (SQLException e) {
	    e.printStackTrace();
	} finally {
	    connection.close();
	}
    }
}

Wayan Saryada

Programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply