Odbc JDBC
Odbc JDBC
Embedded SQL
Database management system program
embedded
SQL
files database
storage
Oracle
CMPT 354: Database I -- ODBC and JDBC
SQL Server
4
Embedded SQL
Oracle library
Oracle
CMPT 354: Database I -- ODBC and JDBC
SQL Server
5
ODBC (Open Database Connectivity) works with C, C++, C#, and Visual Basic JDBC (Java Database Connectivity) works with Java
CMPT 354: Database I -- ODBC and JDBC 6
ODBC
Open DataBase Connectivity (ODBC) standard
Standard for application program to communicate with a database server Application program interface (API) to
Open a connection with a database Send queries and updates Get back results
ODBC (Cont.)
Each database system supporting ODBC provides a "driver" library that must be linked with the client program When client program makes an ODBC API call, the code in the library communicates with the server to carry out the requested action and fetch results
Using Drivers
Application
Embedded SQL
Oracle library
Oracle
CMPT 354: Database I -- ODBC and JDBC
SQL Server
9
ODBC (Cont)
ODBC program first allocates an SQL environment, then a database connection handle Opens database connection using SQLConnect() Parameters for SQLConnect
Connection handle The server to which to connect The user identifier Password
10
ODBC Code
int ODBCexample() {
RETCODE error; HENV env; /* environment */ HDBC conn; /* database connection */ SQLAllocEnv(&env); SQLAllocConnect(env, &conn); SQLConnect(conn, "aura.bell-labs.com", SQL_NTS, "avi", SQL_NTS, "avipasswd", SQL_NTS); { . Do actual work } SQLDisconnect(conn); SQLFreeConnect(conn); SQLFreeEnv(env);
}
CMPT 354: Database I -- ODBC and JDBC 11
Good programming requires checking results of every function call for errors
12
13
Metadata features
Finding all the relations in the database and Finding the names and types of columns of a query result or a relation in the database
CMPT 354: Database I -- ODBC and JDBC 14
Atomicity
By default, each SQL statement is treated as a separate transaction that is committed automatically
Can turn off automatic commit on a connection
SQLSetConnectOption(conn, SQL_AUTOCOMMIT, 0)
SQL Call Level Interface (CLI) standard similar to ODBC interface, but with some minor differences
CMPT 354: Database I -- ODBC and JDBC 16
JDBC
JDBC is a Java API for communicating with database systems supporting SQL JDBC supports a variety of features for querying and updating data, and for retrieving query results JDBC also supports metadata retrieval, such as querying about relations present in the database and the names and types of relation attributes
CMPT 354: Database I -- ODBC and JDBC 17
Communication Model
Open a connection Create a statement object Execute queries using the Statement object to send queries and fetch results Exception mechanism to handle errors
18
JDBC Code
public static void JDBCexample(String dbid, String userid, String passwd) { try {
Class.forName ("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@aura.bell-labs.com:2000:bankdb", userid, passwd);
Statement stmt = conn.createStatement(); Do Actual Work . stmt.close(); conn.close(); } catch (SQLException sqle) { System.out.println("SQLException : " + sqle); } }
CMPT 354: Database I -- ODBC and JDBC 19
}
CMPT 354: Database I -- ODBC and JDBC 20
21
Understand how to use ODBC/JDBC in C# to work with SQL Server 2005 Consider the questions in assignment 1 again. If you are allowed to use embedded SQL in C++/Java, how they can be solved?
CMPT 354: Database I -- ODBC and JDBC 22