Docsity

Java database connectivity, Lecture notes of Java Programming

Partial preview of the text

Download Java database connectivity and more Lecture notes Java Programming in PDF only on Docsity!

©NIIT

Objectives

In this lesson, you will learn about:

  • (^) Layers in JDBC architecture
  • (^) Types of JDBC drivers
  • (^) Classes and interfaces of JDBC API
  • (^) Steps to create JDBC applications

©NIIT JDBC and JavaBeans

  • (^) Java programs can communicate with databases and manipulate their data using the JDBC API.
  • (^) We can develop platform independent database applications using SQL and Java for any relational database systems.
  • (^) JDBC is a Java Database Connectivity API that lets us access virtually any tabular data source from a Java application.
  • (^) JDBC defines a low-level API designated to support basic SQL functionality independently of any specific SQL implementation. This means the focus is on executing raw SQL statements and retrieving their results.
  • (^) In building a database application, we do not have to think about the implementation of underlying classes at all; - (^) The whole point of JDBC is to hide the specifics of each database and

let you worry about just your application.

©NIIT

Database Connectivity (Contd.)

  • (^) JDBC Architecture:
    • (^) Provides the mechanism to translate Java statements into SQL statements.
    • (^) Can be classified into two layers:
      • (^) JDBC Application layer
      • (^) JDBC Driver layer

©NIIT Other way… Java Programs JDBC API MySQL JDBC Driver JDBC-ODBC Bridge Driver

Local or Remote

MySQL DB

Microsoft ODBC Driver Oracle JDBC Driver

Local or Remote

ORACLE DB

Microsoft Access

Database

©NIIT

Database Connectivity (Contd.)

  • (^) JDBC-ODBC Bridge driver

©NIIT

Database Connectivity (Contd.)

  • (^) Native-API Partly-Java driver

©NIIT

Database Connectivity (Contd.)

  • (^) Native-Protocol Pure-Java driver

©NIIT

Using JDBC API

  • (^) The JDBC API classes and interfaces are available in the java.sql and

the javax.sql packages (JDBC Standard Extension).

  • (^) The commonly used classes and interfaces in the JDBC API are: 1. DriverManager class : Loads the driver for a database. 2. Driver interface : Represents a database driver. All JDBC driver classes must implement the Driver interface. 3. Connection interface : Enables you to establish a connection between a Java application and a database. 4. Statement interface : Enables you to execute SQL statements. 5. ResultSet interface : Represents the information retrieved from a database. 6. SQLException class : Provides information about the exceptions that occur while interacting with databases.

©NIIT

A. Loading Driver

i. Programmatically (two options):

  1. Using the forName() method
  2. Using the registerDriver()method ii. Manually:
  • By setting system property

©NIIT

Loading Driver(Contd.)

  1. Using the forName() method
    • (^) The forName() method is available in the java.lang.Class class.
    • (^) The forName() method loads the JDBC driver and registers the driver with the driver manager.
    • (^) The method call to use the the forName() method is: E.g. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  (^) A driver is a concrete class that implements the java.sql.Driver interface  (^) Drivers for Access, MySQL, and Oracle are listed below: Database | DriverClass Source___________ Access | sun.jdbc.odbc.JdbcOdbcDriver Already in JDK MySQL | com.mysql.jdbc.Driver Download & Install Oracle | oracle.jdbc.driver.OracleDriver Download & Install

©NIIT

B. Connecting to DB

  • (^) The DriverManager class provides the getConnection() method to create a Connection object. This method is static.
  • (^) The getConnection()method method has the following three forms:
    • (^) Connection getConnection (String )
    • (^) Connection getConnection (String , String , String )
    • (^) Connection getConnection (String ,Properties ) E.g. Connection con=DriverManager.getConnection(dbURL); Database | URL Pattern_______________________ Access | jdbc:odbc:testDb MySQL | jdbc:mysql://hostname:port/testDb Oracle | jdbc:oracle:thin:@hostname:port#:oracleDBSID __ E.g. Connection con; con=DriverManager.getConnection("jdbc:mysql://localhost:3306/testDb","root","");

©NIIT

C. Creating & Executing Statements

- Creating and Executing JDBC Statements - (^) The Connection object provides the createStatement() method to create a Statement object. - (^) You can use static SQL statements to send requests to a database to retrieve results. - (^) The Statement interface contains the following methods to send static SQL statements to a database: - (^) ResultSet executeQuery(String str) - (^) int executeUpdate(String str) - (^) boolean execute(String str)  (^) E.g. If a Connection object is like a cable linking your program to a database, then an object of Statement or its subclass can be viewed as a cart that delivers SQL statements for execution by the database and brings the result back to the program.  (^) Once Connection object is created, you can create statements for executing SQL statements E.g. Statement stmt=con.createStatement();

©NIIT Creating & Executing Statements(Contd.) 1)Querying a Table

  • (^) The SELECT statement is executed using the executeQuery () method and returns the output in the form of a ResultSet object.
  • (^) The code snippet to retrieve data from the authors table is: String str = "SELECT * FROM authors"; Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(str ); E.g. (Assume the table has ID(int), Name(Varchar) & Sex(Varchar) attributes) while(rs.next()) { System.out.println(rs.getInt(1)+"\t "+rs.getString(2)+"\t"+rs.getString(3)); } //System.out.println(rs.getString("ID")+"\t "+rs.getString("Name")+"\t "+rs.getString("Sex"));//if we want to use columns names 1 in rs.getInt(1) retrieves the value of the first column assuming it is integer, if not integer then error occurs.

©NIIT 2) Inserting Rows in a Table

  • (^) The executeUpdate () method enables you to add rows in a table.
  • (^) The code snippet to insert a row in the authors table is: String str = "INSERT INTO authors (au_id, au_lname, au_fname, address, city, state, contract) VALUES ('998-72-3568', 'Ringer','Albert','801 826-0752 67 Seventh Av.', 'Salt Lake City','UT','1')"; Statement stmt = con.createStatement(); int count = stmt.executeUpdate(str);