使用JDBC简单调用Oracle数据库(jdbc调用oracle)
使用JDBC简单调用Oracle数据库
JDBC是Java Database Connectivity的缩写。它是一种Java能够与各种关系数据库进行连接和操作的标准API。Oracle是其中一种流行的数据库,本文将介绍如何通过JDBC简单调用Oracle数据库。
步骤一:下载并安装Oracle JDBC驱动
在开始之前,需要先下载Oracle JDBC驱动程序(ojdbc.x.jar)。该驱动程序可从Oracle官方网站上下载。下载后,将驱动程序放在项目的class path下。
步骤二:导入驱动
在使用Oracle JDBC驱动之前需要导入它。可以将以下代码放在Java文件中的任意位置:
“`java
import java.sql.*;
public class OracleJdbc {
static final String JDBC_DRIVER = “oracle.jdbc.driver.OracleDriver”;
static final String DB_URL = “jdbc:oracle:thin:@localhost:1521:xe”;
static final String USER = “username”;
static final String PASS = “password”;
public static void mn(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
System.out.println(“Connecting to database…”);
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println(“Creating statement…”);
stmt = conn.createStatement();
String sql;
sql = “SELECT id, name, age FROM employees”;
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt(“id”);
String name = rs.getString(“name”);
int age = rs.getInt(“age”);
System.out.print(“ID: ” + id);
System.out.print(“, Name: ” + name);
System.out.println(“, Age: ” + age);
}
rs.close();
stmt.close();
conn.close();
} catch(SQLException se) {
se.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if(stmt!=null)
stmt.close();
} catch(SQLException se2) {
} try {
if(conn!=null)
conn.close();
} catch(SQLException se) {
se.printStackTrace();
}
}
System.out.println(“Goodbye!”);
}
}
步骤三:连接Oracle数据库
使用以下代码连接到Oracle数据库:
```javaConnection conn = null;
Statement stmt = null;try {
Class.forName(JDBC_DRIVER); System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS); System.out.println("Creating statement...");
stmt = conn.createStatement();
步骤四:执行SQL查询
使用以下代码执行SQL查询:
“`java
String sql;
sql = “SELECT id, name, age FROM employees”;
ResultSet rs = stmt.executeQuery(sql);
步骤五:处理结果集
使用while循环遍历结果集,并将结果打印出来:
```javawhile(rs.next()){
int id = rs.getInt("id"); String name = rs.getString("name");
int age = rs.getInt("age"); System.out.print("ID: " + id);
System.out.print(", Name: " + name); System.out.println(", Age: " + age);
}
步骤六:关闭连接
使用以下代码关闭连接:
“`java
rs.close();
stmt.close();
conn.close();
完整代码示例:
```javaimport java.sql.*;
public class OracleJdbc {
static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver"; static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:xe";
static final String USER = "username"; static final String PASS = "password";
public static void mn(String[] args) { Connection conn = null;
Statement stmt = null; try {
Class.forName(JDBC_DRIVER); System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS); System.out.println("Creating statement...");
stmt = conn.createStatement(); String sql;
sql = "SELECT id, name, age FROM employees"; ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){ int id = rs.getInt("id");
String name = rs.getString("name"); int age = rs.getInt("age");
System.out.print("ID: " + id); System.out.print(", Name: " + name);
System.out.println(", Age: " + age); }
rs.close(); stmt.close();
conn.close(); } catch(SQLException se) {
se.printStackTrace(); } catch(Exception e) {
e.printStackTrace(); } finally {
try { if(stmt!=null)
stmt.close(); } catch(SQLException se2) {
} try { if(conn!=null)
conn.close(); } catch(SQLException se) {
se.printStackTrace(); }
} System.out.println("Goodbye!");
}}
这是一个非常基本的示例,您可以使用更复杂的SQL语句来查询Oracle数据库。此外,JDBC API还提供了预编译查询、批量操作等更高级的特性。