Unit 4 Part 2 Database Programming With JDBC
Unit 4 Part 2 Database Programming With JDBC
• easy to use.
• can be easily connected to any database.
Disadvantages:
Disadvantage:
• The Native driver needs to be installed on the each
client machine.
• The Vendor client library needs to be installed on
client machine.
Network Protocol driver
• The Network Protocol driver uses middleware (application
server) that converts JDBC calls directly or indirectly into the
vendor-specific database protocol. It is fully written in java.
Network Protocol driver
Advantage:
• No client side library is required because of application
server that can perform many tasks like auditing, load
balancing etc.
Disadvantages:
• Network support is required on client machine.
• Requires database-specific coding to be done in the
middle tier.
• Maintenance becomes costly because it requires
database-specific coding to be done in the middle tier.
Thin driver
• The thin driver converts JDBC calls directly into the
vendor-specific database protocol. That is why it is
known as thin driver. It is fully written in Java
language.
Thin driver
Advantage:
• Better performance than all other drivers.
• No software is required at client side or server side.
Disadvantage:
• Drivers depend on the Database.
Steps to connect to the database in java
DriverManager.registerDriver()
2) Create the connection object
The getConnection() method of DriverManager class
is used to establish connection with the database.
Insert
prepared statement
Insert record: (Using prepared statement)
PreparedStatement stmt=con.prepareStatement("insert
into Emp values(?,?)");
stmt.setInt(1,101); //1 specifies the first parameter in query
stmt.setString(2,“ABC");
int i=stmt.executeUpdate();
System.out.println(i+" records inserted");
Update:
int i=stmt.executeUpdate();
System.out.println(i+" records updated");
Interfaces Uses
Statement Used for general-purpose access to your database. Useful
when you are using static SQL statements at runtime. The
Statement interface cannot accept parameters.
PreparedStatement Used when you plan to use the SQL statements many
times. The PreparedStatement interface accepts input
parameters at runtime.
CallableStatement Used when you want to access the database stored
procedures. The CallableStatement interface can also
accept runtime input parameters.
Transaction Management in JDBC
• Transaction represents a single unit of work.
• The ACID properties describes the transaction
management well. ACID stands for Atomicity, Consistency,
isolation and durability.
Statement stmt=con.createStatement();
stmt.executeUpdate("insert into emp values(190,'abhi',40000)");
stmt.executeUpdate("insert into emp values(191,'umesh',50000)");
con.commit();
con.close();
}
JDBC - Stored Procedure
Java CallableStatement Interface
must not have the return type. must have the return type.
Parameter Description
A parameter whose value is unknown when the SQL
IN statement is created. You bind values to IN parameters
with the setXXX() methods.
Example:
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(" jdbc:mysql://localhost:
3306 /EMPLOYEE ","system",“sqll");
System.out.println(stmt.getInt(1));
}
}
What are the major components of the JDBC?
• Type 3 Driver
• Type-2 Driver
• Type-4 Driver
• Type-1 Driver
What is the correct sequence to create a database connection?
• executeResult()
• executeQuery()
• executeUpdate()
• execute()
Which methods are required to load a database driver
in JDBC?
• getConnection()
• registerDriver()
• forName()
• Both b and c
Which of the following is not a valid statement in JDBC?
• Statement
• PreparedStatement
• QueryStatement
• CallableStatement
What does setAutoCommit(false) do?