JDBC Query

If we need to we can get the JDBC Connection and use raw JDBC.

Note that this in practice is rare (occurring less than 1% in recent applications). It is nice that we can go to raw JDBC if we need to but yes, most applications won't need to go down to raw JDBC level.

try (Transaction transaction = DB.beginTransaction()) {

  // obtain the JDBC connection from the transaction
  final Connection connection = transaction.getConnection();

  // use PreparedStatement etc
  String sql = "select id, name from customer where name like ?";
  try (PreparedStatement stmt = connection.prepareStatement(sql)) {
   stmt.setString(1, "Rob");
    try (ResultSet rset = stmt.executeQuery()) {
      while (rset.next()) {
        final long id = rset.getLong(1);
        final String name = rset.getString(2);
        ...
      }
    }
  }

  transaction.commit();
}
DB.beginTransaction().use { transaction ->

  // obtain the JDBC connection from the transaction
  val connection = transaction.connection

  // use PreparedStatement etc
  val sql = "select id, name from customer where name like ?"
  connection.prepareStatement(sql).use { stmt ->

    stmt.setString(1, "Rob")
    stmt.executeQuery().use { rset ->
      while (rset.next()) {
        val id = rset.getLong(1)
        val name = rset.getString(2)
        ...
      }
    }
  }

  transaction.commit()
}

Edit Page