Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture12: Accessing Databases with JDBC
2 Outlines يقول ابن مسعود: مرحبًا بالشتاء, تتنزل فيه البركة و يطول فيه الليل للقيام و يقصر فيه النهار للصيام
3 Outlines SQL’s statement overview Java DB connectivity (JDBC) Connecting to and Querying a Database More
4 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
5 Some popular relational database management systems (RDBMSs) are 1. Microsoft SQL Server, 2.Oracle, 3.Sybase, 4.IBM DB2, 5.MySQL. Introduction
6 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. Java DB connectivity 'JDBC'
7 SQL’s statement overview
8 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: 1.Our Oracle database, 2.Our Java development environment, and 3.The JDBC drivers to connect the Oracle database and our Java programs. Connecting to Database Oracle DB JDE JDBC
9 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 Connecting to Database
10 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.
11 Setting up JDBC enviro
12 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.*;
13 Loading and Registering the JDBC Driver To load the JDBC driver requires two steps: 1.Loading the class and 2.Registering it with the DriverManager. The DriverManager will automatically load the driver for us. DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
14 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.
15 Getting a Connection Oracle provides two JDBC drivers: 1.An OCI driver and 2.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: : : "
16 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( "scott", "tiger"); } catch (SQLException e) { System.out.println("Caught: " + e); System.exit(1); } } }
17 Next Lecture isa 1.Executing SQL Statements 2.Executing SQL Queries 3.Large Objects—BLOBs and CLOBs 4.Stored Procedures 5.RowSet Interface
18 Thank You … Questions?!