Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package example
- import System.nanoTime
- import java.sql._
- import scala.concurrent.duration._
- object Hello extends App {
- def profile[R](code: => R, t: Long = nanoTime) = (code, nanoTime - t)
- println("Connecting")
- Class.forName("org.postgresql.Driver")
- val connection = DriverManager.getConnection("jdbc:postgresql://localhost/fran?user=fran&password=fran&reWriteBatchedInserts=true")
- connection.setAutoCommit(false)
- // init table:
- println("Creating table")
- val stmt = connection.createStatement()
- stmt.execute("create temporary table t(a serial primary key, b text);")
- stmt.close()
- connection.commit()
- println("Inserting")
- val pstmt = connection.prepareStatement("insert into t(b) values(?)")
- val (_, time) = profile {
- for (i <- 1 to 1000000) {
- pstmt.setString(1, i.toString)
- pstmt.addBatch()
- }
- pstmt.executeBatch()
- connection.commit()
- }
- println("Time taken for insertion of 1e6 rows: " + time.nanos.toMillis / 1000.0 + " seconds")
- connection.close()
- }
- // vim: set ts=4 sw=4 et:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement