Docsity

Java Database Connectivity in Application Model | CS 6030, Study notes of Computer Science

Partial preview of the text

Download Java Database Connectivity in Application Model | CS 6030 and more Study notes Computer Science in PDF only on Docsity!

Java DataBaseConnectivity (JDBC)

J2EE application model „ J2EE is a multitiered distributed application model^

^ client machines^ ^ the J2EE server machine^ ^ the database or legacy machines at the back end

The JDBC-ODBC Bridge „^ ODBC (Open Database Connectivity) is aMicrosoft standard from the mid 1990’s. „^ It is an API that allows C/C++ programs toexecute SQL inside databases „^ ODBC is supported by many products.

„^ The JDBC-ODBC bridge allows Java codeto use the C/C++ interface of ODBC^

it means that JDBC can access many differentdatabase products

The JDBC-ODBC Bridge (Contd.) „^ The layers of translation (Java --> C -->SQL) can slow down execution.

JDBC Pseudo Code „ All JDBC programs do the following: „ Step 1) load the JDBC driver „ Step 2) Specify the name and location of the database being used „ Step 3) Connect to the database with a

Connection

object

„^ Step 4) Execute a SQL query using a

Statement

object

„^ Step 5) Get the results in a

ResultSet

object

„^ Step 6) Finish by closing the

ResultSet

,^ Statement

and^ Connection

objects

„^ Set up a database server (Oracle , MySQL, pointbase) „^ Get a JDBC driver^

set CLASSPATH for driver lib^ „^

Set classpath in windows, control panel->system->advanced->environment variable „ Set classpath in Solaris, set CLASSPATH to driver jar file „^ Import the library^

import java.sql.*; „^ Specify the URL to database server^

String

url = "jdbc:pointbase://127.0.0.1/test“ „^ Load the JDBC driver^

Class.forName("

com.pointbase.jdbc.jdbcUniversalDriver

");

„^ Connect to database server^

Connection con = DriverManager.getConnection(url, “dbUser", “dbPass"); „^ Create SQL Statement^

stmt = con.createStatement(); „^ Execute SQL^

stmt.executeUpdate("insert into COFFEES " + "values('Colombian', 00101, 7.99, 0,0)"); ResultSet rs = stmt.executeQuery(query); JDBC API in J2SE

Statement stmt = con.createStatement();String sql= "select * from Traps";ResultSet rs = stmt.executeQuery(sql);String name;double val;java.sql.Date date;while (rs.next()){

name = rs.getString("TrapName");val = rs.getDouble("TrapValue");date = rs.getDate("TrapDate");System.out.println("name = " + name + " Value = " + val + " Date = " + date); } stmt.close();con.close();} catch(ClassNotFoundException ex1){

System.out.println(ex1); } catch(SQLException ex2){

System.out.println(ex2); } } JDBC Example (Contd.) }

JDBC Diagram DriverManager

Connection

Statement

ResultSet

creates

creates

creates Driver SQL SQL

data data

make linkto driver

Specify the URL to database server „^ The name and location of the database isgiven as a URL^

the details of the URL vary depending on thetype of database that is being used

Database URLjdbc:pointbase: //host.domain.com: 9092

/data/file

The commsprotocol

The machineholding thedatabase.

The path tothe databaseon the machine

e.g.^

jdbc:pointbase://localhost/myDB

Databaseport

ResultSet Object „^ Stores the results of a SQL query. „^ A

ResultSet

object is similar to a ‘table’

of answers, which can be examined bymoving a ‘pointer’ (cursor).

„^ Cursor operations:^

first()

,^ last()

,^ next()

,^ previous()

, etc.

„^ Typical code:

while( rs.next() ) {// process the row;}

JohnMarkPaulPeter

cursor

Accessing a ResultSet

„^ The ‘tricky’ aspect is that the values areSQL data, and so must be converted toJava types/objects. „^ There are many methods foraccessing/converting the data, e.g.^

getString(), getDate(), getInt(),getFloat(), getObject() Accessing a ResultSet (Contd.)

Meta Data^ „

Meta data is the information

about

the

database:^

^ e.g. the number of columns, the types ofthe columns^ ^ meta data is the

schema

information

ID^

Name

Course

Mark

James Bond

Shooting

Aj. Andrew

Kung Fu

meta data