Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
By
Sana mateen
• What is JDBC Driver?
• JDBC drivers implement the defined interfaces in the JDBC API, for
interacting with your database...
Which Driver should be Used?
1. If you are accessing one type of database, such as Oracle, Sybase, or
IBM, the preferred d...
Jdbc(java database connectivity)
• JDBC API is a Java API that can access any kind of tabular data, especially
data stored...
• The JDBC library includes APIs for each of the tasks mentioned below that
are commonly associated with database usage.
•...
Creating JDBC Application:
• There are six steps involved in building a JDBC application:
1. Import the packages:
• This r...
3. Open a connection:
• This requires using the DriverManager.getConnection() method to create
a Connection object, which ...
4. Execute a query:
• This requires using an object of type Statement or PreparedStatement for
building and submitting an ...
5. Extract data from result set:
• This step is required in case you are fetching data from the database. You
can use the ...
6. Clean up the environment:
• You should explicitly close all database resources versus relying on the
JVM's garbage coll...
Jdbc in servlets
Nächste SlideShare
Wird geladen in …5
×

Jdbc in servlets

How to use JDBC API in servlets.Four driver types

Ähnliche Bücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen

Ähnliche Hörbücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen
  • Als Erste(r) kommentieren

  • Gehören Sie zu den Ersten, denen das gefällt!

Jdbc in servlets

  1. 1. By Sana mateen
  2. 2. • 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 −
  3. 3. 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.
  4. 4. 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.
  5. 5. • 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.
  6. 6. 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");
  7. 7. 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);
  8. 8. 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);
  9. 9. 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); }
  10. 10. 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();

×