# JDBC

[http://www.informit.com/articles/article.aspx?p=26251\&seqNum=3](https://www.gitbook.com/book/linhangyouxiang/cs-notebook1/edit)

## 1. Fundamental steps in the JDBC

* **import JDBC packages.**

```
import oracle.jdbc.driver.*;
import oracle.sql.*;
```

```
import java.sql.*;
```

* **Load and register the JDBC driver.**

```
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Class.forName("oracle.jdbc.driver.OracleDriver");
```

* **Open a connection to the database.**

```
Connection conn = DriverManager.getConnection(URL, username, passwd);
Connection conn = DriverManager.getConnection(URL);
```

* **Create a statement object to perform a query.**

```
Statement sql_stmt = conn.createStatement();
```

* **Execute the statement object and return a query resultset.**
* **Process the resultset.**
* **Close the resultset and statement objects.**
* **Close the connection.**

## 2.Please always put any closing stuff in the finally block.

## 3. exceptions

Exceptions in JDBC are usually of two types:

* Exceptions occurring in the JDBC driver
* Exceptions occurring in the Oracle 8i database itself

### 4. Flow

* step1: import required packages
* step2: Register JDBC driver
* step3: open a connection
* step4: execute a query
* step5: extract data from result set
* step6: clean-up environment

![](https://583140292-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgDoNtsmWjvbK_E54fX%2F-LgDoUQjXWGykzNUibU8%2F-LgDokTWfaW-firgz5pj%2Fjdbcflow0.png?generation=1559321840556750\&alt=media)

![](https://583140292-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgDoNtsmWjvbK_E54fX%2F-LgDoUQjXWGykzNUibU8%2F-LgDokTZMPADRnQfU3n0%2Fjdbcflow1.png?generation=1559321840551572\&alt=media)
