Unit 2 JDBC Programming 28012018 040330AM
Unit 2 JDBC Programming 28012018 040330AM
JDBC Drivers
1. Type1 (JDBC-ODBC Driver)
Depends on support for ODBC
Type1 is not portable driver
Translate JDBC calls into ODBC calls and use Windows ODBC built in drivers
ODBC must be set up on every client
For server side servlets ODBC must be set up on web server
Driver sun.jdbc.odbc.JdbcOdbc provided by JavaSoft with JDK
No support from JDK 1.8 (Java 8) onwards.
E.g. MS Access
Disadvantages
Compared to Type 2 drivers, Type 3 drivers are slow due to increased number of network calls.
Requires database-specific coding to be done in the middle tier.
The middleware layer added may result in additional latency, but is typically overcome by using
better middleware services.
Disadvantage
This Driver uses database specific protocol and it is DBMS vendor dependent.
Q2. Explain Thick and Thin driver. Comment on selection of driver. Write code
snippet for each type of JDBC connection.
Ans. Thick driver
Thick client would need the client installation.
E.g. Type 1 and Type 2.
Thin driver
The thin client driver, which mean you can connect to a database without the client installed on your
machine.
E.g. Type 4
Connection conn=
DriverManager.getConnection("jdbc:mysql://localhost:PortNo/database
Name",“uid”, “pwd”);
2. Oracle
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=
DriverManager.getConnection("jdbc:oracle:thin:@hostname:port
Number:databaseName","root", "pwd");
3. DB2
Class.forName("com.ibm.db2.jdbc.net.DB2Driver");
Connection conn=
DriverManager.getConnection("jdbc:db2:hostname:port Number
/databaseName")
1. java.sql.*;
2. public class ConnDemo {
3. public static void main(String[] args) {
4. try {
5. // Load and register the driver
6. Class.forName("com.mysql.jdbc.Driver");
Advantages:
The performance of the application will be faster, if you use PreparedStatement interface
because query is compiled only once.
This is because creating a PreparedStatement object by explicitly giving the SQL statement
causes the statement to be precompiled within the database immediately.
Thus, when the PreparedStatement is later executed, the DBMS does not have to recompile
the SQL statement.
Late binding and compilation is done by DBMS.
Provides the programmatic approach to set the values.
Disadvantage:
The main disadvantage of PreparedStatement is that it can represent only one SQL statement at a
time.
Example of PreparedStatement
Write a program to insert student records to database using prepared statement
1. import java.sql.*;
2. public class PreparedInsert {
3. public static void main(String[] args) {
4. try {
5. Class.forName("com.mysql.jdbc.Driver");
6. Connection conn= DriverManager.getConnection
7. ("jdbc:mysql://localhost:3306/DIET", "root","pwd");
Parameter Description
INOUT A parameter that provides both input and output values. You bind
variables with the setXXX() methods and retrieve values with the
getXXX() methods.
int getInt Returns the integer value to the current row in the specified
(int columnIndex) column index. The column index starts at 1, meaning the
throws SQLException first column of a row is 1, the second column of a row is 2,
and so on.
String getString Retrieves the value of the designated column in the current
(String columnLabel) row of this ResultSet object as a String in the Java
throws SQLException programming language.
String getString Retrieves the value of the designated column in the current
(int columnIndex) row of this ResultSet object as a String in the Java
throws SQLException programming language.
Concurrency of ResultSet
ResultSet.CONCUR_READ_ONLY Creates a read-only result set. (Default Type)
ResultSet.CONCUR_UPDATABLE Creates an updateable result set.
Example
Statement stmt = conn.createStatement(
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
Example: ResultSetMetaData
1.import java.sql.*;
2.public class MetadataDemo {
3.public static void main(String[] args) {
4. try {Class.forName("com.mysql.jdbc.Driver");
5. Connection conn= DriverManager.getConnection
6. ("jdbc:mysql://localhost:3306/gtu", "root",“pwd");
7.Statement stmt = conn.createStatement
(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
8. ResultSet rs = stmt.executeQuery("SELECT * from gtu");
9. ResultSetMetaData rsmd=rs.getMetaData();
10. System.out.println("Total columns:
"+rsmd.getColumnCount());
11. System.out.println("Column Name of 1st column:
"+rsmd.getColumnName(1));
12. System.out.println("Column Type Name of 1st column:“
+rsmd.getColumnTypeName(1));
13. stmt.close();
14. conn.close();
15. }catch(Exception e)
16. {System.out.println(e.toString());}
17. }//PSVM
18. }//class
OUTPUT:
Total columns: 3
Column Name of 1st column:Enr_no
Column Type Name of 1st column:INT
System.out.println("getDriverName():"+dbmd.getDriverName())
;
System.out.println("getDriverVersion():
"+dbmd.getDriverVersion());
System.out.println("getURL():"+dbmd.getURL());
System.out.println("getUserName():"+dbmd.getUserName());
Example
con.setTransactionIsolation(8);
System.out.println("con.getTransactionIsolation():"
+con.getTransactionIsolation());
1.Class.forName("com.mysql.jdbc.Driver");
2.Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/GTU","root","root");
3.con.setAutoCommit(false);
4.Statement stmt=con.createStatement();
5.String query1,query2,query3,query4,query5;
6.query1="create table DietStudent(enr INT PRIMARY
KEY, name VARCHAR(20),sem INT,branch
VARCHAR(10))";
7.query2="insert into DietStudent
values(6001,'java',6,'ce')";
8.query3="insert into DietStudent
values(6002,'php',6,'ce')";
9.query4="update DietStudent set name='cg' where
enr=6002";
10. query5="delete from DietStudent where
name='java'";
11. stmt.addBatch(query1);
12. stmt.addBatch(query2);
13. stmt.addBatch(query3);
14. stmt.addBatch(query4);
15. stmt.addBatch(query5);
16. int[] i=stmt.executeBatch();
17. con.commit();
3. Give the use of Statement, PreparedStatement and CallableStatement object. [Win -14]
Write code to insert three records into student table using PreparedStatement
(assume student table with Name, RollNo, and Branch field).
4. What is phantom read in JDBC? Which isolation level prevents it? [Sum -16]