insert null into Integer column using JDBC - code improvements
This commit is contained in:
parent
239da42aab
commit
07af5a2687
|
@ -9,22 +9,18 @@ public class DBConfig {
|
|||
|
||||
private static Connection INSTANCE;
|
||||
|
||||
public static Connection getConnection() throws Exception {
|
||||
public static Connection getConnection() throws SQLException {
|
||||
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();
|
||||
private static void createPersonTable() throws SQLException {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
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.SQLException;
|
||||
import java.sql.Types;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
|
@ -16,7 +14,7 @@ public class InsertNullUnitTest {
|
|||
private final String SQL = "INSERT INTO Person VALUES(?,?,?,?)";
|
||||
|
||||
@Test
|
||||
public void givenNewPerson_whenSetNullIsUsed_thenNewRecordIsCreated() throws Exception {
|
||||
public void givenNewPerson_whenSetNullIsUsed_thenNewRecordIsCreated() throws SQLException {
|
||||
Person person = new Person(1, "John", "Doe", null);
|
||||
|
||||
try (PreparedStatement preparedStatement = DBConfig.getConnection().prepareStatement(SQL)) {
|
||||
|
@ -30,12 +28,11 @@ public class InsertNullUnitTest {
|
|||
int noOfRows = preparedStatement.executeUpdate();
|
||||
|
||||
assertThat(noOfRows, equalTo(1));
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNewPerson_whenSetObjectIsUsed_thenNewRecordIsCreated() throws Exception {
|
||||
public void givenNewPerson_whenSetObjectIsUsed_thenNewRecordIsCreated() throws SQLException {
|
||||
Person person = new Person(2, "John", "Doe", null);
|
||||
|
||||
try (PreparedStatement preparedStatement = DBConfig.getConnection().prepareStatement(SQL)) {
|
||||
|
@ -46,7 +43,6 @@ public class InsertNullUnitTest {
|
|||
int noOfRows = preparedStatement.executeUpdate();
|
||||
|
||||
assertThat(noOfRows, equalTo(1));
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue