Skip to content

Instantly share code, notes, and snippets.

@kazusato
Created September 5, 2019 21:27
Show Gist options
  • Save kazusato/3dfd16dfcc6a9bbd3dd9c38bdd34fe7b to your computer and use it in GitHub Desktop.
Save kazusato/3dfd16dfcc6a9bbd3dd9c38bdd34fe7b to your computer and use it in GitHub Desktop.
PostgreSQLにJDBC接続してfetchSizeを指定してselectするサンプル
package kazusato.dbwork
import java.sql.DriverManager
/**
* This program may be tested with -Xms48m -Xmx48m.
* If fetchSize is not successfully enabled, OutOfMemoryError will occur.
*/
fun main() {
DriverManager.getConnection("jdbc:postgresql://localhost/mydb", "myuser", "mypassword").use { pgConn ->
pgConn.autoCommit = false // fetchSize doesn't work if autoCommit is not disabled
pgConn.prepareStatement(
"select aaa, bbb, ccc, ddd, eee from abc_def"
).use { pgPs ->
pgPs.fetchSize = 10 // set fetch size
pgPs.executeQuery().use { pgRs ->
while (pgRs.next()) {
println(pgRs.getString(1))
println(pgRs.getString(2))
println(pgRs.getString(3))
println(pgRs.getString(4))
println(pgRs.getString(5))
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment