Professional Documents
Culture Documents
JDBC
JDBC
Why JDBC ?
Using JDBC , java program can interact with RDBMS like Oracle , SQL Server ,DB2 ,Access etc Java program becomes RDBMS independent , that is same java program can interact with any kind of RDBMS
Java program
RDBMS
Java program
ODBC driver
Java program
ODBC driver
JDBC-ODBC Bridge
RDBMS
Client
Java Program
JDBC-ODBC Bridge
Translates JDBC calls to ODBC calls Requires installed ODBC Driver (non-Java) on client machine Comes with JDK
JDBC-ODBC Bridge
Advantages: Can be useful for databases where other methods not available (e.g., MSAccess) Disadvantages: Client computer needs to be configured with ODBC driver and ODBC instance which specifies database server and other information JDBC performance relies on ODBC driver
Java program
JDBC API
RDBMS Client
(provided by Sun)
Java program
JDBC Driver
Client
Java Program
JDBC Driver
Client
Java Program
RDBMS
JDBC Driver
Client
Java Program
How to Connect?
Connection involves two steps: Step 1 - Loading the driver Step 2 - Making the connection
Points to be noted
There exists a class whose name in : Class in java.lang package forName is static method of the class :Class. Parameter of forName is string . This string is package qualified class name.
In jdbc we have to supply appropriate driver class name
Points to be noted
Task of forName is to load byte codes of the specified class name in JVM memory. If it fails to do that , it will throw : ClassNotFoundException
Points to be noted
JDBC Driver is available as jar file .
Jdbc driver is java program.
Driver class is part of this jar file . This jar file must be configured in classpath.
otherwise forName will fail to load this class and it will throw ClassNotFound Exception.
JDBC URLs
Specially formatted string , which contains informations like :
What type of driver to use .(type 1, type 2 ,.) What rdbms to connect. What is hostname,port Other databs information
Subprotocol :
Determines what rdbms driver to use i.e driver for oracle / driver for DB2 /driver for mysql
Subname :
What type to use ( Type 1 , Type 2 ,.) Format depends on subprotocol.
jdbc:oracle:oci8:@database
jdbc:db2://orderserver:50000/SAMPLE jdbc:mysql://orderserver:3306/orderdb
Executing a SQL
In JDBC SQL statements can be of two types
Not precompiled (Basic SQL) Precompiled (by the driver)
To execute a not precompiled SQL , an instance of java.sql.Statement is required. java.sql.Statement is an interface Statement stmt = con.createStatement();
Where con is an instance of java.sql.Connection
SQL Statements
Create some form of Statement
Statement
Represents a basic SQL statement Statement stmt = conn.createStatement();
PreparedStatement
A precompiled SQL statement, which can offer improved performance, especially for large/complex SQL statements
Callable Statement
Allows JDBC programs access to stored procedures
Class.forName(oracle.jdbc.driver.OracleDriver); String url=jdbc:oracle:thin:@machine:1521:ins-name; Connection conn=DriverManager.getConnection(url,scott,tiger); Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery(select empno,ename from emp);
When getter is invoked , JDBC driver converts underlying data of the RDBMS into java language type JDBC specification has defined mapping from SQL type to java type
Class.forName(oracle.jdbc.driver.OracleDriver); String url=jdbc:oracle:thin:@machine:1521:ins-name; Connection conn=DriverManager.getConnection(url,scott,tiger); Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery(select empno,ename from emp); while ( rs.next()){ int eno=rs.getInt(1); String name=rs.getString(ename) ; // sysout }
Example
PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES SET SALARY = ? WHERE ID = ?"); pstmt.setFloat(1, 15383.00f); pstmt.setInt(2, 7992) ; int updatecount=pstmt.executeUpdate(); pstmt.close()