Lecture 14 Java Database Connectivity-JDBC
Lecture 14 Java Database Connectivity-JDBC
Introduction
What Is JDBC? Working with leaders in the database field, Sun developed a single API for database accessJDBC. As part of this process, they kept three main goals in mind:
1. JDBC should be a SQL-level API. 2. JDBC should capitalize (take advantage of) on the experience of existing database APIs. 3. JDBC should be simple.
Introduction
A SQL-level API means that JDBC allows you to construct SQL statements and embed them inside Java API calls
Developing Database The JDBC API consists of classes and interfaces for
establishing connections with databases, sending SQL statements to databases, processing the results of the SQL statements, and obtaining database metadata.
Developing Database
Four key interfaces are needed to develop any database application using Java:
Driver, Connection, Statement, and ResultSet.
These interfaces define a framework for generic (general) SQL database access. The JDBC API defines these interfaces. The JDBC driver vendors provide implementation for them. Programmers use the interfaces.
Developing Database
A JDBC application loads an appropriate driver using the Driver interface, Connection to the database will be taken place using the Connection interface, Creating and executing SQL statements will be taken place using the Statement interface, and The result will be processed using the ResultSet interface if the statements return results. Note that some statements, such as SQL data definition statements and SQL data modification statements, do not return results.
Developing Database
The JDBC interfaces and classes are the building blocks in the development of Java database programs. A typical Java program takes the steps outlined below to access the database.
Loading drivers
An appropriate driver must be loaded using the statement shown below before connecting to a database. Class.forName("JDBCDriverClass");
Developing Database A driver is a concrete class that implements the java.sql.Driver interface. The drivers for Access, MySQL, and Oracle are listed below:
com.microsoft.jdbc.sqlserver.SQLServerDriver com.microsoft.sqlserver.jdbc.SQLServerDriver
download download
Developing Database
For example, the following statement creates a Connection object for the local MySQL database test:
Connection connection = DriverManager.getConnection ("jdbc:mysql://localhost/test");
The databaseURL for an Oracle database specifies the hostname, the port# where the database listens for incoming connection requests, and the oracleDBSID database name to locate a database. For example, the following statement creates a Connection object for the Oracle database on cse.ef.bdu.edu with username belay and password tiger:
Connection con =
DriverManager.getConnection(jdbc:oracle:thin:@cse.ef.bdu.edu:1521:oradb,belay,tiger);
Developing Database
Executing statements
An SQL DDL or update statement can be executed using executeUpdate(String sql), and an SQL query statement can be executed using executeQuery(String sql). The result of the query is returned in ResultSet. For example, the following code executes the SQL statement create table Temp (col1 char(5), col2 char(5)):
statement.executeUpdate("create table Student (name char(10), id char(5), dept char(20))");
The next code executes the SQL query select name, id from Student where name = Selamawit': // Select the columns from the Student table
ResultSet resultSet = statement.executeQuery("select name, id, dept from Student where name + " = Selamawit'");
Developing Database
The getString(1), getString(2), and getString(3) methods retrieve the column values for name, id, and dept, respectively. Alternatively, you can use getString(name"), getString(id"), and getString(dept") to retrieve the same three column values. The first execution of the next() method sets the current row to the first row in the result set, and subsequent invocations of the next() method set the current row to the second row, third row, and so on, to the last row.
Developing Database The following is a complete example that demonstrates connecting to a database, executing a simple query, and processing the query result with JDBC. The program connects to a local MySQL database and displays the students whose name is Alemitu.
1 import java.sql.*; 2 3 public class SimpleJdbc { public static void main(String[] args) 4 5 throws SQLException, ClassNotFoundException { // Load the JDBC driver 6 7 Class.forName("com.mysql.jdbc.Driver"); 8 System.out.println("Driver loaded"); 9 10 // Establish a connection 11 Connection connection = DriverManager.getConnection 12 ("jdbc:mysql://localhost/test"); 13 System.out.println("Database connected"); 14 // Create a statement 15 16 Statement statement = connection.createStatement(); 17 // Execute a statement 18 19 ResultSet resultSet = statement.executeQuery 20 ("select name, id, dept from Student where name " 21 + " = Alemitu'"); 22 23 // Iterate through the result and print the student names 24 while (resultSet.next()) 25 System.out.println(resultSet.getString(1) + "\t" + 26 resultSet.getString(2) + "\t" + resultSet.getString(3)); 27 28 // Close the connection 29 connection.close(); 30 } 31 }