jdbc 1
TRANSCRIPT
JDBC
How can I get information from database in my Java application?
JDBC is the Java-based API, which provides a set of classes and interfaces written in Java to access and manipulate different kinds of databases
No matter what database is used!
JDBC takes care for you Developer
I don’t care eh eh eh …
JDBC is not an acronym. It is often mistaken to be "Java Database Connectivity"
Advantages
Continued usage of existing data Vendor independent Platform independent (Cross platform) Ease of use
Behind the scenes database
Two-Tier Data Processing Model
Three-Tier Data Processing Model
JDBC API
JDBC Drivers
. Converts the client request to a db understandable, native format and present it to db. . Converts the response to the Java format and presented to the client.
JDBC Type 1 Driver (JDBC-ODBC Bridge)
Advantages : MS Access or Microsoft SQL Server Disadvantages: don’t support software installation on client native ODBC libraries must reside on the server => reduces the performance
JDBC Type 2 Driver (Native-API partly Java Driver)
Advantages : Faster than Type 1 Disadvantages: don’t support software installation on client
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-tier server.
Advantages : The most flexible type, High adaptability, control underlying database without modifying the client side driver. Disadvantages: Database-specific code needs to be executed in the middle- tier server. Needs to implement security like firewalls.
JDBC Type 4 Driver (Native-protocol pure Java Driver, Java to DB Protocol) Convert JDBC calls into the network protocol that communicates directly with the database. Advantages : Communicate directly with the db engine using Java Sockets. Fastest JDBC drivers available. No addition on clients. Disadvantages: database-specific
Steps to develop a JDBC App
Loading Driver Class.forName(<protocol>)
Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);
Establishing a connection Connection object DriverManager.getConnection() Connection cn = DriverManager.getConnection("jdbc:sqlserver://localhost;DatabaseName=Dev eloperApps;user=sa;Password=123456");
Creating statement and queries Statement interface Statement
PreparedStatement CallableStatement
connection.createStatement() PreparedStatement pstmt = cn.prepareStatement(“Select * from ToppingDetails”);
executeQuery() & ResultSet object connection.createStatement() PreparedStatement pstmt = cn.prepareStatement(“Select * from ToppingDetails”); ResultSet rs = pstmt.executeQuery();
executeUpdate() INSERT, DELETE, UPDATE, SQL DDL (Data Definition Language) return int value : row count
execute() Return more than one result set Return true if a result set object is generated
Handling exeption ClassNotFoundException SQLException
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
Extract the data getString() getInt() getFloat() getObject()
Closing the Database Connection close() method is provided by the Connection, Statement, ResultSet objects. rs.close(); pstmt.close(); cn.close();
Relationship of Connection to other objects
Interactions & relationships between the key JDBC classes and interfaces
Database metadata Data about data. defines structure and properties of the data stored. Column name Datatypes for columns
DatabaseMetaData interface getMetaData() Connection obj DatabaseMetaData dmd = cn.getMetaData()
DatabaseMetaData method
ResultSetMetaData interface metadata of ResultSet. ResultSetMetaData rmd = rs.getMetaData()
ResultSetMetaData method getColumnName() getColumnCount() getColumnType() String colName = rmd.getColumnName(2); int totalCols = rmd.getColumnCount(); String colType = rmd.getColumnType(1);