Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Jdbc 1


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Jdbc 1

  1. 1. JDBC
  2. 2. How can I get information from database in myJava application?
  3. 3. JDBC is the Java-based API, which provides a setof classes and interfaces written in Java to accessand manipulate different kinds of databases
  4. 4. No matter what database is used!
  5. 5. JDBC takes care for you I don’t care eh eh eh … Developer
  6. 6. JDBC is not an acronym.It is often mistaken to be "Java Database Connectivity"
  7. 7. Advantages Continued usage of existing data Vendor independent Platform independent (Cross platform) Ease of use Behind the scenes database
  8. 8. Two-Tier Data Processing Model
  9. 9. Three-Tier Data Processing Model
  10. 10. JDBC API
  11. 11. JDBC Drivers. Converts the client request to a db understandable, nativeformat and present it to db.. Converts the response to the Java format and presented to theclient.
  12. 12. JDBC Type 1 Driver (JDBC-ODBC Bridge)Advantages : MS Access or Microsoft SQL ServerDisadvantages: don’t support software installation on client native ODBC libraries must reside on the server =>reduces the performance
  13. 13. JDBC Type 2 Driver (Native-API partly Java Driver)Advantages : Faster than Type 1Disadvantages: don’t support software installation on client
  14. 14. JDBC Type 3 Driver (JDBC Net pure Java Driver)Converts JDBC calls into a DBMS-independent network protocol,which is translated to database-specific calls by a middle-tierserver.Advantages : The most flexible type, High adaptability, controlunderlying database without modifying the client side driver.Disadvantages: Database-specific code needs to be executed in themiddle- tier server. Needs to implement security like firewalls.
  15. 15. JDBC Type 4 Driver (Native-protocol pure Java Driver, Java to DBProtocol)Convert JDBC calls into the network protocol that communicatesdirectly with the database.Advantages : Communicate directly with the db engine using JavaSockets. Fastest JDBC drivers available. No addition on clients.Disadvantages: database-specific
  16. 16. Steps to develop a JDBC App
  17. 17. Loading Driver Class.forName(<protocol>) Class.forName(“”);Establishing a connection Connection object DriverManager.getConnection() Connection cn = DriverManager.getConnection("jdbc:sqlserver://localhost;DatabaseName=Dev eloperApps;user=sa;Password=123456");
  18. 18. Creating statement and queries Statement interface Statement PreparedStatement CallableStatement connection.createStatement() PreparedStatement pstmt = cn.prepareStatement(“Select * from ToppingDetails”);
  19. 19. executeQuery() & ResultSet object connection.createStatement() PreparedStatement pstmt = cn.prepareStatement(“Select * from ToppingDetails”); ResultSet rs = pstmt.executeQuery();executeUpdate() INSERT, DELETE, UPDATE, SQL DDL (Data DefinitionLanguage) return int value : row countexecute() Return more than one result set Return true if a result set object is generated
  20. 20. Handling exeption ClassNotFoundException SQLException
  21. 21. Processing queries Data in the ResultSet is in a tabular format. The initial cursor is positioned before the first row Traverse using next() method. next() return : true if the current cursor position is on a valid row false if the cursor is placed at a position after the last row
  22. 22. Extract the data getString() getInt() getFloat() getObject()
  23. 23. Closing the Database Connection close() method is provided by the Connection,Statement, ResultSet objects. rs.close(); pstmt.close(); cn.close();
  24. 24. Relationship of Connection to other objects
  25. 25. Interactions&relationshipsbetweenthe key JDBCclassesand interfaces
  26. 26. Database metadata Data about data. defines structure and properties of the data stored. Column name Datatypes for columns
  27. 27. DatabaseMetaData interface getMetaData() Connection obj DatabaseMetaData dmd = cn.getMetaData()
  28. 28. DatabaseMetaData method
  29. 29. ResultSetMetaData interface metadata of ResultSet. ResultSetMetaData rmd = rs.getMetaData()
  30. 30. ResultSetMetaData method getColumnName() getColumnCount() getColumnType() String colName = rmd.getColumnName(2); int totalCols = rmd.getColumnCount(); String colType = rmd.getColumnType(1);