11. jdbc


Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

11. jdbc

  1. 1. Java Database Connectivity info@icareercraft.com
  2. 2. JDBC JDBC is an API with set of classes and interfaces present in package java.sql Thick Client JAVA APP Thin Client JAVA APP DRIVER C LIENT DRIVER C LIENT Supporting Software DB SERVER DB SERVER
  3. 3. JDBC JDBC supports 4 types of Drivers Thick Drivers: JDBC-ODBC Bridge Driver. Native-API Partly Java Driver Thin Drivers: Net-Protocol Fully Java Driver Native-Protocol Fully Java Driver
  4. 4. JDBC-ODBC Bridge Driver (TYPE – I) JDBC API access via one or more ODBC drivers. Binary code be loaded on each client machine JDBC-ODBC BR IDGE JAVA APP J D B C O D B C DB SERVER C LIENT
  5. 5. ODBC The Open Database Connectivity (ODBC) interface by Microsoft allows applications to access data in database management systems (DBMS) using SQL as a standard for accessing the data. DRIVER 1 CLIENT A pplication Request connection DRIVER 2 connection DRIVER 3 connection ODBC Driver Manager DB DB DB SERVER
  6. 6. Native-API Partly Java Driver (TYPE – II) Binary code should be loaded on each client machine Converts JDBC calls into calls on the client API for all databases J JAVA App D B C C LIENT OCI DB Library ORACLE SQL Server SERVER
  7. 7. Net-Protocol Fully Java Driver (TYPE – III) The specific protocol used depends on the vendor Type 1 JA VA App Type 3 Dr iver Protocol 1 Middle ware Appli cation DB1 Type 2 DB2 Type 4 CLIENT APPLICATION SERVER SERVER DB3
  8. 8. Native-Protocol Fully Java Driver(TYPE – IV) Direct call from the client to the DBMS server and is a practical solution for Intranet access. protocols are proprietary to database vendors, primary source for this style of driver. JAVA A pp JDBC Driver Data Base PROTOCOL User Sockets & Streams
  9. 9. JDBC The 4 steps to connect to database are: Loading the Driver. Getting the Connection to database. Executing the SQL Query. Closing the Connection.
  10. 10. Loading Driver Syntax for loading the Class file Class.forName(“Driver Class Name”); Example for Jdbc-Odbc bridge driver: Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); Class.forName("oracle.jdbc.driver.OracleDriver");
  11. 11. JDBC drivers by various Vendors Jdbc-Odbc Bridge : sun.jdbc.odbc.JdbcOdbcDriver Oracle : oracle.jdbc.driver.OracleDriver DB2 : com.ibm.db2.jdbc.app.DB2Driver Pointbase : com.pointbase.jdbc.JdbcUniversalDriver Sybase : com.sybase.jdbc2.jdbc.SybDriver SQL-Server : weblogic.jdbc.mssqlserver4.Driver
  12. 12. Data Source Name (DSN) User DSN Available for user who creates and stores in registry System DSN Available for all users and stores in registry
  13. 13. Various ways of getting database connection Connection con = DriverManager.getConnection(“jdbc:odbc:dsnname”); DriverManager.getConnection(“jdbc:odbc:dsnname”, “username”, “password”); DriverManager.getConnection(“jdbc:oracle:oci”, “username”, “password”); DriverManager.getConnection(“jdbc:oracle:thin:@ipaddress:port :serviceId”, “username”, “password”)
  14. 14. Statement Statement It is used to execute SQL statements Prepared Statement Used to prepare statements with place holders(?) to set the values at run time Callable Statement Used to execute functions or procedures available in data base
  15. 15. Statement A Statement object is used for executing a static SQL statement and obtaining the results produced by it. Statement smt = con.createStatement(); R esultSet rs = smt.executeQuery(“Select_Queries”); int n = smt.executeUpdate(“DML_Queries”); boolean b = smt.execute(“Any_Query”);
  16. 16. ResultSet Is an Object which stores data of the select statement result in records and fields form. By default it is Forward Only and Read Only. Result Set Navigation and updating is possible from new API version 1.2 onwards.
  17. 17. ResultSet Navigating from one record to another boolean b = rs.next() Extracting values from ResultSet is possible either by Field Name or Field Index Column Index int n=rs.getInt(1); or int n = rs.get( “empID”); String s=rs.getString(1); or String s=rs.getString( “empName”); Column Name
  18. 18. ResultSetMetaData It is Data about Data of ResultSet like field names, no. of Columns etc. ResultSetMetaData rsmd = rs.getMetaData(); int count = rsmd.getColumnCount(); String fname = rsmd.getColumnName(int index); String dn = rsmd.getColumnTypeName(int index);
  19. 19. PreparedStatement A PreparedStatement object is used when an application plans to reuse a statement multiple times. The application prepares the SQL Statement it plans to use. Once prepared, the application can specify values for parameters (if any) in the prepared SQL statement. PreparedStatement ps = con.prepareStatement(“Query with Place Holders”); Examples: PreparedStatement ps = con.prepareStatement(“select * from emp where empno=?”); Place Holder
  20. 20. PreparedStatement PreparedStatement ps = con.prepareStatement(“insert into emp (empno,ename) values(?,?)”); ps.setInt(1,102); ps.setString(2, “Scott”); Executing the PreparedStatement ResultSet rs=ps.executeQuery(); int n=ps.executeUpdate(); boolean b=ps.execute(); Set values for place holders
  21. 21. Statement vs PreparedStatement A Statement object Doesn’t contain a SQL statement at the time of creation. Compiled and executed every time A PreparedStatement object Is a precompiled Statement Contains a SQL statement at the time of creation. Support for Place holders, (Ex: For data type blob, clob, binaryStream)
  22. 22. Function create or replace function DemoFunction (id number) return varchar2 is temp varchar2(20); begin select name into temp from Student where rollno=id; return temp; end DemoFunction;
  23. 23. Procedure create or replace procedure DemoProcedure(sname varchar2, rno number) is begin insert into Student values(sname,rno); end DemoProcedure;
  24. 24. CallableStatement A CallableStatement is used to call stored procedures that return values. The CallableStatement has methods for retrieving the return values of the stored procedure. CallableStatement cs = con.prepareCall( “{call prod(?,?)}”); = con.prepareCall( "{?=call fun(?)}"); Example: CallableStatement cs = con.prepareCall( “{call emp.insert(?,?)}”);
  25. 25. CallableStatement CallableStatement cs = con.prepareCall(“{?=call empSal(?)}”); cs.registerOutParameter(1, java.sql.Types.INTEGER); cs.setInt(2,102); cs.executeUpdate(); int result = cs.getInt(1);
  26. 26. CallableStatement java.sql.Types java.sql.Types.DATE java.sql.Types.DOUBLE java.sql.Types.TIME java.sql.Types.FLOAT java.sql.Types.VARCHAR java.sql.Types.INTEGER java.sql.Types.TIMESTAMP java.sql.Types.NUMERIC java.sql.Types. CHAR java.sql.Types.BOOLEAN java.sql.Types.BLOB java.sql.Types.CLOB
  27. 27. Example Program Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger"); CallableStatement cs = con.prepareCall("{?=call empSal(?)}"); cs.setInt(2, 7369); cs.registerOutParameter(1,Types.INTEGER); cs.executeUpdate(); int x=cs.getInt(1); System.out.println(" The Value is: "+ x); con.close();