Advertisement
Guest User

pgtest.scala

a guest
Feb 16th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.11 KB | None | 0 0
  1. package example
  2.  
  3. import System.nanoTime
  4. import java.sql._
  5. import scala.concurrent.duration._
  6.  
  7. object Hello extends App {
  8.     def profile[R](code: => R, t: Long = nanoTime) = (code, nanoTime - t)
  9.  
  10.     println("Connecting")
  11.     Class.forName("org.postgresql.Driver")
  12.     val connection = DriverManager.getConnection("jdbc:postgresql://localhost/fran?user=fran&password=fran&reWriteBatchedInserts=true")
  13.     connection.setAutoCommit(false)
  14.     // init table:
  15.     println("Creating table")
  16.     val stmt = connection.createStatement()
  17.     stmt.execute("create temporary table t(a serial primary key, b text);")
  18.     stmt.close()
  19.     connection.commit()
  20.  
  21.     println("Inserting")
  22.     val pstmt = connection.prepareStatement("insert into t(b) values(?)")
  23.     val (_, time) = profile {
  24.         for (i <- 1 to 1000000) {
  25.             pstmt.setString(1, i.toString)
  26.             pstmt.addBatch()
  27.         }
  28.         pstmt.executeBatch()
  29.         connection.commit()
  30.     }
  31.     println("Time taken for insertion of 1e6 rows: " + time.nanos.toMillis / 1000.0 + " seconds")
  32.  
  33.     connection.close()
  34. }
  35.  
  36. // vim: set ts=4 sw=4 et:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement