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