lec12.365

lec12.365 - . . Fall 2007 . CPE/CSC 365: Introduction to...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
. . Fall 2007 CPE/CSC 365: Introduction to Database Systems Alexander Dekhtyar . . Database Connectivity: JDBC Database Connecitivity Basics Application-level database connectivity: Host language (Java, C/C++, Perl, PHP, etc) Target DBMS (Oracle, MS SQL server, IBM DB2, MySQL, etc) Client — Server environment Client: application program Server: DBMS General structure: Load database driver/database support functionality form an SQL statement connect to the DBMS pass SQL statement to the DBMS recieve result close connection JDBC JDBC originally, an abbreviation for Java Database Connectivity is the database connectivity package for Java. JDBC driver On CSL, JDBC driver is located in the /app/oracle/product/10.2.0/client 1/jdbc/lib 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
directory (i.e., it is part of the Oracle software package). To successfully compile Java programs using JDBC, this directory needs to be added to your CLASSPATH environment variable. in bash the command is: export CLASSPATH=/app/oracle/product/10.2.0/client 1/jdbc/lib/ \ *.jar in tcsh/csh the command is: setenv CLASSPATH /app/oracle/product/10.2.0/client 1/jdbc/lib/ \ *.jar JDBC Package The JDBC package name is java.sql . When writing Java applications, which include JDBC connectivity, add to your import section import java.sql.* Loading the database driver First task in any JDBC application is loading the JDBC driver for the right DBMS. This is achieved using Class.forName(String Name) method. The argument passed to the Class.forName method is the name of the JDBC driver for a speci±c DBMS server. DBMS Driver name Oracle oracle.jdbc.driver.OracleDriver MySQL com.mysql.jdbc.driver Microsoft SQL Server com.microsoft.jdbc.sqlserver.SQLServerDriver IBM DB2 COM.ibm.db2.jdbc.app.DB2Driver Class.forName() invokation must be inside Java’s try catch block. Example. The following code loads Oracle’s JDBC driver or, if unsuccessful, reports an error. try{ Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException ex) { System.out.println("Driver not found"); }; Establishing a Connection JDBC package contains a Connection class representing client-server connec- tions between the client Java applications and the DBMS servers. An instance of the Connection class will be created via the following driver manager call: Connection conn = DriverManager.getConnection(url, userId, password); 2
Background image of page 2
Here, url is a connection URL specifying location and connection port for the database. See below for syntax. userId is the DBMS user login account. password is the password for the DBMS account of the user userId . Connection URL Connection url has the following syntax: <driver>:<dbms>:<connection-type>@<host>:<port>:<server-name> For CSL machines, use the following line value for the connection url: jdbc:oracle:thin:@hercules.csc.calpoly.edu:1522:ora10g Example. The following code establishes the connection to our Oracle server for one of the student accounts: Connection conn = null;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

lec12.365 - . . Fall 2007 . CPE/CSC 365: Introduction to...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online