Using JDBC To Access The Database
Using JDBC To Access The Database
Using JDBC To Access The Database
Web Application
Client server server Data
Business
Presentation
logic
Oracle
Oracle database
Application Server 10g
Java EE Certified
Environment
JDBC
// Standard packages
import java.sql.*;
import java.math.*; // optional
// Oracle extension to JDBC packages
import oracle.jdbc.*;
import oracle.sql.*;
2. Obtain a connection.
OracleDriver
Database
JDBC calls • Thin client commands
• OCI based Database
• Server based
•…
Applet
JDBC
Oracle
Thin driver
Client Server
Application
JDBC
OCI driver
Oracle
ocixxx.dll
Client Server
Application
JDBC
OCI driver
Oracle
ocixxx.dll
Client Server
Applet Thin
Thin
EJB, servlet
(on the middle tier)
OCI
jdbc:<subprotocol>:<subname>
jdbc:oracle:<driver>:@<database>
Connection conn =
DriverManager.getConnection(
"jdbc:oracle:thin:@myhost:1521:ORCL",
"scott","tiger");
Statement stmt = conn.createStatement();
Example:
Statement stmt = conn.createStatement();
int rowcount = stmt.executeUpdate
("DELETE FROM order_items
WHERE order_id = 2354");
Example:
Statement stmt = conn.createStatement();
int rowcount = stmt.executeUpdate
("CREATE TABLE temp (col1 NUMBER(5,2),
col2 VARCHAR2(30)");
import java.sql.*;
class TestJdbc {
public static void main (String args [ ]) throws
SQLException {
DriverManager.registerDriver (new
oracle.jdbc.OracleDriver());
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:@myHost:1521:ORCL","scott",
"tiger");
Statement stmt = conn.createStatement ();
ResultSet rset = stmt.executeQuery
("SELECT ename FROM emp");
while (rset.next ())
System.out.println (rset.getString ("ename"));
rset.close();
stmt.close();
conn.close();
}
}
PreparedStatement pstmt =
conn.prepareStatement
("UPDATE emp SET ename = ? WHERE empno = ?");
PreparedStatement pstmt =
conn.prepareStatement
("SELECT ename FROM emp WHERE empno = ?");
Java servlet
Data source
ConnectionPoolDataSource
JDBC Database
driver
Database
commands