Created
September 5, 2019 21:27
-
-
Save kazusato/3dfd16dfcc6a9bbd3dd9c38bdd34fe7b to your computer and use it in GitHub Desktop.
PostgreSQLにJDBC接続してfetchSizeを指定してselectするサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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