Jdbc in servlets

11
By Sana mateen

Transcript of Jdbc in servlets

Page 1: Jdbc in servlets

BySana mateen

Page 2: Jdbc in servlets

• What is JDBC Driver?• JDBC drivers implement the defined interfaces in the JDBC API, for

interacting with your database server.• JDBC Drivers Types• JDBC driver implementations vary because of the wide variety of

operating systems and hardware platforms in which Java operates. Sun has divided the implementation types into four categories, Types 1, 2, 3, and 4, which is explained below −

Page 3: Jdbc in servlets
Page 4: Jdbc in servlets

Which Driver should be Used?

1. If you are accessing one type of database, such as Oracle, Sybase, or

IBM, the preferred driver type is 4.

2. If your Java application is accessing multiple types of databases at

the same time, type 3 is the preferred driver.

3. Type 2 drivers are useful in situations, where a type 3 or type 4 driver

is not available yet for your database.

4. The type 1 driver is not considered a deployment-level driver, and is

typically used for development and testing purposes only.

Page 5: Jdbc in servlets

Jdbc(java database connectivity)• JDBC API is a Java API that can access any kind of tabular data, especially

data stored in a Relational Database. JDBC works with Java on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

Page 6: Jdbc in servlets

• The JDBC library includes APIs for each of the tasks mentioned below that are commonly associated with database usage.

• Making a connection to a database.• Creating SQL or MySQL statements.• Executing SQL or MySQL queries in the database.• Viewing & Modifying the resulting records.• JDBC - Environment Setup:• Make sure you have done following setup:• Core JAVA Installation• SQL or MySQL Database Installation• Apart from the above you need to setup a database which you would use

for your project.• Assuming this is EMP and you have created on table Employees within

the same database.

Page 7: Jdbc in servlets

Creating JDBC Application:

• There are six steps involved in building a JDBC application:1. Import the packages:• This requires that you include the packages containing the JDBC classes

needed for database programming. • Most often, using import java.sql.* will suffice as follows:• //STEP 1. Import required packages• import java.sql.*;2. Register the JDBC driver:• This requires that you initialize a driver so you can open a

communications channel with the database.• //STEP 2: Register JDBC driver• Class.forName("com.mysql.jdbc.Driver");

Page 8: Jdbc in servlets

3. Open a connection:

• This requires using the DriverManager.getConnection() method to create a Connection object, which represents a physical connection with the database as follows:

• //STEP 3: Open a connection • // Database credentials • static final String USER = "username"; • static final String PASS = "password"; • System.out.println("Connecting to database..."); • conn = DriverManager.getConnection(DB_URL,USER,PASS);

Page 9: Jdbc in servlets

4. Execute a query:• This requires using an object of type Statement or PreparedStatement for

building and submitting an SQL statement to the database as follows:• //STEP 4: Execute a query • System.out.println("Creating statement..."); • stmt = conn.createStatement(); • String sql; • sql = "SELECT id, first, last, age FROM Employees"; • ResultSet rs = stmt.executeQuery(sql);• If there is an SQL UPDATE,INSERT or DELETE statement required, then

following code snippet would be required:• //STEP 4: Execute a query • System.out.println("Creating statement..."); • stmt = conn.createStatement(); • String sql = "DELETE FROM Employees"; • ResultSet rs = stmt.executeUpdate(sql);

Page 10: Jdbc in servlets

5. Extract data from result set:• This step is required in case you are fetching data from the database. You

can use the appropriate ResultSet.getXXX() method to retrieve the data from the result set as follows:

• //STEP 5: Extract data from result set • while(rs.next()){ • //Retrieve by column name • int id = rs.getInt("id"); • int age = rs.getInt("age"); • String first = rs.getString("first"); • String last = rs.getString("last"); • System.out.print("ID: " + id); • System.out.print(", Age: " + age); • System.out.print(", First: " + first);• System.out.println(", Last: " + last); }

Page 11: Jdbc in servlets

6. Clean up the environment:

• You should explicitly close all database resources versus relying on the JVM's garbage collection as follows:

• //STEP 6: Clean-up environment • rs.close(); • stmt.close(); • conn.close();