|
| 1 | +package com.baeldung.jdbc; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.sql.CallableStatement; |
| 6 | +import java.sql.Connection; |
| 7 | +import java.sql.DatabaseMetaData; |
| 8 | +import java.sql.DriverManager; |
| 9 | +import java.sql.PreparedStatement; |
| 10 | +import java.sql.ResultSet; |
| 11 | +import java.sql.ResultSetMetaData; |
| 12 | +import java.sql.SQLException; |
| 13 | +import java.sql.Statement; |
| 14 | +import java.sql.Types; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | +import java.util.stream.IntStream; |
| 18 | + |
| 19 | +import org.apache.log4j.Logger; |
| 20 | +import org.junit.After; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +public class JdbcTest { |
| 25 | + |
| 26 | + private static final Logger LOG = Logger.getLogger(JdbcTest.class); |
| 27 | + |
| 28 | + private Connection con; |
| 29 | + |
| 30 | + @Before |
| 31 | + public void setup() throws ClassNotFoundException, SQLException { |
| 32 | + Class.forName("com.mysql.cj.jdbc.Driver"); |
| 33 | + |
| 34 | + con = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDb?noAccessToProcedureBodies=true", "user1", "pass"); |
| 35 | + |
| 36 | + Statement stmt = con.createStatement(); |
| 37 | + |
| 38 | + String tableSql = "CREATE TABLE IF NOT EXISTS employees (emp_id int PRIMARY KEY AUTO_INCREMENT, name varchar(30), position varchar(30), salary double)"; |
| 39 | + stmt.execute(tableSql); |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void whenInsertUpdateRecord_thenCorrect() throws SQLException { |
| 45 | + Statement stmt = con.createStatement(); |
| 46 | + |
| 47 | + String insertSql = "INSERT INTO employees(name, position, salary) values ('john', 'developer', 2000)"; |
| 48 | + stmt.executeUpdate(insertSql); |
| 49 | + |
| 50 | + String selectSql = "SELECT * FROM employees"; |
| 51 | + ResultSet resultSet = stmt.executeQuery(selectSql); |
| 52 | + |
| 53 | + List<Employee> employees = new ArrayList<>(); |
| 54 | + |
| 55 | + while (resultSet.next()) { |
| 56 | + Employee emp = new Employee(); |
| 57 | + emp.setId(resultSet.getInt("emp_id")); |
| 58 | + emp.setName(resultSet.getString("name")); |
| 59 | + emp.setSalary(resultSet.getDouble("salary")); |
| 60 | + emp.setPosition(resultSet.getString("position")); |
| 61 | + employees.add(emp); |
| 62 | + } |
| 63 | + |
| 64 | + assertEquals("employees list size incorrect", 1, employees.size()); |
| 65 | + assertEquals("name incorrect", "john", employees.iterator().next().getName()); |
| 66 | + assertEquals("position incorrect", "developer", employees.iterator().next().getPosition()); |
| 67 | + assertEquals("salary incorrect", 2000, employees.iterator().next().getSalary(), 0.1); |
| 68 | + |
| 69 | + Statement updatableStmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); |
| 70 | + ResultSet updatableResultSet = updatableStmt.executeQuery(selectSql); |
| 71 | + |
| 72 | + updatableResultSet.moveToInsertRow(); |
| 73 | + updatableResultSet.updateString("name", "mark"); |
| 74 | + updatableResultSet.updateString("position", "analyst"); |
| 75 | + updatableResultSet.updateDouble("salary", 2000); |
| 76 | + updatableResultSet.insertRow(); |
| 77 | + |
| 78 | + String updatePositionSql = "UPDATE employees SET position=? WHERE emp_id=?"; |
| 79 | + PreparedStatement pstmt = con.prepareStatement(updatePositionSql); |
| 80 | + pstmt.setString(1, "lead developer"); |
| 81 | + pstmt.setInt(2, 1); |
| 82 | + |
| 83 | + String updateSalarySql = "UPDATE employees SET salary=? WHERE emp_id=?"; |
| 84 | + PreparedStatement pstmt2 = con.prepareStatement(updateSalarySql); |
| 85 | + pstmt.setDouble(1, 3000); |
| 86 | + pstmt.setInt(2, 1); |
| 87 | + |
| 88 | + boolean autoCommit = con.getAutoCommit(); |
| 89 | + |
| 90 | + try { |
| 91 | + con.setAutoCommit(false); |
| 92 | + pstmt.executeUpdate(); |
| 93 | + pstmt2.executeUpdate(); |
| 94 | + con.commit(); |
| 95 | + } catch (SQLException exc) { |
| 96 | + con.rollback(); |
| 97 | + } finally { |
| 98 | + con.setAutoCommit(autoCommit); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void whenCallProcedure_thenCorrect() { |
| 104 | + |
| 105 | + try { |
| 106 | + String preparedSql = "{call insertEmployee(?,?,?,?)}"; |
| 107 | + CallableStatement cstmt = con.prepareCall(preparedSql); |
| 108 | + cstmt.setString(2, "ana"); |
| 109 | + cstmt.setString(3, "tester"); |
| 110 | + cstmt.setDouble(4, 2000); |
| 111 | + cstmt.registerOutParameter(1, Types.INTEGER); |
| 112 | + cstmt.execute(); |
| 113 | + int new_id = cstmt.getInt(1); |
| 114 | + assertTrue(new_id > 0); |
| 115 | + } catch (SQLException exc) { |
| 116 | + LOG.error("Procedure incorrect or does not exist!"); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void whenReadMetadata_thenCorrect() throws SQLException { |
| 122 | + |
| 123 | + DatabaseMetaData dbmd = con.getMetaData(); |
| 124 | + ResultSet tablesResultSet = dbmd.getTables(null, null, "%", null); |
| 125 | + while (tablesResultSet.next()) { |
| 126 | + LOG.info(tablesResultSet.getString("TABLE_NAME")); |
| 127 | + } |
| 128 | + |
| 129 | + String selectSql = "SELECT * FROM employees"; |
| 130 | + Statement stmt = con.createStatement(); |
| 131 | + ResultSet resultSet = stmt.executeQuery(selectSql); |
| 132 | + ResultSetMetaData rsmd = resultSet.getMetaData(); |
| 133 | + int nrColumns = rsmd.getColumnCount(); |
| 134 | + assertEquals(nrColumns, 4); |
| 135 | + |
| 136 | + IntStream.range(1, nrColumns).forEach(i -> { |
| 137 | + try { |
| 138 | + LOG.info(rsmd.getColumnName(i)); |
| 139 | + } catch (SQLException e) { |
| 140 | + e.printStackTrace(); |
| 141 | + } |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + @After |
| 146 | + public void closeConnection() throws SQLException { |
| 147 | + |
| 148 | + Statement updatableStmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); |
| 149 | + ResultSet updatableResultSet = updatableStmt.executeQuery("SELECT * FROM employees"); |
| 150 | + |
| 151 | + while (updatableResultSet.next()) { |
| 152 | + updatableResultSet.deleteRow(); |
| 153 | + } |
| 154 | + |
| 155 | + con.close(); |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments