Java JDBC ResultSet的接口方法wasNULL()方法来判断接受到的SQL查询值是否为NULL,该方法返回值为Boolean类型

时间:2024-04-09 08:21:43
  • Java ResultSet wasNull()方法与示例

    当我们执行某些SQL查询(通常是SELECT查询)时,它们返回表格数据。

    java.sql.ResultSet中的接口表示由SQL语句返回这样的表格数据。

    ResultSet 对象保存由执行查询数据库的语句的方法返回的表格数据(通常为Statement接口的executeQuery()方法)。

    ResultSet对象具有指向当前行的光标/指针。最初,此光标位于第一行之前。

    Java JDBC ResultSet的接口方法wasNULL()方法来判断接受到的SQL查询值是否为NULL,该方法返回值为Boolean类型

    ResultSet接口的wasNull()方法确定最后读取的列是否具有Null值。即,每当您使用getter方法(getInt(),getString...)读取ResultSet的列的内容时,都可以使用wasNull()方法确定其(列)是否包含空值。

    此方法返回一个布尔值,该值指定刚读取的列是否包含空值。如果列中有空值,则此方法返回true,否则返回false

    让我们使用CREATE语句在MySQL数据库中创建一个名为MyPlayers的表,如下所示

    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)

    );

    现在,我们将使用INSERT语句在MyPlayers表中插入7条记录-

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

    接下来的JDBC程序建立与数据库的连接,将具有空值的新记录插入MyPlayers表中,将其内容检索到ResultSet对象中,并使用wasNull()方法打印包含空值的记录的内容。

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

             }

          }

       }

    }

    输出

    Connection established......

    Id: 8, First Name: Ryan, Last Name: McLaren, Date Of Birth: 1983-02-09, Place Of Birth: Kumberly, Country: null

     

 

在上面例子中,会判断Country列是否为SQL NULL。所以,wasNULL()方法会判断最后一个接收到的最后一个值是否为空