Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. ##################################################################
  2. # Simple Jython program to demonstrate the Splice Machine
  3. # database and exercise common operations.
  4. #################################################################
  5. from java.sql import DriverManager
  6. from java.lang import Class
  7. from java.util import Properties
  8. from java.math import BigDecimal
  9. from java.lang import Boolean
  10. import datetime
  11.  
  12. url = 'jdbc:splice://localhost:1527/splicedb'
  13. driver = 'com.splicemachine.db.jdbc.ClientDriver'
  14. user = 'splice';
  15. pwd = 'admin';
  16. # Sql statement constants
  17. SQL_DROP = "DROP TABLE IF EXISTS SPLICE_DEMO"
  18. # Demo table with most common datatypes
  19. SQL_CREATE = """
  20. CREATE TABLE SPLICE_DEMO (
  21. C_INTEGER INTEGER,
  22. C_BIGINT BIGINT,
  23. C_NUMERIC NUMERIC (10,4),
  24. C_DOUBLE DOUBLE,
  25. C_LONG_VARCHAR LONG VARCHAR,
  26. C_VARCHAR VARCHAR(100),
  27. C_CHAR CHAR(10),
  28. C_DATE DATE,
  29. C_TIMESTAMP TIMESTAMP,
  30. C_BOOLEAN BOOLEAN)"""
  31. SQL_INSERT = """
  32. INSERT INTO SPLICE_DEMO
  33. (C_INTEGER, C_BIGINT, C_NUMERIC, C_COUBLE, C_LONG_VARCHAR, C_VARCHAR, C_CHAR, C_DATE, C_TIMESTAMP, C_BOOLEAN)
  34. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"""
  35. SQL_DELETE = "DELETE FROM SPLICE_DEMO WHERE C_INTEGER = 1"
  36. SQL_SELECT = "SELECT * FROM SPLICE_DEMO"
  37.  
  38. ##################################################################
  39. # Open a jdbc connection
  40. ##################################################################
  41. props = Properties()
  42. props.setProperty('user', user)
  43. props.setProperty('password', pwd)
  44. jcc = Class.forName(driver).newInstance()
  45. conn = DriverManager.getConnection(url, props)
  46.  
  47. ##################################################################
  48. # Create table with an unprepared statement
  49. #################################################################
  50. stmt = conn.createStatement()
  51. stmt.executeUpdate(SQL_DROP)
  52. stmt.executeUpdate(SQL_CREATE)
  53. ##################################################################
  54. # Insert data with a prepared statement
  55. #################################################################
  56. stmt = conn.prepareStatement(SQL_INSERT)
  57. stmt.setInt(1, 0)
  58. stmt.setLong(2, 1234)
  59. stmt.setDouble(3, 1.2345)
  60. stmt.setDouble(4, 1234.4567)
  61. stmt.setString(5, 'abc' * 1024)
  62. stmt.setString(6, 'xyz')
  63. stmt.setString(7, 'def')
  64. stmt.setTimestamp(8, datetime.datetime.now())
  65. stmt.setTimestamp(9, datetime.datetime.now())
  66. stmt.setBoolean(10, Boolean("True"))
  67.  
  68. for i in range(10):
  69. print "Inserting row " + str(i)
  70. stmt.setInt(1, i)
  71. stmt.executeUpdate()
  72.  
  73.  
  74. ##################################################################
  75. # Perform a delete and roll it back.
  76. # This row will appear in the select
  77. #################################################################
  78. conn.setAutoCommit(Boolean("False"))
  79. stmt = conn.createStatement()
  80. stmt.executeUpdate(SQL_DELETE)
  81. conn.rollback()
  82. conn.setAutoCommit(Boolean("True"))
  83.  
  84. ##################################################################
  85. # Retrieve the data as a resultset
  86. #################################################################
  87. stmt = conn.createStatement()
  88. rs = stmt.executeQuery(SQL_SELECT)
  89. rsmd = rs.getMetaData()
  90. columnCount = rsmd.getColumnCount()
  91.  
  92. rowNum = 0
  93. while (rs.next()) :
  94. rowNum += 1
  95. print "Row " + str(rowNum)
  96. for j in range (1, columnCount) :
  97. print rsmd.getColumnName(j) + ": " + rs.getString(j)
  98.  
  99. rs.close()
  100.  
  101. ##################################################################
  102. # Drop the table
  103. #################################################################
  104. stmt.executeUpdate(SQL_DROP)
  105.  
  106. stmt.close()
  107. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement