4. Database Connectivity using JDBC .ppt

HITENKHEMANI 13 views 22 slides Oct 19, 2024
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

Database connectivity by jdbc


Slide Content

JDBC – CONNECTIONS
1

ESTABLISHING JDBC CONNECTION
Import JDBC Packages
Register JDBC Driver
Database URL Formulation
Create Connection Object
Close Connection
2

PROCESS / CONNECTING DATABASE
Import JDBC Packages
Load and Register JDBC Driver
Establish connection
Create statement
Execute query
Process result
Close Connection
3

IMPORT JDBC PACKAGES
Requires that you include the packages containing
the JDBC classes needed for database programming.
import java.sql.*;
Or
import java.sql.Connection;  
import java.sql.DriverManager; 
4

REGISTER JDBC DRIVER
The most common approach to register a driver is to use
Java's Class.forName() method, to dynamically load the
driver's class file into memory, which automatically
registers it.
This method is preferable because it allows you to make
the driver registration configurable and portable.
to register the Oracle driver −
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException ex)
{
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
5

DATABASE URL FORMULATION /
CREATE CONNECTION
After you've loaded the driver, you can establish a
connection using
the DriverManager.getConnection() method. For
easy reference, let me list the three overloaded
DriverManager.getConnection() methods −
•getConnection(String url)
•getConnection(String url, Properties prop)
•getConnection(String url, String user, String
password)
6

CONTINUE…
popular JDBC driver names and database URL
All the highlighted part in URL format is static and you
need to change only the remaining part as per your
database setup.
RDBMS JDBC driver name URL format
MySQL com.mysql.jdbc.Driver jdbc:mysql://hostname/
databaseName
ORACLE oracle.jdbc.driver.OracleDriverjdbc:oracle:thin:@hostname:port
Number:databaseName
DB2 COM.ibm.db2.jdbc.net.DB2Driverjdbc:db2:hostname:port
Number/databaseName
Sybase com.sybase.jdbc.SybDriver jdbc:sybase:Tds:hostname: port
Number/databaseName
7

CREATE CONNECTION OBJECT
Connection con = DriverManager.getConnection(String url);
String uname = “system";
String passwd = “manager";
String url="jdbc:mysql://172.21.170.15/sybca_dap";
Connection con =
DriverManager.getConnection(url, uname,
passwd);
8

CLOSING JDBC CONNECTION
Explicitly closing a connection conserves
DBMS resources, which will make your
database administrator happy.
To close the above opened connection, you
should call close() method as follows −
con.close();
9

JDBC – STATEMENT
OBJECTS
10

CREATING STATEMENT OBJECT
The JDBC Statement interfaces define the methods
and properties that enable you to send SQL or
PL/SQL commands and receive data from your
database.
Before you can use a Statement object to execute a
SQL statement, you need to create one using the
Connection object's createStatement( ) method,
Statement stmt = null;
try {
stmt = conn.createStatement( ); . . .
}
catch (SQLException e) {
. . . }
finally { . . . }
11

EXECUTING STATEMENT
Boolean execute(String SQL)  : Returns a Boolean value of
true if a ResultSet object can be retrieved; otherwise, it returns
false. Use this method to execute SQL DDL statements or when
you need to use truly dynamic SQL.
int executeUpdate(String SQL)  : Returns the numbers of
rows affected by the execution of the SQL statement. Use this
method to execute SQL statements for which you expect to get a
number of rows affected - for example, an INSERT, UPDATE, or
DELETE statement.
ResultSet executeQuery(String SQL)  : Returns a ResultSet
object. Use this method when you expect to get a result set, as
you would with a SELECT statement. 12

CLOSING STATEMENT OBJECT
Statement stmt = null;
try {
stmt = conn.createStatement( );
. . .
}
catch (SQLException e) {
. . . }
finally {
stmt.close();
}
13

JDBC – RESULTSET
14

WHAT IS RESULTSET ??...
The SQL statements that read data from a database
query return the data in a result set.
The SELECT statement is the standard way to select
rows from a database and view them in a result set. The
java.sql.ResultSet interface represents the result set of a
database query.
A ResultSet object maintains a cursor that points to the
current row in the result set. The term "result set"
refers to the row and column data contained in a
ResultSet object.
15

RESULTSET METHODS
The methods of the ResultSet interface can be broken
down into three categories:
Navigational methods: used to move the cursor
around.
Get methods: used to view the data in the columns of
the current row being pointed to by the cursor.
Update methods: used to update the data in the
columns of the current row. The updates can then be
updated in the underlying database as well.
16

RESULTSET PROPERTIES
createStatement(int RSType, int
RSConcurrency);
Possible Values of RSType
17

RESULTSET PROPERTIES
createStatement(int RSType, int
RSConcurrency);
Possible Values of RSConcurrency
18

CURSOR MOVEMENT
void beforeFirst() 
void afterLast() 
boolean first()
void last()
boolean previous()
boolean next()
and others….
19

VIEWING A RESULTSET
The ResultSet interface contains dozens of
methods for getting the data of the current
row.
For example, if the column you are interested
in viewing contains an int, you need to use one
of the getInt() methods of ResultSet:
getInt(columnname)
getInt(columnindex)
20

EXAMPLE TO CONNECT JAVA
APPLICATION WITH ORACLE
DATABASE
import java.sql.*;  
class OracleCon{  
public static void main(String args[]){  
try{  
//step1 load the driver class  
Class.forName("oracle.jdbc.driver.OracleDriver");  
  
//step2 create  the connection object  
Connection con=DriverManager.getConnection(  
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle"); 
 
  
21

//step3 create the statement object  
Statement stmt=con.createStatement();  
  
//step4 execute query  
ResultSet rs=stmt.executeQuery("select * from emp");  
while(rs.next())  
System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.ge
tString(3));  
  
//step5 close the connection object  
con.close();  
  
}catch(Exception e){ System.out.println(e);}  
  
}  
}  
22