JDBC クエリ

必要な場合は、JDBC 接続を取得して生の JDBC を使用できます。

最近のアプリケーションでは実際にはこれはまれで 1% 未満であることに注意してください。必要に応じて生の JDBC にアクセスできるのは結構なことですが、ほとんどのアプリケーションでは生の JDBC レベルにまで下げる必要はありません。

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()
}