180 likes | 315 Views
A dvanced P rogramming. Lecture 12: Accessing Databases with JDBC. Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra. Outlines. يقول ابن مسعود: مرحبًا بالشتاء, تتنزل فيه البركة و يطول فيه الليل للقيام و يقصر فيه النهار للصيام. Outlines.
E N D
Advanced Programming Lecture12: Accessing Databases with JDBC Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra
Outlines يقول ابن مسعود: مرحبًا بالشتاء, تتنزل فيه البركة و يطول فيه الليل للقيام و يقصر فيه النهار للصيام
Outlines • SQL’s statement overview • Java DB connectivity (JDBC) • Connecting to and Querying a Database • More
Introduction • A database is an organized collection of data. • There are many different strategies for organizing data to facilitate easy access and manipulation. • A database management system (DBMS) provides mechanisms for storing, organizing, retrieving and modifying data for many users. • Database management systems allow for the access and storage of data without concern for the internal representation of data.
Introduction • Some popular relational database management systems (RDBMSs) are • Microsoft SQL Server, • Oracle, • Sybase, • IBM DB2, • MySQL. 5
Java DB connectivity 'JDBC' • Java programs communicate with databases and manipulate their data using the JDBC™API. • A JDBC driver enables Java applications to connect to a database in a particular DBMS and allows programmers to manipulate that database using the JDBC API. • JDBC is almost always used with a relational database. • However, it can be used with any table-based data source. 6
Connecting to Database • The hardest thing about using JDBC is usually getting the JDBC drivers to connect to the database in the first place. • The principle difficulty is because we need to get three different things working together: • Our Oracle database, • Our Java development environment, and • The JDBC drivers to connect the Oracle database and our Java programs. Oracle DB JDE JDBC
Connecting to Database • There are numerous options and alternatives for connecting, many of which you can explore in more reading, but for now we'll start with the basics. • These are the steps required to connect to the database: • Setting up the Oracle, Java, and JDBC environment • Importing the JDBC classes we need • Loading and registering the JDBC driver • Getting a Connection object from the driver
Setting up JDBC enviro. • You should set your CLASSPATH environment variable to include the Oracle JDBC driver. • This is provided by Oracle in the [ORACLE_HOME]\jdbc\lib directory. • If you are using Windows NT/2000/XP, you can set CLASSPATH using the System applet in the Control Panel. • If a CLASSPATH variable does not already exist, start by choosing New. Type in CLASSPATH as the variable name and .;\[ORACLE_HOME] \jdbc\lib\classes12.zip as the variable value.
Setting up JDBC enviro. 1 3 5 4 2 3
Importing the JDBC Classes • The Java compiler needs to import the JDBC classes we will be using. • At the top of our Java source file, we need to include the following import statements: • If the CLASSPATH hasn't been set correctly, the second import above will generate a complaint from the compiler, claiming that the package oracle.jdbc.driver does not exist. import java.sql.*; import oracle.jdbc.driver.*;
Loading and Registering the JDBC Driver • To load the JDBC driver requires two steps: • Loading the class and • Registering it with the DriverManager. • The DriverManager will automatically load the driver for us. DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
Getting a Connection • The DriverManager is responsible for connecting us to resources. • When we want to use a resource, such as a database, we construct a Uniform Resource Locater (URL) and use it to request a Connection object from the DriverManager. • The DriverManager will search the registered drivers for one that can accept our URL. 14
Getting a Connection • Oracle provides two JDBC drivers: • An OCI driver and • A pure-Java thin driver; • Each of these accepts several different types of URLs. • We will use the thin driver with a URL of the following format: "jdbc:oracle:thin:@<host>:<port>:<sid>" "jdbc:oracle:thin:@noizmaker:1521:osiris" 15
Sample Connection Program // TestConnection.java – Load JDBC and connect to database import java.sql.*; import oracle.jdbc.driver.*; public class TestConnection { public static void main(String [] vars){ Connection conn; try { DriverManager.registerDriver(new oracle.jdbc.OracleDriver()); // The following needs to be edited with your database specifics: conn = DriverManager.getConnection( "jdbc:oracle:thin:@noizmaker:1521:osiris", "scott", "tiger"); } catch (SQLException e) { System.out.println("Caught: " + e); System.exit(1); } } } 16
Next Lecture isa Executing SQL Statements Executing SQL Queries Large Objects—BLOBs and CLOBs Stored Procedures RowSet Interface
Thank You … Questions?!