Fixed issues with adding the 2nd record

This commit is contained in:
Venkata Kiran Surapaneni 2019-01-24 13:16:56 -05:00
parent eb8f0cf66c
commit 48bad82439
1 changed files with 268 additions and 286 deletions

View File

@ -20,6 +20,8 @@ import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import junit.framework.Assert;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ResultSetLiveTest {
@ -38,23 +40,20 @@ public class ResultSetLiveTest {
@BeforeClass
public static void setup() throws ClassNotFoundException, SQLException {
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", "user1", "pass");
String tableSql = "CREATE TABLE IF NOT EXISTS employees (emp_id int PRIMARY KEY AUTO_INCREMENT, name varchar(30), position varchar(30), salary double)";
try (Statement stmt = dbConnection.createStatement()) {
stmt.execute(tableSql);
try (PreparedStatement pstmt = dbConnection.prepareStatement(
"INSERT INTO employees(name, position, salary) values ('John', 'Developer', 1000.0)")) {
try (PreparedStatement pstmt = dbConnection.prepareStatement("INSERT INTO employees(name, position, salary) values ('John', 'Developer', 1000.0)")) {
pstmt.executeUpdate();
}
}
}
@Test
public void givenDbConnectionA_whenARetreiveByColumnNames_thenCorrect() throws SQLException {
public void givenDbConnectionA_whenRetreiveByColumnNames_thenCorrect() throws SQLException {
Employee employee = null;
try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees");
ResultSet rs = pstmt.executeQuery()) {
try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees"); ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
String name = rs.getString("name");
Integer empId = rs.getInt("emp_id");
@ -68,10 +67,9 @@ public class ResultSetLiveTest {
}
@Test
public void givenDbConnectionB_whenBRetreiveByColumnIds_thenCorrect() throws SQLException {
public void givenDbConnectionB_whenRetreiveByColumnIds_thenCorrect() throws SQLException {
Employee employee = null;
try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees");
ResultSet rs = pstmt.executeQuery()) {
try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees"); ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
Integer empId = rs.getInt(1);
String name = rs.getString(2);
@ -87,10 +85,9 @@ public class ResultSetLiveTest {
@Test
public void givenDbConnectionD_whenInsertRow_thenCorrect() throws SQLException {
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", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) {
rs.moveToInsertRow();
rs.updateString("name", "Venkat");
rs.updateString("name", "Chris");
rs.updateString("position", "DBA");
rs.updateDouble("salary", 925.0);
rs.insertRow();
@ -115,8 +112,7 @@ public class ResultSetLiveTest {
@Test
public void givenDbConnectionE_whenRowCount_thenCorrect() throws SQLException {
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", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) {
rs.last();
numOfRows = rs.getRow();
}
@ -127,8 +123,7 @@ public class ResultSetLiveTest {
@Test
public void givenDbConnectionG_whenAbsoluteNavigation_thenCorrect() throws SQLException {
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", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) {
rs.absolute(2);
secondEmployee = populateResultSet(rs);
}
@ -139,8 +134,7 @@ public class ResultSetLiveTest {
@Test
public void givenDbConnectionH_whenLastNavigation_thenCorrect() throws SQLException {
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", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) {
rs.last();
secondEmployee = populateResultSet(rs);
}
@ -151,8 +145,7 @@ public class ResultSetLiveTest {
@Test
public void givenDbConnectionI_whenNavigation_thenCorrect() throws SQLException {
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", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
Employee employee = populateResultSet(rs);
}
@ -184,8 +177,7 @@ public class ResultSetLiveTest {
public void givenDbConnectionJ_whenClosedCursor_thenCorrect() throws SQLException {
int numOfRows = 0;
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, ResultSet.CLOSE_CURSORS_AT_COMMIT)) {
dbConnection.setAutoCommit(false);
pstmt.executeUpdate("INSERT INTO employees (name, salary,position) VALUES ('Chris',2100.0,'Manager')");
ResultSet rs = pstmt.executeQuery("select * from employees");
@ -203,8 +195,7 @@ public class ResultSetLiveTest {
@Test
public void givenDbConnectionK_whenUpdate_thenCorrect() throws SQLException {
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, ResultSet.HOLD_CURSORS_OVER_COMMIT)) {
dbConnection.setAutoCommit(false);
pstmt.executeUpdate("INSERT INTO employees (name, salary,position) VALUES ('Michael',1200.0,'Consultant')");
ResultSet rs = pstmt.executeQuery("select * from employees");
@ -222,8 +213,7 @@ public class ResultSetLiveTest {
@Test
public void givenDbConnectionL_whenDelete_thenCorrect() throws SQLException {
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", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) {
rs.absolute(3);
rs.deleteRow();
rs.last();
@ -236,12 +226,16 @@ public class ResultSetLiveTest {
@Test
public void givenDbConnectionC_whenUpdate_thenCorrect() throws SQLException {
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", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
Assert.assertEquals(1000.0, rs.getDouble("salary"));
rs.updateDouble("salary", 1100.0);
rs.updateRow();
rs.refreshRow();
Assert.assertEquals(1100.0, rs.getDouble("salary"));
String name = rs.getString("name");
Integer empId = rs.getInt("emp_id");
Double salary = rs.getDouble("salary");
@ -249,8 +243,6 @@ public class ResultSetLiveTest {
employee = new Employee(empId, name, salary, position);
}
}
assertEquals("Employee information updated successfully.", updatedEmployee1, employee);
}
@Test
@ -261,29 +253,21 @@ public class ResultSetLiveTest {
boolean supportsTypeScrollInSensitive = dbmd.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);
boolean supportsCloseCursorsAtCommit = dbmd.supportsResultSetHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
boolean supportsHoldCursorsAtCommit = dbmd.supportsResultSetHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
boolean concurrency4TypeFwdNConcurReadOnly = dbmd.supportsResultSetConcurrency(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
boolean concurrency4TypeFwdNConcurUpdatable = dbmd.supportsResultSetConcurrency(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
boolean concurrency4TypeScrollInSensitiveNConcurUpdatable = dbmd
.supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
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);
boolean concurrency4TypeFwdNConcurReadOnly = dbmd.supportsResultSetConcurrency(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
boolean concurrency4TypeFwdNConcurUpdatable = dbmd.supportsResultSetConcurrency(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
boolean concurrency4TypeScrollInSensitiveNConcurUpdatable = dbmd.supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
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 : ", true,
concurrency4TypeScrollInSensitiveNConcurUpdatable);
assertEquals("checking scroll sensitivity and concur updates : ", true, concurrency4TypeScrollInSensitiveNConcurUpdatable);
}
@Test
public void givenDbConnectionF_whenRSMetaInfo_thenCorrect() throws SQLException {
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", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) {
ResultSetMetaData metaData = rs.getMetaData();
columnCount = metaData.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
@ -317,8 +301,7 @@ public class ResultSetLiveTest {
ResultSet rs = null;
List<Employee> listOfEmployees = new ArrayList<Employee>();
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, ResultSet.CONCUR_READ_ONLY);
pstmt.setFetchSize(2);
rs = pstmt.executeQuery();
rs.setFetchSize(1);
@ -340,8 +323,7 @@ public class ResultSetLiveTest {
@AfterClass
public static void closeConnection() throws SQLException {
PreparedStatement deleteStmt = dbConnection.prepareStatement("drop table employees",
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
PreparedStatement deleteStmt = dbConnection.prepareStatement("drop table employees", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
deleteStmt.execute();
dbConnection.close();
}