JDBC
JDBC
JDBC
JDBC
Lets programmers connect to a database, query it or update through a Java application.
JDBC library is implemented in java.sql package.
Database commands
A driver is a program that converts the Java method calls to the corresponding method calls understandable by the database in use. JDBC loads a driver Driver talks to a particular database An application can work with several databases by using all corresponding drivers
JDBC Driver
A driver is a program that converts the Java method calls to the corresponding method calls understandable by the database in use.
JDBC Architecture
Java Application
JDBC API
Database
Database
Oracle
Java Application
JDBC
DB2 Driver
DB2 Network
MySQL Driver
MySQL
6
Types of JDBC(Drivers)
JDBC-ODBC Bridge Native-API partly Java Driver Net-Protocol All-Java Driver (Type 1= Bridge) (Type 2= Native) (Type 3= Middleware )
(Type 4= Pure)
portable.
2. Due to bridge, slowest of all driver types. 3. Not good for the Web.
Disadvantage
With type 4 drivers, the user needs a different driver for each database.
Connection con =
DriverManager.getConnection(URL);
Create a Statement
Driver
Connection
Statement
ResultSet
Making a Connection
There are several getConnection() methods on DriverManager with
Statement
A Statement object is used for executing a SQL statement and obtaining the results produced by it.
Statement Methods
ResultSet executeQuery(String)
Execute a SQL statement that returns a single ResultSet.
int executeUpdate(String)
Execute a SQL INSERT, UPDATE or DELETE statement. Returns the number of rows changed.
boolean execute(String)
Execute a SQL statement that may return multiple results.
ResultSet
A ResultSet provides access to a table of data generated by executing a Statement. Only one ResultSet per Statement can be open at once. The table rows are retrieved in sequence. A ResultSet maintains a cursor pointing to its current row of data.
ResultSet Methods
boolean next()
activates the next row the first call to next() activates the first row returns false if there are no more rows
void close()
disposes of the ResultSet
ResultSet Methods
Type getType(int columnIndex) returns the given field as the given type fields indexed starting at 1 (not 0)
ResultSet Methods
String getString(int columnIndex)
ResultSet Methods
String getString(String columnName)
PreparedStatement
It has set of methods that can be used for sending queries with input parameters. PreparedStatement stat = con. PreparedStatement( Select
// connect to DB
try{
con = DriverManager.getConnection("jdbc:odbc:my_database"); } catch(SQLException se) {
System.out.println(se);
} System.out.println("connection is successful!!!");
try{
String selectSQL = "select ID, NAME, ADDRESS from tb_address"; Statement stat = conn.createStatement(); ResultSet rs = stmt.executeQuery(selectSQL);
while(rs.next()){
System.out.println("ID: " + rs.getString(1) + " NAME: " + rs.getString(2) + " ADDRESS:" +rs.getString(3));
}
stat.close(); } catch(SQLException se) {
System.out.println(se);
} }