0% found this document useful (0 votes)
62 views3 pages

JDBC

Java can connect to Oracle databases using JDBC, which acts as a bridge between Java's object-oriented approach and Oracle's table-based SQL interface. To do so, a Java program first registers the Oracle JDBC driver, then uses the DriverManager to get a Connection. With a Connection, the program can create Statements to execute SQL queries and updates and process the results.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views3 pages

JDBC

Java can connect to Oracle databases using JDBC, which acts as a bridge between Java's object-oriented approach and Oracle's table-based SQL interface. To do so, a Java program first registers the Oracle JDBC driver, then uses the DriverManager to get a Connection. With a Connection, the program can create Statements to execute SQL queries and updates and process the results.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Java + JDBC + Oracle

Java:   An object-oriented procedural programming language

Oracle:   A table-based declarative interface to data (SQL)

JDBC:   A bridge between these two worlds ... Java + persistent data

Alternatives:   ObjectStore,   PJama,   J++ & ODBC,   ...

JDBC Architecture

Java/DB Interaction
import java.sql.*;

...

Statement s = ...
// Run SQL query to fetch data
ResultSet r = s.executeQuery("select name from Employees");

// Iterate through result set


while (r.next())
System.out.println(r.getString("name"));
...

... Java/DB Interaction

import java.sql.*;

...

Statement s = ...

// Generate update command


int lowPay = 20000;
String sackWorkers =
"delete Employees where salary < "+lowPay;

// Run SQL update to modify data


int nr = s.executeUpdate(sackWorkers);

// Indicate update effect


System.out.println(nr + " employees laid off");
...

Getting Connected
1. Register driver
2. DriverManager.registerDriver(
3. new oracle.jdbc.driver.OracleDriver);

or
Class.forName("oracle.jdbc.driver.OracleDriver");

4. Make connection through driver


5. Connection conn =
6. DriverManager.getConnection(URL, Username, Password);

... Getting Connected

Username and Username are the ones for your Oracle account.

The URL specifies:
 the protocol (jdbc)
 the particular DBMS (in this case, oracle)
 the specific driver (Oracle supplies thin, oci7, oci8)
 the connection details for local environment ( Host:Port:Database)

For the CSE environment:


jdbc:oracle:thin:@dingo:1521:cse1

or
jdbc:oracle:oci7:@(DESCRIPTION =
(ADDRESS_LIST=(PROTOCOL=TCP)(Host=dingo)(Port=1521))
(CONNECT_DATA=(SID=cse1)))

Making a Statement
Each database operation has to be done via a Statement.

Each Statement is attached to a Connection.


Statement s = conn.createStatement();

Multiple Statements can be handled by a Connection.

Transactions are also handled by the Connection.


conn.commit();

All transactions are committed and all Statements are closed when


a Connection closes.

You might also like