JDBC
• JDBC provides java programmers with a uniform interface for accessing and manipulating a wide range of relational DBs .
• JDBC API : is a set of java interfaces & classes used to write java programs for accessing and manipulating relational databases
• JDBC Driver : serves as the interface to facilitate communication between JDBC and a proprietry database
Note : JDBC Driver are database specific .
Developing Database Applications using JDBC :
• JDBC API : is a java application program interface to generic databases that enables java developers to develop DBMS , implement java applications using a uniform interface.
• JDBC API consists of classes and interfaces for establishing connections with databases ,sending SQL statements into the database ,processing the results of the SQL statements and obtaining database metadata.
- four key interfaces are needed to develop any database application using java Driver ,connection ,statement,resultset.
- These interfaces define a framework for generic SQL database access .
-JDBC Drivers vendors provide implementation for them
The Relationship of these interfaces is
A typical java program takes the steps outlined below to access the database
1- Loading Drivers
- an appropriate driver must be loaded using the statement shown below before establishing a connection to the database .
Class.forName(“JDBC Driver Class”);
- a driver is a class that implements the
java.sql.Driver interface .
Database Driver Class
Access Sun.jdbc.odbc.JdbcOdbcDriver
MySQL Com.mysql.jdbc.Driver
Oracle Oracle.jdbc.driver.OracleDriver
2-Establishing connections :-
to connect to a database use the static method getCnnecion(“Database URL”);
Connection con=DriverManager.getConnection(“URL”);
Database URL
Access Jdbc:odbc:datasource
MySQL Jdbc:mysql://hostname/dbname
Oracle Jdbc:oracle:thin:@hostname:port#:oracleDBSID
Connection con=DriverManager.getConnection(“”,UN,PASS);
2- Creating Statements
Statement stmt=con.createStatement();
3 –Executing Statements
stmt.ExecuteUpdate(“non query statement”);
Resultset rs= stmt.ExecuteQuery(“query statement”);
3- Processing Resutset
While(rs.read())
System.out.println(rs.getString(1));
Prepared Statements
- The static statement interface is used to execute static SQL statements that contain no parameters.
- The prepared statement interface extending statement interface is used to execute a precompiled SQL statements with or without IN parameters.
Since the SQL statements are precompiled they are effective for repeated execution.
PreparedStatement pstmt=Connection.PreparedStatement(“insert into table values(?,?,?,?)”);
Pstmt.setString (1, value);
Batch ProcessingStatement stmt=con.createStatement();
Stmt.addBatch(“create table t1 (….)”);
Stmt.addBatch(“INSERT INTO t1 VALUES(“”,….)”);
Stmt.addBatch(“INSERT INTO t1 VALUES(“”,….)”);
Int count[] =stmt.executeBatch();b
Scrollable & updatable ResultSet
The possible values of the resultset type are constants defined in the resultset :
1-Type_forward_only
2-Type_scroll_insensitive
3-Type_scroll_sensitive
The possible values of the resultset concurrency
1-concur_Read_Only.
2-concur_Updatable.
Statement stmt=Con.createStatement(ResultSet.Type_scroll_sensetive,
ResultSet.concur_Updatable)
ResultSet rs=stmt.executeQuery(query);