
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java ResultSet wasNull Method with Example
When we execute certain SQL queries (SELECT query in general) they return tabular data.
The java.sql.ResultSet interface represents such tabular data returned by the SQL statements.
i.e. the ResultSet object holds the tabular data returned by the methods that execute the statements which quires the database (executeQuery() method of the Statement interface in general).
The ResultSet object has a cursor/pointer which points to the current row. Initially this cursor is positioned before first row.
The wasNull() method of the ResultSet interface determines whether the last column read had a Null value. i.e. whenever you read the contents of a column of the ResultSet using the getter methods (getInt(), getString etc...) you can determine whether it (column) contains null values, using the wasNull() method.
This method returns a boolean value specifying whether the column just read contains null values. If there are null values in the column this method returns true, else it returns false.
Let us create a table with name MyPlayers in MySQL database using CREATE statement as shown below -
CREATE TABLE MyPlayers( ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Date_Of_Birth date, Place_Of_Birth VARCHAR(255), Country VARCHAR(255), PRIMARY KEY (ID) );
Now, we will insert 7 records in MyPlayers table using INSERT statements -
insert into MyPlayers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India'); insert into MyPlayers values(2, 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica'); insert into MyPlayers values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka'); insert into MyPlayers values(4, 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India'); insert into MyPlayers values(5, 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India'); insert into MyPlayers values(6, 'Ravindra', 'Jadeja', DATE('1988-12-06'), 'Nagpur', 'India'); insert into MyPlayers values(7, 'James', 'Anderson', DATE('1982-06-30'), 'Burnley', 'England');
Following JDBC program establishes connection with the database, inserts a new record with null values, into the MyPlayers table, retrieves its contents into a ResultSet object and, prints the contents of the record that contains null values using the wasNull() method.
Example
import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ResultSet_wasNull { public static void main(String args[]) throws SQLException { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating the Statement Statement stmt = con.createStatement(); //Insert a row into the MyPlayers table PreparedStatement pstmt = con.prepareStatement("INSERT INTO MyPlayers values (?, ?, ?, ?, ?, ? )"); pstmt.setInt(1, 8); pstmt.setString(2, "Ryan"); pstmt.setString(3, "McLaren"); pstmt.setDate(4, new Date(413596800000L)); pstmt.setString(5, "Kumberly"); pstmt.setNull(6, java.sql.Types.VARCHAR); pstmt.executeUpdate(); //Query to retrieve records String query = "Select * from MyPlayers"; //Executing the query ResultSet rs = stmt.executeQuery(query); //Verifying whether last row read was null while(rs.next()) { int id = rs.getInt("ID"); String first_name = rs.getString("First_Name"); String last_name = rs.getString("Last_Name"); Date date_of_birth = rs.getDate("Date_Of_Birth"); String place_of_birth = rs.getString("Place_Of_Birth"); String country = rs.getString("Country"); boolean bool = rs.wasNull(); if(bool) { System.out.print("Id: "+id+", "); System.out.print("First Name: "+first_name+", "); System.out.print("Last Name: "+last_name+", "); System.out.print("Date Of Birth: "+date_of_birth+", "); System.out.print("Place Of Birth: "+place_of_birth+", "); System.out.print("Country: "+country); System.out.println(" "); } } } }
Output
Connection established...... Id: 8, First Name: Ryan, Last Name: McLaren, Date Of Birth: 1983-02-09, Place Of Birth: Kumberly, Country: null