insert null into Integer column using JDBC

This commit is contained in:
Mladen Savic 2021-05-26 00:14:14 +02:00
parent c24379b927
commit 239da42aab
3 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package com.baeldung.insertnull;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DBConfig {
private static Connection INSTANCE;
public static Connection getConnection() throws Exception {
if (INSTANCE == null) {
Class.forName("org.h2.Driver");
INSTANCE = DriverManager.getConnection("jdbc:h2:mem:insertnull", "user", "password");
createPersonTable();
}
return INSTANCE;
}
private static void createPersonTable() {
try {
Statement statement = INSTANCE.createStatement();
String sql = "CREATE TABLE Person (id INTEGER not null, name VARCHAR(50), lastName VARCHAR(50), age INTEGER, PRIMARY KEY (id))";
statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.insertnull;
public class Person {
private Integer id;
private String name;
private String lastName;
private Integer age;
public Person(Integer id, String name, String lastName, Integer age) {
this.id = id;
this.name = name;
this.lastName = lastName;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}

View File

@ -0,0 +1,52 @@
package com.baeldung.insertnull;
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Types;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
public class InsertNullUnitTest {
private final String SQL = "INSERT INTO Person VALUES(?,?,?,?)";
@Test
public void givenNewPerson_whenSetNullIsUsed_thenNewRecordIsCreated() throws Exception {
Person person = new Person(1, "John", "Doe", null);
try (PreparedStatement preparedStatement = DBConfig.getConnection().prepareStatement(SQL)) {
preparedStatement.setInt(1, person.getId());
preparedStatement.setString(2, person.getName());
preparedStatement.setString(3, person.getLastName());
if (person.getAge() == null)
preparedStatement.setNull(4, Types.INTEGER);
else
preparedStatement.setInt(4, person.getAge());
int noOfRows = preparedStatement.executeUpdate();
assertThat(noOfRows, equalTo(1));
} catch (Exception ignored) {
}
}
@Test
public void givenNewPerson_whenSetObjectIsUsed_thenNewRecordIsCreated() throws Exception {
Person person = new Person(2, "John", "Doe", null);
try (PreparedStatement preparedStatement = DBConfig.getConnection().prepareStatement(SQL)) {
preparedStatement.setInt(1, person.getId());
preparedStatement.setString(2, person.getName());
preparedStatement.setString(3, person.getLastName());
preparedStatement.setObject(4, person.getAge(), Types.INTEGER);
int noOfRows = preparedStatement.executeUpdate();
assertThat(noOfRows, equalTo(1));
} catch (Exception ignored) {
}
}
}