前端之家收集整理的这篇文章主要介绍了
Oracle 数据库的连接方式实现方法,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
/**
* Oracle 数据库的连接方式实现方法
*/
public void testJDBCCommon() {
// 建立数据库连接对象
Connection conn = null;
Statement st = null;
ResultSet rst = null;
Driver driver = null;
try {
//step 1: 注册驱动到jvm
driver = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(driver);
//step 2:获取数据库连接;
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:XE","hr","hr");
//step 3:创建Statement;
st = conn.createStatement();
//step 4:执行查询语句,获取结果集;
rst = st.executeQuery("select * from employees");
//step 5:处理结果集—输出结果集中保存的查询结果;
while (rst.next()) {
System.out.println(rst.getString(1) + " " + rst.getString(2)
+ " " + rst.getString("LAST_NAME"));
}
} catch (sqlException e) {
e.printStackTrace();
} finally {// 无论运行会不会进入到try。。catch代码块,最终都会运行finally代码块
// 关闭对象,防止多用户环境下挂起问题和锁定问题
try {
if (rst != null)
rst.close();
if (st != null)
st.close();
if (conn != null)
conn.close();
driver = null;
} catch (sqlException e) {
e.printStackTrace();
}
}
}
|
原文链接:https://www.f2er.com/oracle/208112.html