Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.

Similar presentations


Presentation on theme: "Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase."— Presentation transcript:

1 Java and Databases

2 JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase

3 JDBC Driver Types Type 1 – JDBC-ODBC Driver Type 2 –Java/Native Code Driver Type 3 –JDBC Driver Type 4 –JDBC Driver

4 Type 1 Drivers Translate JDBC into ODBC and use Windows ODBC built in drivers ODBC must be set up on every client – driver must be physically on each machine for both java applications and applets –for server side servlets ODBC must be set up on web server driver sun.jdbc.odbc.JdbcOdbc provided by JavaSoft with JDK

5 Type 1 Driver (cont.) ClientJDBC- ODBC ODBCDB Vendor Library

6 Type 2 java/Native code Drivers Uses java classes to generate platform specific code- only understood by a specific DBMS. Disadvantage –The loss of some portability of code

7 Type 2 Drivers (cont.) Client JDBC Vendor Library DB

8 Type 3 Drivers Also referred as java protocol Convert SQL queries into JDBC formatted statement.

9 Type 3 Drivers (cont.) ClientJDBC Vendor Middleware DB Tier 1Tier 2 Tier 3

10 Type 4 Drivers Database protocol. compiles into the application, applet or servlet; doesn’t require anything to be installed on client machine, except JVM Fastest way to communicate SQL queries to the DB.

11 Type 4 Drivers (cont.) Client JDBCDB

12 JDBC Process Loading JDBC Driver Connecting to the DBMS Creating and executing statement Processing data returned by the DBMS Terminating the connection with DBMS

13 import java.sql.*; public class Emp { public static void main(String[] args) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:emp", "",""); Statement s = con.createStatement(); s.execute("create table employee ( emp_id number,emp_name varchar(20),emp_address varchar(30) )"); // create a table s.execute("insert into employee values(001,'ARman','Delhi')"); // insert first row into the table s.execute("insert into employee values(002,'Robert','Canada')"); // insert second row into the table s.execute("insert into employee values(003,'Ahuja','Karnal')"); // insert third row into the table s.execute("select * from employee"); // select the data from the table ResultSet rs = s.getResultSet(); // get the ResultSet that will generate from our query if (rs != null) // if rs == null, then there is no record in ResultSet to show

14 while ( rs.next() ) // By this line we will step through our data row-by-row { System.out.println("________________________________________" ); System.out.println("Id of the employee: " + rs.getString(1) ); System.out.println("Name of employee: " + rs.getString(2) ); System.out.println("Address of employee: " + rs.getString(3) ); System.out.println("________________________________________" ); } s.close(); // close the Statement to let the database know we're done with it con.close(); // close the Connection to let the database know we're done with it } catch (Exception err) { System.out.println("ERROR: " + err); }

15 Once a connection is obtained we can interact with the database. The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enable you to send SQL or PL/SQL commands and receive data from your database. They also define methods that help bridge data type differences between Java and SQL data types used in a database. InterfacesRecommended Use StatementUse for general-purpose access to your database. Useful when you are using static SQL statements at runtime. The Statement interface cannot accept parameters. PreparedStatementUse when you plan to use the SQL statements many times. The PreparedStatement interface accepts input parameters at runtime. CallableStatementUse when you want to access database stored procedures. The CallableStatement interface can also accept runtime input parameters.


Download ppt "Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase."

Similar presentations


Ads by Google