Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##################################################################
- # Simple Jython program to demonstrate the Splice Machine
- # database and exercise common operations.
- #################################################################
- from java.sql import DriverManager
- from java.lang import Class
- from java.util import Properties
- from java.math import BigDecimal
- from java.lang import Boolean
- import datetime
- url = 'jdbc:splice://localhost:1527/splicedb'
- driver = 'com.splicemachine.db.jdbc.ClientDriver'
- user = 'splice';
- pwd = 'admin';
- # Sql statement constants
- SQL_DROP = "DROP TABLE IF EXISTS SPLICE_DEMO"
- # Demo table with most common datatypes
- SQL_CREATE = """
- CREATE TABLE SPLICE_DEMO (
- C_INTEGER INTEGER,
- C_BIGINT BIGINT,
- C_NUMERIC NUMERIC (10,4),
- C_DOUBLE DOUBLE,
- C_LONG_VARCHAR LONG VARCHAR,
- C_VARCHAR VARCHAR(100),
- C_CHAR CHAR(10),
- C_DATE DATE,
- C_TIMESTAMP TIMESTAMP,
- C_BOOLEAN BOOLEAN)"""
- SQL_INSERT = """
- INSERT INTO SPLICE_DEMO
- (C_INTEGER, C_BIGINT, C_NUMERIC, C_COUBLE, C_LONG_VARCHAR, C_VARCHAR, C_CHAR, C_DATE, C_TIMESTAMP, C_BOOLEAN)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"""
- SQL_DELETE = "DELETE FROM SPLICE_DEMO WHERE C_INTEGER = 1"
- SQL_SELECT = "SELECT * FROM SPLICE_DEMO"
- ##################################################################
- # Open a jdbc connection
- ##################################################################
- props = Properties()
- props.setProperty('user', user)
- props.setProperty('password', pwd)
- jcc = Class.forName(driver).newInstance()
- conn = DriverManager.getConnection(url, props)
- ##################################################################
- # Create table with an unprepared statement
- #################################################################
- stmt = conn.createStatement()
- stmt.executeUpdate(SQL_DROP)
- stmt.executeUpdate(SQL_CREATE)
- ##################################################################
- # Insert data with a prepared statement
- #################################################################
- stmt = conn.prepareStatement(SQL_INSERT)
- stmt.setInt(1, 0)
- stmt.setLong(2, 1234)
- stmt.setDouble(3, 1.2345)
- stmt.setDouble(4, 1234.4567)
- stmt.setString(5, 'abc' * 1024)
- stmt.setString(6, 'xyz')
- stmt.setString(7, 'def')
- stmt.setTimestamp(8, datetime.datetime.now())
- stmt.setTimestamp(9, datetime.datetime.now())
- stmt.setBoolean(10, Boolean("True"))
- for i in range(10):
- print "Inserting row " + str(i)
- stmt.setInt(1, i)
- stmt.executeUpdate()
- ##################################################################
- # Perform a delete and roll it back.
- # This row will appear in the select
- #################################################################
- conn.setAutoCommit(Boolean("False"))
- stmt = conn.createStatement()
- stmt.executeUpdate(SQL_DELETE)
- conn.rollback()
- conn.setAutoCommit(Boolean("True"))
- ##################################################################
- # Retrieve the data as a resultset
- #################################################################
- stmt = conn.createStatement()
- rs = stmt.executeQuery(SQL_SELECT)
- rsmd = rs.getMetaData()
- columnCount = rsmd.getColumnCount()
- rowNum = 0
- while (rs.next()) :
- rowNum += 1
- print "Row " + str(rowNum)
- for j in range (1, columnCount) :
- print rsmd.getColumnName(j) + ": " + rs.getString(j)
- rs.close()
- ##################################################################
- # Drop the table
- #################################################################
- stmt.executeUpdate(SQL_DROP)
- stmt.close()
- conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement