JDBC Part1
JDBC Part1
1 Enterprise Java
JDBC Definition
• JDBC stands for Java Database Connectivity, which is a standard Java
API for database-independent connectivity between the Java
programming language, and a wide range of databases.
• The JDBC library includes APIs for each of the tasks mentioned below that
are commonly associated with database usage.
JDBC Architecture
• The JDBC API supports both two-tier and three-tier processing models for
database access but in general, JDBC Architecture consists of two layers:
• The JDBC driver manager ensures that the correct driver is used to access
each data source. The driver manager is capable of supporting multiple
concurrent drivers connected to multiple heterogeneous databases.
Driver
Types of Drivers
• In a Type 2 driver, JDBC API calls are converted into native C/C++ API
calls, which are unique to the database.
• These drivers are typically provided by the database vendors and used in the
same manner as the JDBC-ODBC Bridge.
• This kind of driver is extremely flexible, you don't need to install special
software on the client or server.
• This is for making the JDBC API classes immediately available to the
application program.
• Syntax
import java.sql.*;
Class.forName(String);
• The forName() method of Class class is used to register the driver class.
• throws SQLException
• Statement stmt=con.createStatement();
• This method returns the object of ResultSet that can be used to get all the
records of a table.
• To process the result return from ResultSet object we use while loop with
ResultSet method next() as
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2));
• con.close();
• St.close();
PreparedStatement Interface
• The PreparedStatement interface is a subinterface of Statement. It is used to
execute parameterized query.
• String sql="insert into emp values(?,?,?)";
• As you can see, we are passing parameter (?) for the values. Its value will be
set by calling the setter methods of PreparedStatement
Method Description
public void setInt(int paramIndex, sets the integer value to the given
int value) parameter index.
public void setString(int sets the String value to the given
paramIndex, String value) parameter index.
public void setFloat(int sets the float value to the given
paramIndex, float value) parameter index.
public void setDouble(int sets the double value to the given
paramIndex, double value) parameter index.
public int executeUpdate() executes the query. It is used for create,
drop, insert, update, delete etc.
public ResultSet executeQuery() executes the select query. It returns an
instance of ResultSet.
1. import java.sql.*;
2. class InsertPrepared{
3. public static void main(String args[]){
4. try{
5. Class.forName("com.mysql.jdbc.Driver");
6.
7. Connection con=DriverManager.getConnection("jdbc:mysql://localhost/db1",
"root","root");
8.
9. PreparedStatement stmt=con.prepareStatement("insert into Emp values(?,?)
");
stmt.setInt(1,101);//1 specifies the first parameter in the query
stmt.setString(2,"Ratan");
int i=stmt.executeUpdate();
System.out.println(i+" records inserted");
con.close();
}
}
• The metadata means data about data i.e. we can get further information from
the data.
• If you have to get metadata of a table like total number of column, column
name, column type etc. , ResultSetMetaData interface is useful because it
provides methods to get metadata from the ResultSet object.
• Method • Description
import java.sql.*;
class Rsmd{
try{
Class.forName("com.mysql.jdbc.Driver ");
ResultSet rs=ps.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
con.close();
ResultSet interface
• But we can make this object to move forward and backward direction by
passing either TYPE_SCROLL_INSENSITIVE or
TYPE_SCROLL_SENSITIVE in createStatement(int,int) method as well as
we can make this object as updatable by:
ResultSet.CONCUR_UPDATABLE);
1) public boolean next(): • is used to move the cursor to the one row
next from the current position.
3) public boolean first(): • is used to move the cursor to the first row
in result set object.
4) public boolean last(): • is used to move the cursor to the last row
in result set object.
import java.sql.*;
class FetchRecord{
Class.forName("com.mysql.jdbc.Driver ");
Connection con=DriverManager.getConnection(""jdbc:mysql://localhost/db
1","root","root");
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIV
E,ResultSet.CONCUR_UPDATABLE);
rs.absolute(3);
con.close();
}}