160 likes | 297 Views
JDBC and OCCI. JDBC. Java API to allow java program to interface with database Platform independence Database independence. JDBC applications. Import the JDBC class (java.sql.*) Load the JDBC drivers Connect to the database Interact with the database using JDBC
E N D
JDBC • Java API to allow java program to interface with database • Platform independence • Database independence
JDBC applications • Import the JDBC class (java.sql.*) • Load the JDBC drivers • Connect to the database • Interact with the database using JDBC • Disconnect with the database
Load the JDBC drivers • Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connect to the database • Connection Conn = DriverManager.getConnection(url,username,password); • url is of the form: • Jdbc:oracle:drivertype:@database • Example: "jdbc:oracle:thin:@erg.csci.unt.edu:1521:ERG"
The Connection Object • Statement • prepareStatement • CallableStatement
Using Statement Object • Statement Stmt = Conn.createStatement(); ResultSet RS = Stmt.executeQuery("SELECT * from user_tables");
The ResultSet Class • The resultset class provides access to a table of data generated by executing a query. • Methods: • Next • Close • getString(int columnIndex)
ResultSet Metadata • ResultSetMetData rsetmd = rs.getMetaData() • Method: • getColumnCount • getColumnDisplaySize(int column) • getColumnTypeName(int column)
Close • RS.close(); • Stmt.close(); • Conn.close();
OCCI • Oracle C++ call interface • Steps are similar to JDBC
Create an environment • An OCCI driver manager maps to an OCI environment handle. • env = Environment::createEnvironment (Environment::DEFAULT); • Establish a connection • conn = env->createConnection (user, passwd, db);
create and bind a SQL statement Statement • *stmt = conn->createStatement ("SELECT * FROM user_tables"); • execute the statement • stmt->execute ();
get the result of the statement • ResultSet *rs = stmt->getResultSet ();
The ResultSet Class • The resultset class provides access to a table of data generated by executing a query. • Methods: • Next • Close • getString(int columnIndex)
Terminate the connection • env->terminateConnection (conn); Environment::terminateEnvironment (env);