Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.

Similar presentations


Presentation on theme: "1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a."— Presentation transcript:

1 1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a Database (Java’s Statement class)  Obtaining Query Results (Java’s ResultSet class)   Preview: Introduction to Data Communications

2 2 Lecture 29 Objectives of the Lecture  To have an acquintance of JDBC drivers  To know the basic issues required to connect to a database from a Java application  To be able to write simple queries and receive their results using methods in Java programs

3 3 Lecture 29 JDBC Basics  The JDBC (short for Java Database Connectivity) interface is a pure Java API used to execute SQL statements.  The JDBC provides a set of classes and interfaces that can be used by developers to write database applications.  Basic JDBC interaction, in its simplest form, can be broken down into four steps:  1. Open a connection to the database  2. Execute a SQL statement  3. Process the SQL statement’s results  4. Close the connection to the database.

4 4 Lecture 29 JDBC Drivers  As mentioned in Lecture 28, a Java application can communicate to a DBMS using specialized programs called Drivers.  The JDBC supports two database access models: two-tier and three- tier.  Using a two-tier model, a Java application talks directly to the database. This is accomplished through the use of a JDBC driver, which sends commands directly to the database.  With a two-tier database access model, the JDBC driver and the database communicate through a middle-tier system.  Sun Microsystems has defined four JDBC driver types:  JDBC-ODBC bridge plus ODBC Driver  Native-API Partly-Java Driver  JDBC-Net Pure Java Driver  Native-Protocol Pure Java Driver

5 5 Lecture 29 JDBC Drivers (cont.)

6 6 Lecture 29 Connecting to DBMS from Java  As mentioned earlier writing a Java application to communicate with a database involves: 1.Establishing a connection with a data source 2.Sending queries and update statements to the data source 3.Processing the results 4.Closing the database  Establishing a Connection: Establishing a connection involves loading the driver and then making the connection. Loading the driver or drivers you want to use is very simple and involves just one line of code as follows: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Note: The method forName() generates ClassNotFoundException if JDBC is not installed. This is a checked exception, so we must handle it before the above statement can compile.

7 7 Lecture 29 Establishing a Connection  Making the Connection involves creating a Connection object that links the driver to the DBMS. Connection con = DriverManager.getConnection(url, "myLogin", "myPassword"); or Connection con = DriverManager.getConnection(url); where url = “jdbc:odbc:DataSetName”  Since we are using the JDBC-ODBC Bridge driver, the JDBC URL will start with jdbc:odbc:. The rest of the URL is generally the data source name defined in the configuration of ODBC  The second version assumes you do not need to login to the computer hosting the database file. Connection con = DriverManager.getConnection(url);

8 8 Lecture 29 Creating JDBC Statements and Sending SQL statements to the data source  The next step after establishing a connection is to create a Statement object using the Connection object as follows: Statement statement = con.createStatement();  The Statement object created above has methods which are used to send SQL statement to the DBMS. Which method is used, depends on the SQL statement being sent. For a SELECT statement, the method to use is executeQuery(). Whereas, for statements that create or modify tables, the method to use is executeUpdate(). String query = "select * from table1"; ResultSet result = statement.executeQuery(query);  Notice that executeQuery() method returns an object of ResultSet which contains the result of the query.

9 9 Lecture 29 Creating JDBC Statements and Sending SQL statements to the data source  The executeUpdate() is similar, except that we are not expecting result from the data source. String query2 = "INSERT INTO table1 VALUES (‘a', ‘b', ‘c', 1)"; statement.executeUpdate(query2);

10 10 Lecture 29 Processing Results obtained from data source  The variable result, which is an instance of ResultSet, contains all the rows of the table1 parameter. In order to process each row, we need to go to the row and retrieve its values.  The method next() moves what is called a cursor to the next row and makes that row (called the current row) the one upon which we can operate.  The cursor is initially positioned just above the first row of a ResultSet object, the first call to the method next() moves the cursor to the first row and makes it the current row.  Successive invocations of the method next() move the cursor down one row at a time from top to bottom.

11 11 Lecture 29 Processing Results obtained from data source  The ResultSet object has a get method for each type ( getString, getInt, getDouble, getLong, getFloat, etc) which can be used to retrieve the value of a field depending on its type.  For example, the following loop is use to retrieve and print the bookNumber, author and title fields of a books table. while(result.next()) { System.out.println(result.getString(“ColumnName1")+ "\t"+ result.getString(" ColumnName2")+ “\t"+ result.getString(" ColumnName3")); }  Note: Most of the methods of the ResultSet object (e.g next()) generate SQLException which is a checked exception, so again we must handle this exception before our program can compile.


Download ppt "1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a."

Similar presentations


Ads by Google