DataBase Connectivity
DataBase Connectivity
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.
ODBC program first allocates an SQL environment, then a database connection handle.
Opens database connection using SQLConnect(). Parameters for SQLConnect:
o connection handle,
o the server to which to connect
o the user identifier,
o password
Must also specify types of arguments:
o SQL_NTS denotes previous argument is a null-terminated string.
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);
}
Good programming requires checking results of every function call for errors; we have
omitted most checks for brevity.
char branchname[80];
float balance;
int lenOut1, lenOut2;
HSTMT stmt;
SQLAllocStmt(conn, &stmt);
char * sqlquery = "select branch_name, sum (balance)
from account
group by branch_name";
error = SQLExecDirect(stmt, sqlquery, SQL_NTS);
if (error == SQL_SUCCESS) {
SQLBindCol(stmt, 1, SQL_C_CHAR, branchname , 80, &lenOut1);
SQLBindCol(stmt, 2, SQL_C_FLOAT, &balance, 0 , &lenOut2);
while (SQLFetch(stmt) >= SQL_SUCCESS) {
printf (" %s %g\n", branchname, balance);
}
}
SQLFreeStmt(stmt, SQL_DROP);
Prepared Statement
o SQL statement prepared: compiled at the database
o Can have placeholders: E.g. insert into account values(?,?,?)
o Repeatedly executed with actual values for the placeholders
Metadata features
o finding all the relations in the database and
o finding the names and types of columns of a query result or a relation in the
database.
SQL Call Level Interface (CLI) standard similar to ODBC interface, but with some minor
differences.
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
Model for communicating with the database:
o Open a connection
o Create a “statement” object
o Execute queries using the Statement object to send queries and fetch results
Exception mechanism to handle errors
JDBC code
Update to database
try {
stmt.executeUpdate( "insert into account values
('A-9732', 'Perryridge', 1200)");
} catch (SQLException sqle) {
System.out.println("Could not insert tuple. " + sqle);
}
Execute query and fetch and print results
int a = rs.getInt(“a”);
if (rs.wasNull()) Systems.out.println(“Got null value”);
Prepared statement
Prepared statement allows queries to be compiled and executed multiple times with different
arguments
SQL sessions
An SQL environment contains several components, including a user identifier, and a schema,
which identifies which of several schemas a session is using.
Transactions in JDBC
Application Architectures
Two-tier model
E.g. Java code runs at client site and uses JDBC to communicate with the backend server
Benefits:
o flexible, need not be restricted to predefined queries
Problems:
o Security: passwords available at client site, all database operation possible
o More code shipped to client
o Not appropriate across organizations, or in large
ones like universities