Download presentation
Presentation is loading. Please wait.
1
JDBC – ODBC DRIVERS
2
JDBC DRIVERS Type of JDBC Drivers
The JDBC API defines the Java interfaces and classes that programmers use to connect to databases and send queries. A JDBC driver implements these interfaces and classes for a particular DBMS vendor. A Java program that uses the JDBC API loads the specified driver for a particular DBMS before it actually connects to a database. The JDBC DriverManager class then sends all JDBC API calls to the loaded driver. Type of JDBC Drivers JDBC – ODBC Bridge Native – API Partly Java Driver JDBC – Net pure Java Driver Native – Protocol Pure Java Driver
3
JDBC – ODBC bridge plus ODBC driver, also called Type 1.
Translates JDBC API calls into Microsoft Open Database Connectivity (ODBC) calls that are then passed to the ODBC driver. The ODBC binary code must be loaded on every client computer that uses this type of driver. Native – API, partly Java driver, also called Type 2. Converts JDBC API calls into DBMS-specific client API calls. Like the bridge driver, this type of driver requires that some binary code be loaded on each client computer. JDBC – Net, pure Java driver, also called Type 3. Sends JDBC API calls to a middle-tier net server that translates the calls into the DBMS-specific network protocol. The translated calls are then sent to a particular DBMS. Native – protocol, pure Java driver, also called Type 4. Converts JDBC API calls directly into the DBMS-specific network protocol without a middle tier. This allows the client applications to connect directly to the database server.
4
JDBC Implementation JDBC is implemented as the java.sql.package. Some of the classes of this package are: CLASSES PACKAGES Drivers java.sql.Driver java.sql.DriverManager java.sql.DriverPropertyInfo Connection java.sql.Connection Statements java.sql.Statement java.sql.PreparedStatement java.sql.CallableStatement ResultSet java.sql.ResultSet Exceptions java.sql.SQLException Date java.sql.Date
5
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
How to Java Application or Applets Connected to the Database Loading the Driver Making the Connection Creating and Executing a JDBC statement Retrieving Data From the Result Sets Loading the Driver If you are using the JDBC – ODBC Bridge driver, use the following statement to load it. Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); 2. Making the Connection To get the driver connected to the database, which is done by creating a Connection object and initializing it with connection obtained from the DriverManager Connection con = DriverManager.getConnection(url, “LoginName”, “pwd”);
6
Statement stmt = con.createStatement();
3. Creating and executing a JDBC statement a. A statement object is used to send the SQL statement to the database. b. You create a statement object and then execute it, supplying the appropriate execute method with the SQL statement you want to send. c. For a SELECT statement, the method used is executeQuery. For statements that create or modify tables, the method used is executeUpdate Statement stmt = con.createStatement(); Example: stmt.executeQuery (SELECT * FROM MARK WHERE SLNO = "+ t1.getText() +"“); Stmt.executeUpdate(UPDATE MARK SET SLNO="+ s1 +",NAME='"+ s2 +"' WHERE SLNO="+ s1 +“);
7
ResultSet rs = stmt.executeQuery (“select * from mark”);
4. Retrieving Data from the Result Sets JDBC uses ResultSet object to return the output. So the previous executeQuery statement can be fully written as: ResultSet rs = stmt.executeQuery (“select * from mark”);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.