Se ha denunciado esta presentación.
Se está descargando tu SlideShare. ×
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Cargando en…3
×

Eche un vistazo a continuación

1 de 36 Anuncio

Más Contenido Relacionado

Anuncio

Más de st anns PG College,Mallapur,Hyderabad, India (14)

Más reciente (20)

Anuncio

Prashanthi

  1. 1. WELCOME
  2. 2. JDBC DRIVERS
  3. 3. <ul><li>ODBC </li></ul>2. JDBC 3. JDBCAPI 4. JDBC DRIVERS 5. JDBC STEPS
  4. 4. ODBC <ul><li>O pen D ata B ase C onnectivity , Standard Database Access a method developed by the SQL Access group in 1992. </li></ul><ul><li>The goal of ODBC is to make it possible to access any data from any application. </li></ul><ul><li>ODBC manages this by inserting a middle layer, called a database driver , between an application and the DBMS. </li></ul>Home
  5. 5. JDBC <ul><li>JDBC technology allows you to use the Java to exploit &quot;Write Once, Run Anywhere“ capabilities. </li></ul><ul><li>The JDBC API makes it possible to do 3 things: </li></ul><ul><li>1.Establish a connection </li></ul><ul><li>2.Send SQL statements </li></ul><ul><li>3.Process the results </li></ul><ul><li>The JDBC API is the database-independent connectivity between the Java and a wide range of databases. </li></ul>Home
  6. 6. JDBC API <ul><li>The JDBC API provides universal data access from the Java programming language. </li></ul><ul><li>JDBC technology also provides a common base on which tools & alternate interfaces can be built. </li></ul><ul><li>The JDBC 3.0 API is comprised of two packages: </li></ul><ul><li>java.sql package </li></ul><ul><li>javax.sql package </li></ul>Home
  7. 7. JDBC Architecture <ul><li>Java code calls JDBC library </li></ul><ul><li>JDBC loads a driver </li></ul><ul><li>Driver talks to a particular database </li></ul><ul><li>Can have more than one driver -> more than one database </li></ul><ul><li>Ideal: can change database engines without changing any application code </li></ul>Application JDBC Driver
  8. 8. JDBC Drivers <ul><li>Type I: “Bridge” </li></ul><ul><li>Type II: “Native” </li></ul><ul><li>Type III: “Middleware” </li></ul><ul><li>Type IV: “Pure” </li></ul>Home
  9. 9. JDBC-ODBC Bridge driver <ul><li>The Type 1 driver translates all JDBC calls into ODBC calls and sends them to the ODBC driver. ODBC is a generic API. </li></ul><ul><li>The JDBC-ODBC Bridge driver is recommended only for experimental use or when no other alternative is available. </li></ul><ul><li>The Type 1 driver translates all JDBC calls into ODBC calls and sends them to the ODBC driver. ODBC is a generic API. </li></ul><ul><li>The JDBC-ODBC Bridge driver is recommended only for experimental use or when no other alternative is available. </li></ul>
  10. 10. Type 1: JDBC-ODBC Bridge
  11. 11. Advantage & Disadvantages of type1 driver <ul><li>The JDBC-ODBC Bridge allows access to almost any database, </li></ul><ul><li>since the database’s ODBC drivers are already available. </li></ul><ul><li>Type 1 drivers are not portable. </li></ul><ul><li>A performance is not gud. </li></ul><ul><li>The client system requires the ODBC Installation to use the driver. </li></ul><ul><li>Not good for the Web. </li></ul>
  12. 12. Native-API/partly Java driver <ul><li>The distinctive characteristic of type 2 jdbc drivers are that Type 2 drivers convert JDBC calls into database-specific calls i.e. this driver is specific to a particular database. </li></ul><ul><li>Some distinctive characteristic of type 2 jdbc drivers are shown below. </li></ul><ul><li>Ex: Oracle will have oracle native api. </li></ul>
  13. 13. Type 2: Native api/ Partly Java Driver
  14. 14. Advantage & Disadvantages of type2 driver <ul><li>They typically offer better performance than the JDBC-ODBC Bridge </li></ul><ul><li>The layers of communication (tiers) are less than that of Type1 </li></ul><ul><li>Also it uses Native api which is Databas specific. </li></ul><ul><li>Native API must be installed in the Client System . </li></ul><ul><li>Does not solve portability issue. </li></ul><ul><li>If we change the Database we have to change the native api as it is specific to a database. </li></ul><ul><li>Usually not thread safe. </li></ul>
  15. 15. All Java/Net-protocol driver <ul><li>Calls middleware server, usually on database host. </li></ul><ul><li>Type 3 database requests are passed through the network to the middle-tier server. The middle-tier then translates the request to the database. </li></ul>
  16. 16. Type 3: All Java/ Net-Protocol Driver
  17. 17. Advantage & Disadvantages of type3 driver <ul><li>This driver is server-based. </li></ul><ul><li>It is Portable. </li></ul><ul><li>It is suitable for the web. </li></ul><ul><li>The net protocol can be designed to make the client JDBC driver very small and fast to load. </li></ul><ul><li>It is very flexible . </li></ul><ul><li>They are the most efficient . </li></ul><ul><li>It requires another server application to install and maintain. </li></ul><ul><li>Traversing the recordset may take longer, since the data comes through the backend server. </li></ul>
  18. 18. Native-protocol/all-Java driver <ul><li>100% Pure Java -- the Holy Grail. </li></ul><ul><li>The Type 4 uses java networking libraries to communicate directly with the database server. </li></ul><ul><li>This referred to as the Oracle “thin” driver. </li></ul><ul><li>This driver includes its own implementation of a TCP/IP written entirely in Java, can be downloaded to a browser at runtime. </li></ul><ul><li>This driver requires a TCP/IP listener on the server side. </li></ul>
  19. 19. Type 4: Native-protocol/all-Java driver
  20. 20. Advantage & Disadvantages of type4 driver <ul><li>They are completely written in Java. </li></ul><ul><li>It is most suitable for the web. </li></ul><ul><li>Number of translation layers is very less. </li></ul><ul><li>Performance is typically quite good. </li></ul><ul><li>These drivers can be downloaded dynamically. </li></ul><ul><li>If you need to attach to various types of SQL databases, several JDBC drivers must be loaded onto the client machines. </li></ul>
  21. 21. JDBC Drivers JDBC Type I “ Bridge” Type II “ Native” Type III “ Middleware” Type IV “ Pure” ODBC ODBC Driver CLI (.lib) Middleware Server
  22. 22. JDBC steps <ul><li>Connect to database </li></ul><ul><li>Query database (or insert/update/delete) </li></ul><ul><li>Process results </li></ul><ul><li>Close connection to database </li></ul>Home
  23. 23. JDBC Object Classes <ul><li>Driver Manager </li></ul><ul><ul><li>Loads, chooses drivers </li></ul></ul><ul><li>Driver </li></ul><ul><ul><li>connects to actual database </li></ul></ul><ul><li>Connection </li></ul><ul><ul><li>a series of SQL statements to and from the DB </li></ul></ul><ul><li>Statement </li></ul><ul><ul><li>a single SQL statement </li></ul></ul><ul><li>ResultSet </li></ul><ul><ul><li>the records returned from a Statement </li></ul></ul>
  24. 24. 1. Connect to database <ul><li>Load JDBC driver Class.forName(&quot;com.mysql.jdbc.Driver&quot;).newInstance(); </li></ul><ul><li>Make connection </li></ul><ul><ul><li>Connection conn = DriverManager.getConnection(url); </li></ul></ul><ul><li>URL </li></ul><ul><ul><li>Format: “ jdbc:< subprotocol >:< subname > ” </li></ul></ul><ul><ul><li>jdbc:mysql://128.100.53.33/GROUPNUMBER?user=USER&password=PASSWORD </li></ul></ul>
  25. 25. <ul><li>DriverManager tries all the drivers </li></ul><ul><li>When a driver class is first loaded, it registers itself with the DriverManage </li></ul><ul><li>Connection: </li></ul><ul><li>A Connection represents a session with a specific database. </li></ul><ul><li>Within the context of a Connection, SQL statements are executed and results are returned. </li></ul><ul><li>Can have multiple connections to a database </li></ul>Driver Manager:
  26. 26. 2. Query database <ul><li>Create statement </li></ul><ul><ul><li>Statement stmt = conn.createStatement(); </li></ul></ul><ul><ul><li>stmt object sends SQL commands to database </li></ul></ul><ul><ul><li>Methods </li></ul></ul><ul><ul><ul><li>executeQuery() for SELECT statements </li></ul></ul></ul><ul><ul><ul><li>executeUpdate() for INSERT, UPDATE, DELETE, statements </li></ul></ul></ul><ul><li>Send SQL statements </li></ul><ul><ul><li>stmt.executeQuery(“SELECT …”); </li></ul></ul><ul><ul><li>stmt.executeUpdate(“INSERT …”); </li></ul></ul>
  27. 27. 3. Process results <ul><li>Result of a SELECT statement (rows/columns) returned as a ResultSet object </li></ul><ul><ul><li>ResultSet rs = stmt.executeQuery(&quot;SELECT * FROM users&quot;); </li></ul></ul><ul><li>Step through each row in the result </li></ul><ul><ul><li>rs.next() </li></ul></ul><ul><li>Get column values in a row </li></ul><ul><ul><li>String userid = rs.getString(“userid”); </li></ul></ul><ul><ul><li>int type = rs.getInt(“type”); </li></ul></ul>users table userid firstname lastname password type Bob Bob King cat 0 John John Smith pass 1
  28. 28. Statement Methods <ul><li>ResultSet executeQuery(String) </li></ul><ul><ul><li>Execute a SQL statement that returns a single ResultSet. </li></ul></ul><ul><li>int executeUpdate(String) </li></ul><ul><ul><li>Execute a SQL INSERT, UPDATE or DELETE statement. Returns the number of rows changed. </li></ul></ul><ul><li>boolean execute(String) </li></ul><ul><ul><li>Execute a SQL statement that may return multiple results. </li></ul></ul>
  29. 29. ResultSet <ul><li>A ResultSet provides access to a table of data generated by executing a Statement. </li></ul><ul><li>Only one ResultSet per Statement can be open at once. </li></ul><ul><li>The table rows are retrieved in sequence. </li></ul><ul><li>The 'next' method moves the cursor to the next row. </li></ul><ul><li>- you can’t rewind </li></ul>
  30. 30. ResultSet Methods <ul><li>boolean next() </li></ul><ul><ul><li>activates the next row </li></ul></ul><ul><ul><li>the first call to next() activates the first row </li></ul></ul><ul><ul><li>returns false if there are no more rows </li></ul></ul><ul><li>void close() </li></ul><ul><ul><li>disposes of the ResultSet </li></ul></ul><ul><ul><li>allows you to re-use the Statement that created it </li></ul></ul><ul><ul><li>automatically called by most Statement methods. </li></ul></ul>
  31. 31. Print the users table <ul><li>ResultSet rs = stmt. executeQuery (&quot;SELECT * FROM users&quot;); </li></ul><ul><li>while ( rs.next ()) { </li></ul><ul><li>String userid = rs.getString(1); </li></ul><ul><li>String firstname = rs.getString(“firstname”); </li></ul><ul><li>String lastname = rs.getString(“lastname”); </li></ul><ul><li>String password = rs.getString(4); </li></ul><ul><li>int type = rs.getInt(“type”); </li></ul><ul><li>System.out.println(userid + ” ” + firstname + ” ” + lastname + ” ” + password + ” ” + type); </li></ul><ul><li>} </li></ul>users table userid firstname lastname password type Bob Bob King cat 0 John John Smith pass 1
  32. 32. 4. Close connection to database <ul><li>Close the ResultSet object </li></ul><ul><ul><li>rs.close(); </li></ul></ul><ul><li>Close the Statement object </li></ul><ul><ul><li>stmt.close(); </li></ul></ul><ul><li>Close the connection </li></ul><ul><ul><li>conn.close(); </li></ul></ul>
  33. 33. <ul><li>import java.sql.*; </li></ul><ul><li>public class Tester { </li></ul><ul><li>public static void main(String[] args) { </li></ul><ul><li>try { </li></ul><ul><li>// Load JDBC driver </li></ul><ul><li>Class.forName(&quot;com.mysql.jdbc.Driver&quot;).newInstance(); </li></ul><ul><li>// Make connection </li></ul><ul><li>String url = “jdbc:mysql://128.100.53.33/GRP?user=USER&password=PASS” </li></ul><ul><li>Connection conn = DriverManager.getConnection(url); </li></ul>
  34. 34. // Create statement Statement stmt = conn.createStatement (); // Print the users table ResultSet rs = stmt.executeQuery(&quot;SELECT * FROM users&quot;); while (rs.next()) { ... } // Cleanup rs.close(); stmt.close(); conn.close(); } catch (Exception e) { System.out.println(&quot;exception &quot; + e); } }
  35. 35. ANY QUERIES ?
  36. 36. Thank you

×