Presentation is loading. Please wait.

Presentation is loading. Please wait.

6-1 JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.

Similar presentations


Presentation on theme: "6-1 JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson."— Presentation transcript:

1 6-1 JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson

2 6-2 JDBC Introduction JDBC is a JAVA package (java.sql.*). The JDBC package contains the base API class. JDBC provides for a common DB interface. JDBC is in the public domain. Many vendors provide JDBC drivers. Focus of JDBC is on executing raw SQL and retrieving the results.

3 6-3 JDBC and SQL What type of SQL is acceptable? Many different SQL syntax's exists. With JDBC, SQL is just passed through. With JDBC, a query can even pass text and JDBC will not pass judgment on the text. JDBC API consists of a series of abstract Java interfaces.

4 6-4 JDBC-ODBC Bridge The bridge translates JDBC operations into ODBC operations. Use the sun.jdbc.odbc package to use the bridge. The Bridge is part of the Java SDK. The bridge does not work well with applets. The bridge is a reference driver so that other manufacturers can use it as a template.

5 6-5 JDBC Models JDBC supports a two-tier and three-tier model. Two Tier –Applets are allowed to connect directly to the database. –Use in a secure environment such as an intranet.

6 6-6 JDBC Models - Cont’d Three Tier –An application server written in Java mediates access to the database. –Java applet is downloaded to the client –This applet then talks to the Java server which talks to the database. –Used more for the Internet.

7 6-7 JDBC vs. ODBC JDBC does not use VOID*. At one point, developers attempted to map ODBC into Java. Java programming style is to use lots of smaller methods. ODBC uses a smaller number of procedures with a rich set of flags.

8 6-8 JDBC Classes JDBC consists of various class definitions in the java.sql package. JAVA JDBC Classes –java.sql.Date –java.sql.DriverManager –java.sql.DriverPropertyInfo –java.sql.Time –java.sql.Timestamp –java.sql.Types

9 6-9 Main JDBC Interfaces java.sql.CallableStatement java.sql.Connection java.sql. DatabaseMetaData java.sql.Driver java.sql.PreparedStatement java.sql.ResultSet java.sql.ResultSetMetaData java.sql.Statement

10 6-10 JDBC Exceptions java.sql.DataTruncation java.sql.SQLException –Always provides string describing error. –An error code. –Chain to next exception. java.sql.SQLWarning

11 6-11 DriverManager Class All database connections start with this class. This class keeps track of all loaded JDBC drivers. This class maps database URLs to a specific driver. Once an applet has a connection to a database, this class is no longer involved.

12 6-12 Database Connection Example Using the JDBC-ODBC bridge, the driver is loaded as follows: Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); Calling Class.forName will register it with the DriverManager. Once driver is loaded, to connect to database: String url = “jdbc.odbc:NwindDSN”; Connection con = DriverManager.getConnection ( url, “peterson”, “db_passwd”);

13 6-13 Database URLs JDBC allows you to use the database DSN. With JDB, you can also specify the target node, port, and connection attributes as part of the database name. Standard format for applets is: – : : –protocolname is always “jdbc” –subprotocol is database connect mechanism, e.g “odbc”

14 6-14 Connection Class This class obtains information about a specific database connection via the instantiated JDBC driver. A connection is a session with a database. This is where SQL statements are executed and results returned. With JDBC, multiple connections can be maintained with a single database. Connections can be specified as read only

15 6-15 Statement Class String createInventory = “CREATE TABLE Inventory “ + “(Inv_ID INTEGER, Item_Name “+ “VARCHAR(50), Price FLOAT, “+ “Cost FLOAT, Sup_ID INTEGER)”; Statement stmt = con.createStatement(); stmt.executeUpdate(createInventory) Stmt.executeUpdate(“INSERT INTO Inventory “+ “VALUES (202, ‘Widget A’, 19.95, “+ “16.50, 111)”);

16 6-16 PreparedStatement Class PreparedStatement updateInventory = con.prepareStatement( “UPDATE Inventory SET Price = Price * ? “ + “WHERE Item_Name LIKE ?”); To use the PreparedStatement, you must supply the values and the method. updateInventory.setFloat(1, 1.05); updateInventory.setString(2, “Wid”); updateInventory.executeUpdate();

17 6-17 CallableStatement Class CallableStatement cs = con.prepareCall( “{call DISPLAY_INVENTORY}”); ResultSet rs=cs.executeQuery(); String newProcedure = “create procedure DISPLAY_INVENTORY as “ + “select * from INVENTORY order by inv_ID”; Statement stmt=con.createStatement(); stmt.executeUpdate(newProcedure);

18 6-18 ResultSet Class This class is used to analyze results of a SQL select. A ResultSet maintains that cursor pointing to the current row of data. By using the various get methods, you can refer to specific columns by index or name.

19 6-19 ResultSetMetaData Class Used to describe a ResultSet object. Types of data that can be obtained are: –Catalog name of a specified column’s table –Number of columns in a ResultSet –Name of a column –Is auto-num on for a column –Can we put null entries in a column –etc

20 6-20 DatabaseMetaData Class Class is used to obtain information about an entire database. Typical types of information include: –Is database in read-only mode –The name of the database –Determine the database schemas –Obtain database URL


Download ppt "6-1 JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson."

Similar presentations


Ads by Google