JDBC for CSQL Database

1,738 views

Published on

Connection and execution of DDL and DML statements in CSQL database using JDBC Driver.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,738
On SlideShare
0
From Embeds
0
Number of Embeds
47
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

JDBC for CSQL Database

  1. 1. CSQL - JDBC Jitendra Lenka Developer - Lakshya Solutions Ltd. [email_address] olutions.com
  2. 2. Module Objectives <ul><li>After completing this module you will be able to : </li></ul><ul><li>Understand various interfaces in JDBC. </li></ul><ul><li>Write application to access CSQL database using JDBC driver. </li></ul><ul><li>Understand different SQL statements executed in CSQL databse with parameter and projection value. </li></ul>www.csqldb.com
  3. 3. <ul><li>The topics to be covered in this session are :- </li></ul><ul><li>Introduction to CSQL main memory database. </li></ul><ul><li>Overview of different Interfaces to client provided by CSQL. </li></ul><ul><li>Important classes and functions for writing application in JDBC. </li></ul><ul><li>Connection to the CSQL database and execute DDL and DML statements. </li></ul><ul><li>Handling errors returned by various functions in JDBC. </li></ul>Module Coverage www.csqldb.com
  4. 4. <ul><li>CSQL is a open source main memory high-performance relational database management system developed in sourceforge.net site. </li></ul><ul><li>CSQL comprises suite of products such as CSQL MMDB, CSQL Cache for leading target databases such as Oracle, MySQL and Postgres , and CSQL Replication which provides high availability and load balancing cluster for MMDB. </li></ul><ul><li>CSQL MMDB is 30x faster than any other leading databases with undisrupted performance for real-time applications. </li></ul><ul><li>CSQL is mainly developed to be used as a cache for existing disk based commercial databases. </li></ul>What is CSQL? www.csqldb.com
  5. 5. <ul><li>ODBC C/C++ standard interface for SQL engine. </li></ul><ul><li>JDBC Java standard interface for SQL engine. </li></ul><ul><li>SQLAPI Proprietary interface for SQL engine. </li></ul><ul><li>DBAPI Proprietary interface for Storage engine </li></ul>Overview www.csqldb.com
  6. 6. <ul><li>Java Database Connectivity defines an API that java programs can use to connect to CSQL server. </li></ul><ul><li>The JDBC API defines interfaces and classes for writing database applications in java by making database connections. </li></ul><ul><li>Java application calls the JDBC library. JDBC loads a driver which talks to the CSQL database. </li></ul>What is JDBC ? www.csqldb.com
  7. 7. <ul><li>Proprietary C++ Interface </li></ul><ul><li>Important Package </li></ul><ul><ul><li>java.sql </li></ul></ul><ul><li>Important classes </li></ul><ul><ul><li>java.sql.Date </li></ul></ul><ul><ul><li>java.lang.DriverManager </li></ul></ul><ul><ul><li>java.sql.Time </li></ul></ul><ul><li>Important Interfaces </li></ul><ul><ul><li>java.sql.Connection </li></ul></ul><ul><ul><li>java.sql.PreparedStatement </li></ul></ul><ul><ul><li>java.sql.ResultSet </li></ul></ul><ul><ul><li>java.sql.Statement </li></ul></ul><ul><ul><li>java.sql.SQLException </li></ul></ul><ul><li>Library </li></ul><ul><ul><li>libcsqljdbc.so </li></ul></ul>JDBC www.csqldb.com
  8. 8. Data Type www.csqldb.com SQL Type Java Type TINYINT Byte SMALLINT Short INTEGER Int BIGINT Long REAL Float FLOAT,DOUBLE Double CHAR java.lang.String DATE java.sql.Date TIME java.sql.Time TIMESTAMP java.sql.Timestamp
  9. 9. <ul><li>Before making a connection to database the JDBC driver should be loaded. </li></ul><ul><li>Class forName( <Database Driver> ) : </li></ul><ul><li>Load the driver class by calling this function. </li></ul><ul><li>Driver class name as an argument. </li></ul><ul><li>Once loaded the driver class creates an instances of itself. </li></ul><ul><li>A client can connect to database server through JDBC driver. </li></ul><ul><li>The return type of this method is Class which is a class in java.lang package. </li></ul>Method for Loading Driver www.csqldb.com
  10. 10. <ul><li>Below method will establish a connection to the database. </li></ul><ul><li>getConenction( <url> , <LoginName> , <password> ) : </li></ul><ul><li>This method is belongs to JDBC DriverManager class. </li></ul><ul><li>DriverManager class defines object which can connect java application to a JDBC driver. </li></ul><ul><li>Driver can recognizes the url (jdbc:csql) . </li></ul><ul><li>This method uses the above argument(url,LoginName, password) to connect to database. </li></ul><ul><li>This method returns a new Connection object to the database. </li></ul>DriverManager www.csqldb.com
  11. 11. <ul><li>java.sql.Connection </li></ul><ul><li>The below methods are belongs to the Connection interface. </li></ul><ul><li>Statement createStatement ( ) throws SQLException. </li></ul><ul><li>PreparedStatement prepareStatement (<SqlStatement>) throws SQLException . </li></ul><ul><li>void close ( ) throws SQLException. </li></ul><ul><li>void commit ( ) throws SQLException. </li></ul><ul><li>void rollback ( ) throws SQLException. </li></ul>Connection www.csqldb.com
  12. 12. <ul><li>java.sql.Connection </li></ul><ul><li>In order to use DDL Statements,we require a Statement object. So, the next step is to create the Statement object from the Connection . </li></ul><ul><li>Statement stmt = con. createStatemen t() </li></ul><ul><li>This method is for interacting with the database. </li></ul><ul><li>It generates new query plan for DDL statements. </li></ul><ul><li>By the help of Statement object SQL statements would be used. </li></ul><ul><li>A Statement object is used to send and execute SQL statements to a database. </li></ul><ul><li>Note : we used createStatement for DDL Statements. </li></ul>Connection www.csqldb.com
  13. 13. <ul><li>Example : Connect to the CSQL through JDBC Driver. </li></ul><ul><li>import java.sql.* ; </li></ul><ul><li>public class ConnTest { </li></ul><ul><li>public static void main( String argv[ ] ) { </li></ul><ul><li> Connection con = null ; </li></ul><ul><li> try { </li></ul><ul><li> </li></ul><ul><li> class.forName(“csql.jdbc.JdbcSqlDriver ”) ; </li></ul><ul><li> Connection con = DriverManager . getConnection(“jdbc:csql” , “root” ,”manager”) ; </li></ul><ul><li> if(con==null) System.exit(1) ; </li></ul><ul><li> con.close() ; System.exit(0) ; </li></ul><ul><li> </li></ul><ul><li> } catch (Exception e) { </li></ul><ul><li> System.out.println(“Exception in Test :“ +e) ; </li></ul><ul><li> e.getStackTrace(); </li></ul><ul><li> System.exit(1) ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Connection www.csqldb.com
  14. 14. <ul><li>java.sql.Statement </li></ul><ul><li>The Statement class provide the below method to execute the SQL statements, mainly for DDL Statements. </li></ul><ul><li>execute (<sqlstatement>) </li></ul><ul><ul><li>This method is for execution of SQL statements in the database. </li></ul></ul><ul><ul><li>Returns a result set object using specified Statement object. </li></ul></ul><ul><ul><li>Return type is Boolean value. </li></ul></ul><ul><li>Note : execute method for DDL statements. </li></ul>Execution of DDL Statements www.csqldb.com
  15. 15. <ul><li>Example : CREATE TABLE EMP(eid integer, ename char(20)); </li></ul><ul><li> CREATE INDEX IDX_EMP ON EMP(eid); </li></ul><ul><li>// Driver should be register here followed by Connection. </li></ul><ul><li>Statement cStmt = con . createStatement(); </li></ul><ul><li>cStmt . execute( “CREATE TABLE EMP(eid integer, ename char(20)) ;” ) ; </li></ul><ul><li>cStmt . execute( “CREATE INDEX IDX_EMP ON EMP(eid) ;” ) ; </li></ul><ul><li>cStmt.close(); </li></ul><ul><li>con.commit(); </li></ul><ul><li>con.close() ; </li></ul><ul><li>// close the connection. </li></ul>Create Table and Index www.csqldb.com
  16. 16. <ul><li>java.sql.PreparedStatement </li></ul><ul><li>The PreparedStatement Interface provides the below methods to execute the SQL statements, for DML Statements. </li></ul><ul><li>ResultSet executeQuery () </li></ul><ul><li>execute the select statements in the database . </li></ul><ul><li>int execute Update () </li></ul><ul><li>This method is used for UPDATE,DELETE,INSERT statements. </li></ul><ul><li>Xxx setXxx () </li></ul><ul><li>Used for input value of parameter marker for different data types . </li></ul><ul><li>void close () </li></ul><ul><li>It implicitly closes all PreparedStatement and Statement instances associated with the connection. </li></ul>Methods for DML Statements www.csqldb.com
  17. 17. <ul><li>The below method will execute precompiled SQL query with or without parameters. </li></ul><ul><li>PreparedStatement stmt = con . prepareStatement ( String sql ) </li></ul><ul><li>- This method generates one time query plan. </li></ul><ul><li>- This method is called by connection object. </li></ul><ul><li>- Argument is sql statement. </li></ul><ul><li>- It returns the new PreparedStatement object. </li></ul>Methods for DML Statements www.csqldb.com
  18. 18. <ul><li>In order to bind the input parameters, PreparedStatements provides the below methods to set the IN parameters </li></ul><ul><li>setInt ( Index, Integer ) </li></ul><ul><li>setString ( Index, String ) </li></ul><ul><li>- The binding of an variable in positional based. </li></ul><ul><li>- The first parameter is the index of the column to set for both the methods. </li></ul><ul><li>- The second parameter is the value according to the datatypes. </li></ul>Binding the Parameters www.csqldb.com
  19. 19. <ul><li>Example : INSERT INTO EMP(eid, ename) VALUES( ?, ?) ; </li></ul><ul><li>// Register the driver and get the connection object. </li></ul><ul><li>// create the prepared Statement object. </li></ul><ul><li>PreparedStatement stmt =nul ; </li></ul><ul><li>stmt =con.prepareStatement( “ INSERT INTO EMP(eid,ename) VALUES(?,?) ;”) ; </li></ul><ul><li>for( int i=0; i<5 ;i++ ) </li></ul><ul><li>{ </li></ul><ul><li>stmt . setInt(1, i) ; </li></ul><ul><li>stmt.setString( 2 , String.valueOf( i+100 )) ; </li></ul><ul><li>ret = stmt . executeUpdate(); </li></ul><ul><li>if(ret !=1) break ; count++; </li></ul><ul><li>} </li></ul><ul><li>stmt.close(); </li></ul><ul><li>con . commit() ; </li></ul><ul><li>con . close() ; </li></ul><ul><li> </li></ul>Table - Insert www.csqldb.com
  20. 20. <ul><li>Example : UPDATE EMP SET ename = ? WHERE eid = ? ; </li></ul><ul><li>// you register the driver and get the connection object. </li></ul><ul><li>// create the prepared Statement object. </li></ul><ul><li>PreparedStatement stmt = nul ; </li></ul><ul><li>stmt=con.prepareStatement(“UPDATE EMP SET ename=? Where eid=? ; ”) ; </li></ul><ul><li>for( int i=0; i<5 ;i++ ) </li></ul><ul><li>{ </li></ul><ul><li>stmt.setString( 1 , String.valueOf( i+200 )) ; </li></ul><ul><li>stmt.setInt(2,i) ; </li></ul><ul><li>ret = stmt . executeUpdate(); </li></ul><ul><li>if(ret !=1) break ; count++; </li></ul><ul><li>} </li></ul><ul><li>stmt.close(); </li></ul><ul><li>con . commit() ; </li></ul><ul><li>con . close() ; </li></ul><ul><li> </li></ul>Table - Update www.csqldb.com
  21. 21. <ul><li>The below method is used for executing sql statements and reading the values. </li></ul><ul><li>ResultSet executeQuery ( ) </li></ul><ul><li>- This method is belongs to Statement Interface </li></ul><ul><li>- It executes select statements in the database. </li></ul><ul><li>- This returns a result set object using the specified statement or prepared statement object and SQL SELECT statement . </li></ul><ul><li>Java.sql.ResultSet </li></ul><ul><li> boolean nex t( ) </li></ul><ul><li> - This method is belongs to ResultSet interface </li></ul><ul><ul><li>- The iterator is initialized to a position before the first row. </li></ul></ul><ul><ul><li>- This method will be called once to move it to the first row. </li></ul></ul><ul><ul><li>xxx getXxx ( int column number ) </li></ul></ul><ul><ul><li>- once we have the result set we can retrieve the data by looping through it. </li></ul></ul><ul><ul><li>void close ( ) </li></ul></ul><ul><ul><li>- used to close a result set. </li></ul></ul>Executing Query www.csqldb.com
  22. 22. <ul><li>Example : SELECT * FROM EMP WHERE eid = ? ; </li></ul><ul><li>// Register the driver and get the connection object. </li></ul><ul><li>// Create the statement object. </li></ul><ul><li>PreparedStatement selStmt = null ; int count =0 , ret = 0 ; </li></ul><ul><li>selStmt = con . prepareStatement ( SELECT * FROM EMP WHERE eid = ? ; ”) ; </li></ul><ul><li>ResultSet rs = null ; </li></ul><ul><li>for ( int i = 0 ; i < 5 ; i++ ) </li></ul><ul><li>{ </li></ul><ul><li> selStmt . setInt ( 1 , i ) ; </li></ul><ul><li> ret = selStmt . executeQuery( ) ; </li></ul><ul><li> while ( rs . next( ) ) </li></ul><ul><li> { </li></ul><ul><li>System.out.println ( “ value = rs . getInt( 1 ) + “ “ + rs . getString( 2 ) ) ; </li></ul><ul><li>count ++ ; </li></ul><ul><li> } </li></ul><ul><li> rs . close ( ) ; </li></ul><ul><li>} </li></ul>Table – Select - Where www.csqldb.com
  23. 23. <ul><li>Example : DELETE FROM EMP WHERE eid = ? ; </li></ul><ul><li>// Register the driver and get the connection object. </li></ul><ul><li>// create the statement object. </li></ul><ul><li>PreparedStatement stmt = null ; int count =0; </li></ul><ul><li>stmt = con . prepareStatement ( “ DELETE FROM EMP WHERE eid = ? ; “ ) ; </li></ul><ul><li>for ( int i =0 ; i < 5 ; i++ ) </li></ul><ul><li>{ </li></ul><ul><li> stmt . setInt ( 1 , i) ; </li></ul><ul><li> ret = stmt . executeUpdate( ) ; </li></ul><ul><li> if( ret !=1 ) break ; count ++ ; </li></ul><ul><li>} </li></ul><ul><li>stmt . Close ( ) ; </li></ul><ul><li>con . Commit ( ) ; </li></ul><ul><li>con . Close ( ) ; </li></ul>Table – Delete www.csqldb.com
  24. 24. <ul><li>Example : DROP TABLE EMP ; </li></ul><ul><li>import java . Sql . * ; </li></ul><ul><li>public class jdbcexample { </li></ul><ul><li>public static void main ( String [ ] args ) { </li></ul><ul><li> try { </li></ul><ul><li> // get the connection object ‘ con ‘ </li></ul><ul><li> Statement cStmt = con . createStatement ( ) ; </li></ul><ul><li>cStmt . execute ( “ DROP TABLE EMP ; “ ) ; </li></ul><ul><li>cStmt . Close ( ) ; </li></ul><ul><li>con . Close ( ) ; </li></ul><ul><li>} catch ( Exception e ) { </li></ul><ul><li> // print the error value here </li></ul><ul><li> } </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Table – Drop www.csqldb.com
  25. 25. <ul><li>Example : set the auto commit false and commit the transaction. </li></ul><ul><li>import java.sql.* ; </li></ul><ul><li>public class ConnTest { </li></ul><ul><li>public static void main( String argv[ ] ) { </li></ul><ul><li> Connection con = null ; </li></ul><ul><li> try { </li></ul><ul><li> class.forName(“csql.jdbc.JdbcSqlDriver ”) ; </li></ul><ul><li> Connection con = DriverManager . getConnection(“jdbc:csql” , “root” ,”manager”) ; </li></ul><ul><li> </li></ul><ul><li> con . setAutoCommit(false) ; </li></ul><ul><li> // perform DML operations here </li></ul><ul><li> con.commit() ; </li></ul><ul><li>} </li></ul><ul><li>// close the connection </li></ul>Transaction www.csqldb.com
  26. 26. <ul><li>Referenced Manuals: </li></ul><ul><li>User Manual: Describes Concept, components and basic usages and useful for administrators. </li></ul><ul><li>Programmer Guide: It covers JDBC, ODBC and proprietary SQL interface and useful for application developers. </li></ul><ul><li>Cache Guide: Describes caching functionality of CSQL and configuration settings required to set up caching for MySQL, Postgres and Oracle DBMS. </li></ul><ul><li>DOWNLOAD : http://www.csqldb.com/pro_documentation.html </li></ul>CSQL Documents www.csqldb.com
  27. 27. <ul><li>For detailed information, please visit: </li></ul><ul><li>Enterprise: www.csqldb.com </li></ul><ul><li>Open Source: http://sourceforge.net/products/csql </li></ul>CSQL Web Site www.csqldb.com

×