Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_Oracle10g_JDBC 1 Application Development (JDBC)

Similar presentations


Presentation on theme: "CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_Oracle10g_JDBC 1 Application Development (JDBC)"— Presentation transcript:

1 CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_Oracle10g_JDBC 1 Application Development (JDBC)

2 CSC411_L12_Oracle10g_JDBCDr. Nan Wang 22 Contents Overview of JDBC technology JDBC drivers Seven basic steps in using JDBC

3 CSC411_L12_Oracle10g_JDBCDr. Nan Wang 33 JDBC Introduction JDBC provides a standard library for accessing relational databases –API standardizes Way to establish connection to database Approach to initiating queries Method to create stored (parameterized) queries The data structure of query result (table) –Determining the number of columns –Looking up metadata, etc. –JDBC classes are in the java.sql package Note: JDBC is not officially an acronym; unofficially, “Java DataBase Connectivity” is commonly used

4 CSC411_L12_Oracle10g_JDBCDr. Nan Wang 44 On-line Resources Sun’s JDBC Site –http://java.sun.com/products/jdbc/ JDBC Tutorial –http://java.sun.com/docs/books/tutorial/jdbc/ List of Available JDBC Drivers –http://industry.java.sun.com/products/jdbc/drivers/ API for java.sql –http://java.sun.com/j2se/1.4/docs/api/java/sql/ package-summary.html SQLJ & JDBC Basic Samples –http://technet.oracle.com/tech/java/sqlj_jdbc/index2.htm?Code&files/basi c/basic.htm

5 CSC411_L12_Oracle10g_JDBCDr. Nan Wang 55 JDBC Drivers JDBC consists of two parts: –JDBC API, a purely Java-based API –JDBC Driver Manager,which communicates with vendor-specific drivers that perform the real communication with the database. Point: translation to vendor format is performed on the client –No changes needed to server –Driver (translator) needed on client

6 CSC411_L12_Oracle10g_JDBCDr. Nan Wang 6 Tutorial http://docs.oracle.com/javase/tutorial/jdbc/basi cs/gettingstarted.html#step11http://docs.oracle.com/javase/tutorial/jdbc/basi cs/gettingstarted.html#step11 Install the latest version of the Java SE SDK on your computer Install your database management system (DBMS) if needed Install a JDBC driver from the vendor of your database Install Apache Download the sample code Modify the build.xml file Modify the tutorial properties file Compile and package the samples 6

7 CSC411_L12_Oracle10g_JDBCDr. Nan Wang 77 Seven Basic Steps in Using JDBC 1.Load the driver 2.Define the Connection URL 3.Establish the Connection 4.Create a Statement object 5.Execute a query 6.Process the results 7.Close the connection

8 CSC411_L12_Oracle10g_JDBCDr. Nan Wang 88 JDBC: Details of Process 1.Load the driver try { Class.forName("connect.microsoft.MicrosoftDriver"); Class.forName("oracle.jdbc.driver.OracleDriver"); Class.forName(“com.mysql.jdbc.Driver”); } catch { ClassNotFoundException cnfe) { System.out.println("Error loading driver: " cnfe); }

9 CSC411_L12_Oracle10g_JDBCDr. Nan Wang 99 JDBC: Details of Process 2.Define the Connection URL String host = "dbhost.yourcompany.com"; String dbName = "someName"; int port = 1234; String oracleURL = "jdbc:oracle:thin:@" + host + ":" + port + ":" + dbName; String sybaseURL = "jdbc:sybase:Tds:" + host + ":" + port + ":" + "?SERVICENAME=" + dbName; String mysqlURL = "jdbc:mysql:" + host + ":" + port + ":" + "?SERVICENAME=" + dbName;

10 CSC411_L12_Oracle10g_JDBCDr. Nan Wang 10 JDBC: Details of Process (Continued) 3.Establish the Connection String username = "jay_debesee"; String password = "secret"; Connection connection = DriverManager.getConnection(oracleURL, username, password); Optionally, look up information about the database DatabaseMetaData dbMetaData = connection.getMetaData(); String productName = dbMetaData.getDatabaseProductName(); System.out.println("Database: " + productName); String productVersion = dbMetaData.getDatabaseProductVersion(); System.out.println("Version: " + productVersion);

11 CSC411_L12_Oracle10g_JDBCDr. Nan Wang 11 JDBC: Details of Process (Continued) 4.Create a Statement A statement object is to send queries and commands to DB. Statement statement = connection.createStatement(); 5.Execute a Query String query = "SELECT col1, col2, col3 FROM sometable"; ResultSet resultSet = statement.executeQuery(query); –To modify the database, use executeUpdate, supplying a string that uses UPDATE, INSERT, or DELETE –Use setQueryTimeout to specify a maximum delay to wait for results

12 CSC411_L12_Oracle10g_JDBCDr. Nan Wang 12 JDBC: Details of Process (Continued) 6.Process the Result while(resultSet.next()) { System.out.println(resultSet.getString(1) + " " + resultSet.getString(2) + " " + resultSet.getString(3)); } –First column has index 1, not 0 –ResultSet provides various getXxx methods that take a column index or column name and returns the data –You can also access result meta data (column names, etc.) 7.Close the Connection connection.close(); Since opening a connection is expensive, postpone this step if additional database operations are expected

13 CSC411_L12_Oracle10g_JDBCDr. Nan Wang 13 Example Code

14 CSC411_L12_Oracle10g_JDBCDr. Nan Wang 14 Summary You use the same Java syntax with all databases –Translation to native format is done on the client via a JDBC driver Steps in using JDBC 1.Load the driver 2.Define the Connection URL 3.Establish the Connection 4.Create a Statement object 5.Execute a query 6.Process the results 7.Close the connection Examples

15 CSC411_L12_Oracle10g_JDBCDr. Nan Wang 15 PHP and MySQL http://www.w3schools.com/php/php_mysql_conne ct.asphttp://www.w3schools.com/php/php_mysql_conne ct.asp Python and MySQL http://www.tutorialspoint.com/python/python_data base_access.htmhttp://www.tutorialspoint.com/python/python_data base_access.htm 15

16 CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_Oracle10g_JDBC 16 Questions?


Download ppt "CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_Oracle10g_JDBC 1 Application Development (JDBC)"

Similar presentations


Ads by Google