* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Class 16 - Turning Wheel
Microsoft Access wikipedia , lookup
Oracle Database wikipedia , lookup
Concurrency control wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Database model wikipedia , lookup
Clusterpoint wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson 6-1 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. 6-2 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. 6-3 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. 6-4 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-5 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. 6-6 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. 6-7 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 6-8 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 6-9 JDBC Exceptions • java.sql.DataTruncation • java.sql.SQLException – Always provides string describing error. – An error code. – Chain to next exception. • java.sql.SQLWarning 6-10 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. 6-11 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”); 6-12 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>:<subprotocol>:<subname> – protocolname is always “jdbc” – subprotocol is database connect mechanism, e.g “odbc” 6-13 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 6-14 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)”); 6-15 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(); 6-16 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); 6-17 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. 6-18 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 6-19 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 6-20