Fixed unit tests and equals method for ResultSetLiveTest

This commit is contained in:
Venkata Kiran Surapaneni 2019-01-22 02:20:08 -05:00
parent f5c4e3af74
commit eb8f0cf66c
2 changed files with 344 additions and 305 deletions

View File

@ -1,55 +1,71 @@
package com.baeldung.jdbc; package com.baeldung.jdbc;
import java.util.Objects;
public class Employee { public class Employee {
private int id; private int id;
private String name; private String name;
private String position; private String position;
private double salary; private double salary;
public Employee() { public Employee() {
} }
public Employee(int id, String name, double salary, String position) { public Employee(int id, String name, double salary, String position) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.salary = salary; this.salary = salary;
this.position = position; this.position = position;
} }
public int getId() { public int getId() {
return id; return id;
} }
public void setId(int id) { public void setId(int id) {
this.id = id; this.id = id;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public double getSalary() { public double getSalary() {
return salary; return salary;
} }
public void setSalary(double salary) { public void setSalary(double salary) {
this.salary = salary; this.salary = salary;
} }
public String getPosition() { public String getPosition() {
return position; return position;
} }
public void setPosition(String position) {
this.position = position;
}
@Override
public int hashCode() {
return Objects.hash(id, name, position, salary);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
return id == other.id && Objects.equals(name, other.name) && Objects.equals(position, other.position)
&& Double.doubleToLongBits(salary) == Double.doubleToLongBits(other.salary);
}
public void setPosition(String position) {
this.position = position;
}
@Override
public boolean equals(Object obj) {
return this.getId() == ((Employee) obj).getId();
}
} }

View File

@ -23,303 +23,326 @@ import org.junit.runners.MethodSorters;
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ResultSetLiveTest { public class ResultSetLiveTest {
private static final Logger logger = Logger.getLogger(ResultSetLiveTest.class); private static final Logger logger = Logger.getLogger(ResultSetLiveTest.class);
private final Employee expectedEmployee1 = new Employee(1, "John", 1000.0, "Developer"); private final Employee expectedEmployee1 = new Employee(1, "John", 1000.0, "Developer");
private final Employee updatedEmployee1 = new Employee(1, "John", 1100.0, "Developer"); private final Employee updatedEmployee1 = new Employee(1, "John", 1100.0, "Developer");
private final Employee expectedEmployee2 = new Employee(2, "Chris", 925.0, "DBA"); private final Employee expectedEmployee2 = new Employee(2, "Chris", 925.0, "DBA");
private final int rowCount = 2; private final int rowCount = 2;
private static Connection dbConnection; private static Connection dbConnection;
@BeforeClass @BeforeClass
public static void setup() throws ClassNotFoundException, SQLException { public static void setup() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver"); Class.forName("com.mysql.cj.jdbc.Driver");
dbConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDB?noAccessToProcedureBodies=true", "user1", "pass"); dbConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDB?noAccessToProcedureBodies=true",
String tableSql = "CREATE TABLE IF NOT EXISTS employees (emp_id int PRIMARY KEY AUTO_INCREMENT, name varchar(30), position varchar(30), salary double)"; "user1", "pass");
try (Statement stmt = dbConnection.createStatement()) { String tableSql = "CREATE TABLE IF NOT EXISTS employees (emp_id int PRIMARY KEY AUTO_INCREMENT, name varchar(30), position varchar(30), salary double)";
stmt.execute(tableSql); try (Statement stmt = dbConnection.createStatement()) {
try (PreparedStatement pstmt = dbConnection.prepareStatement("INSERT INTO employees(name, position, salary) values ('John', 'Developer', 1000.0)")) { stmt.execute(tableSql);
pstmt.executeUpdate(); try (PreparedStatement pstmt = dbConnection.prepareStatement(
} "INSERT INTO employees(name, position, salary) values ('John', 'Developer', 1000.0)")) {
} pstmt.executeUpdate();
} }
}
}
@Test @Test
public void givenDbConnectionA_whenARetreiveByColumnNames_thenCorrect() throws SQLException { public void givenDbConnectionA_whenARetreiveByColumnNames_thenCorrect() throws SQLException {
Employee employee = null; Employee employee = null;
try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees"); ResultSet rs = pstmt.executeQuery()) { try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees");
while (rs.next()) { ResultSet rs = pstmt.executeQuery()) {
String name = rs.getString("name"); while (rs.next()) {
Integer empId = rs.getInt("emp_id"); String name = rs.getString("name");
Double salary = rs.getDouble("salary"); Integer empId = rs.getInt("emp_id");
String position = rs.getString("position"); Double salary = rs.getDouble("salary");
employee = new Employee(empId, name, salary, position); String position = rs.getString("position");
} employee = new Employee(empId, name, salary, position);
} }
}
assertEquals("Employee information retreived by column names.", expectedEmployee1, employee); assertEquals("Employee information retreived by column names.", expectedEmployee1, employee);
} }
@Test @Test
public void givenDbConnectionB_whenBRetreiveByColumnIds_thenCorrect() throws SQLException { public void givenDbConnectionB_whenBRetreiveByColumnIds_thenCorrect() throws SQLException {
Employee employee = null; Employee employee = null;
try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees"); ResultSet rs = pstmt.executeQuery()) { try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees");
while (rs.next()) { ResultSet rs = pstmt.executeQuery()) {
Integer empId = rs.getInt(1); while (rs.next()) {
String name = rs.getString(2); Integer empId = rs.getInt(1);
String position = rs.getString(3); String name = rs.getString(2);
Double salary = rs.getDouble(4); String position = rs.getString(3);
employee = new Employee(empId, name, salary, position); Double salary = rs.getDouble(4);
} employee = new Employee(empId, name, salary, position);
} }
}
assertEquals("Employee information retreived by column ids.", employee, expectedEmployee1); assertEquals("Employee information retreived by column ids.", expectedEmployee1, employee);
} }
@Test @Test
public void givenDbConnectionD_whenInsertRow_thenCorrect() throws SQLException { public void givenDbConnectionD_whenInsertRow_thenCorrect() throws SQLException {
int rowCount = 0; int rowCount = 0;
try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) { try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees",
rs.moveToInsertRow(); ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) {
rs.updateString("name", "Venkat"); rs.moveToInsertRow();
rs.updateString("position", "DBA"); rs.updateString("name", "Venkat");
rs.updateDouble("salary", 925.0); rs.updateString("position", "DBA");
rs.insertRow(); rs.updateDouble("salary", 925.0);
rs.moveToCurrentRow(); rs.insertRow();
rs.last(); rs.moveToCurrentRow();
rowCount = rs.getRow(); rs.last();
} rowCount = rs.getRow();
}
assertEquals("Row Count after inserting a row", rowCount, 2); assertEquals("Row Count after inserting a row", 2, rowCount);
} }
private Employee populateResultSet(ResultSet rs) throws SQLException { private Employee populateResultSet(ResultSet rs) throws SQLException {
Employee employee; Employee employee;
String name = rs.getString("name"); String name = rs.getString("name");
Integer empId = rs.getInt("emp_id"); Integer empId = rs.getInt("emp_id");
Double salary = rs.getDouble("salary"); Double salary = rs.getDouble("salary");
String position = rs.getString("position"); String position = rs.getString("position");
employee = new Employee(empId, name, salary, position); employee = new Employee(empId, name, salary, position);
return employee; return employee;
} }
@Test @Test
public void givenDbConnectionE_whenRowCount_thenCorrect() throws SQLException { public void givenDbConnectionE_whenRowCount_thenCorrect() throws SQLException {
int numOfRows = 0; int numOfRows = 0;
try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) { try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees",
rs.last(); ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) {
numOfRows = rs.getRow(); rs.last();
} numOfRows = rs.getRow();
}
assertEquals("Num of rows", numOfRows, rowCount); assertEquals("Num of rows", rowCount, numOfRows);
} }
@Test @Test
public void givenDbConnectionG_whenAbsoluteNavigation_thenCorrect() throws SQLException { public void givenDbConnectionG_whenAbsoluteNavigation_thenCorrect() throws SQLException {
Employee secondEmployee = null; Employee secondEmployee = null;
try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) { try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees",
rs.absolute(2); ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) {
secondEmployee = populateResultSet(rs); rs.absolute(2);
} secondEmployee = populateResultSet(rs);
}
assertEquals("Absolute navigation", secondEmployee, expectedEmployee2); assertEquals("Absolute navigation", expectedEmployee2, secondEmployee);
} }
@Test @Test
public void givenDbConnectionH_whenLastNavigation_thenCorrect() throws SQLException { public void givenDbConnectionH_whenLastNavigation_thenCorrect() throws SQLException {
Employee secondEmployee = null; Employee secondEmployee = null;
try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) { try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees",
rs.last(); ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) {
secondEmployee = populateResultSet(rs); rs.last();
} secondEmployee = populateResultSet(rs);
}
assertEquals("Using Last", secondEmployee, expectedEmployee2); assertEquals("Using Last", expectedEmployee2, secondEmployee);
} }
@Test @Test
public void givenDbConnectionI_whenNavigation_thenCorrect() throws SQLException { public void givenDbConnectionI_whenNavigation_thenCorrect() throws SQLException {
Employee firstEmployee = null; Employee firstEmployee = null;
try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) { try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees",
while (rs.next()) { ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) {
Employee employee = populateResultSet(rs); while (rs.next()) {
} Employee employee = populateResultSet(rs);
rs.beforeFirst(); }
while (rs.next()) { rs.beforeFirst();
Employee employee = populateResultSet(rs); while (rs.next()) {
} Employee employee = populateResultSet(rs);
rs.first(); }
while (rs.next()) { rs.first();
Employee employee = populateResultSet(rs); while (rs.next()) {
} Employee employee = populateResultSet(rs);
while (rs.previous()) { }
Employee employee = populateResultSet(rs); while (rs.previous()) {
} Employee employee = populateResultSet(rs);
rs.afterLast(); }
while (rs.previous()) { rs.afterLast();
Employee employee = populateResultSet(rs); while (rs.previous()) {
} Employee employee = populateResultSet(rs);
rs.last(); }
while (rs.previous()) { rs.last();
firstEmployee = populateResultSet(rs); while (rs.previous()) {
} firstEmployee = populateResultSet(rs);
} }
}
assertEquals("Several Navigation Options", firstEmployee, updatedEmployee1); assertEquals("Several Navigation Options", updatedEmployee1, firstEmployee);
} }
@Test @Test
public void givenDbConnectionJ_whenClosedCursor_thenCorrect() throws SQLException { public void givenDbConnectionJ_whenClosedCursor_thenCorrect() throws SQLException {
int numOfRows = 0; int numOfRows = 0;
dbConnection.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT); dbConnection.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
try (Statement pstmt = dbConnection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE, ResultSet.CLOSE_CURSORS_AT_COMMIT)) { try (Statement pstmt = dbConnection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE,
dbConnection.setAutoCommit(false); ResultSet.CLOSE_CURSORS_AT_COMMIT)) {
pstmt.executeUpdate("INSERT INTO employees (name, salary,position) VALUES ('Chris',2100.0,'Manager')"); dbConnection.setAutoCommit(false);
ResultSet rs = pstmt.executeQuery("select * from employees"); pstmt.executeUpdate("INSERT INTO employees (name, salary,position) VALUES ('Chris',2100.0,'Manager')");
dbConnection.commit(); ResultSet rs = pstmt.executeQuery("select * from employees");
while (rs.next()) { dbConnection.commit();
Employee employee = populateResultSet(rs); while (rs.next()) {
} Employee employee = populateResultSet(rs);
rs.last(); }
numOfRows = rs.getRow(); rs.last();
} numOfRows = rs.getRow();
}
assertEquals("Inserted using close cursor after commit", numOfRows, 3); assertEquals("Inserted using close cursor after commit", 3, numOfRows);
} }
@Test @Test
public void givenDbConnectionK_whenUpdate_thenCorrect() throws SQLException { public void givenDbConnectionK_whenUpdate_thenCorrect() throws SQLException {
int numOfRows = 0; int numOfRows = 0;
try (Statement pstmt = dbConnection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE, ResultSet.HOLD_CURSORS_OVER_COMMIT)) { try (Statement pstmt = dbConnection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE,
dbConnection.setAutoCommit(false); ResultSet.HOLD_CURSORS_OVER_COMMIT)) {
pstmt.executeUpdate("INSERT INTO employees (name, salary,position) VALUES ('Michael',1200.0,'Consultant')"); dbConnection.setAutoCommit(false);
ResultSet rs = pstmt.executeQuery("select * from employees"); pstmt.executeUpdate("INSERT INTO employees (name, salary,position) VALUES ('Michael',1200.0,'Consultant')");
dbConnection.commit(); ResultSet rs = pstmt.executeQuery("select * from employees");
while (rs.next()) { dbConnection.commit();
Employee employee = populateResultSet(rs); while (rs.next()) {
} Employee employee = populateResultSet(rs);
rs.last(); }
numOfRows = rs.getRow(); rs.last();
} numOfRows = rs.getRow();
}
assertEquals("Inserted using hold cursor after commit", numOfRows, 4); assertEquals("Inserted using hold cursor after commit", 4, numOfRows);
} }
@Test @Test
public void givenDbConnectionL_whenDelete_thenCorrect() throws SQLException { public void givenDbConnectionL_whenDelete_thenCorrect() throws SQLException {
int numOfRows = 0; int numOfRows = 0;
try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) { try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees",
rs.absolute(3); ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) {
rs.deleteRow(); rs.absolute(3);
rs.last(); rs.deleteRow();
numOfRows = rs.getRow(); rs.last();
} numOfRows = rs.getRow();
}
assertEquals("Deleted row", numOfRows, 3); assertEquals("Deleted row", 3, numOfRows);
} }
@Test @Test
public void givenDbConnectionC_whenUpdate_thenCorrect() throws SQLException { public void givenDbConnectionC_whenUpdate_thenCorrect() throws SQLException {
Employee employee = null; Employee employee = null;
try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) { try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees",
while (rs.next()) { ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) {
rs.updateDouble("salary", 1100.0); while (rs.next()) {
rs.updateRow(); rs.updateDouble("salary", 1100.0);
rs.refreshRow(); rs.updateRow();
String name = rs.getString("name"); rs.refreshRow();
Integer empId = rs.getInt("emp_id"); String name = rs.getString("name");
Double salary = rs.getDouble("salary"); Integer empId = rs.getInt("emp_id");
String position = rs.getString("position"); Double salary = rs.getDouble("salary");
employee = new Employee(empId, name, salary, position); String position = rs.getString("position");
} employee = new Employee(empId, name, salary, position);
} }
}
assertEquals("Employee information updated successfully.", employee, updatedEmployee1); assertEquals("Employee information updated successfully.", updatedEmployee1, employee);
} }
@Test @Test
public void givenDbConnectionE_whenDBMetaInfo_thenCorrect() throws SQLException { public void givenDbConnectionE_whenDBMetaInfo_thenCorrect() throws SQLException {
DatabaseMetaData dbmd = dbConnection.getMetaData(); DatabaseMetaData dbmd = dbConnection.getMetaData();
boolean supportsTypeForward = dbmd.supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY); boolean supportsTypeForward = dbmd.supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY);
boolean supportsTypeScrollSensitive = dbmd.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE); boolean supportsTypeScrollSensitive = dbmd.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
boolean supportsTypeScrollInSensitive = dbmd.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE); boolean supportsTypeScrollInSensitive = dbmd.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);
boolean supportsCloseCursorsAtCommit = dbmd.supportsResultSetHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT); boolean supportsCloseCursorsAtCommit = dbmd.supportsResultSetHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
boolean supportsHoldCursorsAtCommit = dbmd.supportsResultSetHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT); boolean supportsHoldCursorsAtCommit = dbmd.supportsResultSetHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
boolean concurrency4TypeFwdNConcurReadOnly = dbmd.supportsResultSetConcurrency(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); boolean concurrency4TypeFwdNConcurReadOnly = dbmd.supportsResultSetConcurrency(ResultSet.TYPE_FORWARD_ONLY,
boolean concurrency4TypeFwdNConcurUpdatable = dbmd.supportsResultSetConcurrency(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); ResultSet.CONCUR_READ_ONLY);
boolean concurrency4TypeScrollInSensitiveNConcurUpdatable = dbmd.supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); boolean concurrency4TypeFwdNConcurUpdatable = dbmd.supportsResultSetConcurrency(ResultSet.TYPE_FORWARD_ONLY,
boolean concurrency4TypeScrollInSensitiveNConcurReadOnly = dbmd.supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet.CONCUR_UPDATABLE);
boolean concurrency4TypeScrollSensitiveNConcurUpdatable = dbmd.supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); boolean concurrency4TypeScrollInSensitiveNConcurUpdatable = dbmd
boolean concurrency4TypeScrollSensitiveNConcurReadOnly = dbmd.supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); .supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
int rsHoldability = dbmd.getResultSetHoldability(); boolean concurrency4TypeScrollInSensitiveNConcurReadOnly = dbmd
.supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
boolean concurrency4TypeScrollSensitiveNConcurUpdatable = dbmd
.supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
boolean concurrency4TypeScrollSensitiveNConcurReadOnly = dbmd
.supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
int rsHoldability = dbmd.getResultSetHoldability();
assertEquals("checking scroll sensitivity and concur updates : ", concurrency4TypeScrollInSensitiveNConcurUpdatable, true); assertEquals("checking scroll sensitivity and concur updates : ", true,
} concurrency4TypeScrollInSensitiveNConcurUpdatable);
}
@Test @Test
public void givenDbConnectionF_whenRSMetaInfo_thenCorrect() throws SQLException { public void givenDbConnectionF_whenRSMetaInfo_thenCorrect() throws SQLException {
int columnCount = 0; int columnCount = 0;
try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) { try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees",
ResultSetMetaData metaData = rs.getMetaData(); ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) {
columnCount = metaData.getColumnCount(); ResultSetMetaData metaData = rs.getMetaData();
for (int i = 1; i <= columnCount; i++) { columnCount = metaData.getColumnCount();
String catalogName = metaData.getCatalogName(i); for (int i = 1; i <= columnCount; i++) {
String className = metaData.getColumnClassName(i); String catalogName = metaData.getCatalogName(i);
String label = metaData.getColumnLabel(i); String className = metaData.getColumnClassName(i);
String name = metaData.getColumnName(i); String label = metaData.getColumnLabel(i);
String typeName = metaData.getColumnTypeName(i); String name = metaData.getColumnName(i);
Integer type = metaData.getColumnType(i); String typeName = metaData.getColumnTypeName(i);
String tableName = metaData.getTableName(i); Integer type = metaData.getColumnType(i);
String schemaName = metaData.getSchemaName(i); String tableName = metaData.getTableName(i);
boolean isAutoIncrement = metaData.isAutoIncrement(i); String schemaName = metaData.getSchemaName(i);
boolean isCaseSensitive = metaData.isCaseSensitive(i); boolean isAutoIncrement = metaData.isAutoIncrement(i);
boolean isCurrency = metaData.isCurrency(i); boolean isCaseSensitive = metaData.isCaseSensitive(i);
boolean isDefiniteWritable = metaData.isDefinitelyWritable(i); boolean isCurrency = metaData.isCurrency(i);
boolean isReadOnly = metaData.isReadOnly(i); boolean isDefiniteWritable = metaData.isDefinitelyWritable(i);
boolean isSearchable = metaData.isSearchable(i); boolean isReadOnly = metaData.isReadOnly(i);
boolean isReadable = metaData.isReadOnly(i); boolean isSearchable = metaData.isSearchable(i);
boolean isSigned = metaData.isSigned(i); boolean isReadable = metaData.isReadOnly(i);
boolean isWritable = metaData.isWritable(i); boolean isSigned = metaData.isSigned(i);
int nullable = metaData.isNullable(i); boolean isWritable = metaData.isWritable(i);
} int nullable = metaData.isNullable(i);
} }
}
assertEquals("column count", columnCount, 4); assertEquals("column count", 4, columnCount);
} }
@Test @Test
public void givenDbConnectionM_whenDelete_thenCorrect() throws SQLException { public void givenDbConnectionM_whenDelete_thenCorrect() throws SQLException {
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null; ResultSet rs = null;
List<Employee> listOfEmployees = new ArrayList<Employee>(); List<Employee> listOfEmployees = new ArrayList<Employee>();
try { try {
pstmt = dbConnection.prepareStatement("select * from employees", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); pstmt = dbConnection.prepareStatement("select * from employees", ResultSet.TYPE_FORWARD_ONLY,
pstmt.setFetchSize(2); ResultSet.CONCUR_READ_ONLY);
rs = pstmt.executeQuery(); pstmt.setFetchSize(2);
rs.setFetchSize(1); rs = pstmt.executeQuery();
while (rs.next()) { rs.setFetchSize(1);
Employee employee = populateResultSet(rs); while (rs.next()) {
listOfEmployees.add(employee); Employee employee = populateResultSet(rs);
} listOfEmployees.add(employee);
} catch (Exception e) { }
throw e; } catch (Exception e) {
} finally { throw e;
if (rs != null) } finally {
rs.close(); if (rs != null)
if (pstmt != null) rs.close();
pstmt.close(); if (pstmt != null)
} pstmt.close();
}
assertEquals(listOfEmployees.size(), 3);
}
@AfterClass assertEquals(3, listOfEmployees.size());
public static void closeConnection() throws SQLException { }
PreparedStatement deleteStmt = dbConnection.prepareStatement("drop table employees", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
deleteStmt.execute(); @AfterClass
dbConnection.close(); public static void closeConnection() throws SQLException {
} PreparedStatement deleteStmt = dbConnection.prepareStatement("drop table employees",
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
deleteStmt.execute();
dbConnection.close();
}
} }