Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. package com.splicemachine.tutorials.jdbc
  2.  
  3. import java.sql.DriverManager
  4. import java.sql.Connection
  5.  
  6. /**
  7. * Simple example of Establishes a connection with splice and executes statements
  8. */
  9. object SampleScalaJDBC{
  10.  
  11. def main(args: Array[String]) {
  12.  
  13. // connect to the database named "splicedb" on the localhost
  14. val driver = "com.splicemachine.db.jdbc.ClientDriver"
  15. val dbUrl = "jdbc:splice://localhost:1527/splicedb;user=splice;password=admin"
  16.  
  17. var connection:Connection = null
  18.  
  19. try {
  20.  
  21. // make the connection
  22. Class.forName(driver)
  23. connection = DriverManager.getConnection(dbUrl)
  24.  
  25. // create the statement
  26. var statement = connection.createStatement()
  27.  
  28. //Create a table
  29. statement.execute("CREATE TABLE MYTESTTABLE(a int, b varchar(30))");
  30. statement.close
  31.  
  32. //Insert data into the table
  33. var pst = connection.prepareStatement("insert into MYTESTTABLE (a,b) values (?,?)")
  34.  
  35. pst.setInt (1, 1)
  36. pst.setString (2, "a")
  37. pst.executeUpdate()
  38.  
  39. pst.clearParameters()
  40. pst.setInt (1, 2)
  41. pst.setString (2, "b")
  42. pst.executeUpdate()
  43.  
  44. pst.clearParameters()
  45. pst.setInt (1, 3)
  46. pst.setString (2, "c")
  47. pst.executeUpdate()
  48.  
  49. pst.clearParameters()
  50. pst.setInt (1, 4)
  51. pst.setString (2, "c")
  52. pst.executeUpdate()
  53.  
  54. pst.clearParameters()
  55. pst.setInt (1, 5)
  56. pst.setString (2, "c")
  57. pst.executeUpdate()
  58.  
  59. pst.close
  60.  
  61. //Read the data
  62. statement = connection.createStatement()
  63. val resultSet = statement.executeQuery("select a, b from MYTESTTABLE")
  64. var counter =0
  65.  
  66. while ( resultSet.next() ) {
  67. counter += 1
  68. val val_a = resultSet.getInt(1)
  69. val val_b = resultSet.getString(2)
  70. println("record=[" + counter + "] a=[" + val_a + "] b=[" +val_b + "]")
  71. }
  72. resultSet.close()
  73. statement.close()
  74. } catch {
  75. case ex : java.sql.SQLException => println("SQLException: "+ex)
  76. } finally {
  77. connection.close()
  78. }
  79. }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement