标签:
package com.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class DBConnection { // 连接Oracle数据库 public void OracleConnection() { Connection con = null; PreparedStatement pre = null; ResultSet rs = null; try { // 1. 加载Oracle驱动程序 Class.forName("oracle.jdbc.driver.OracleDriver"); // 2. 设置Oracle数据库基本信息 String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"; String user = "scott"; String password = "goodluck"; // 2. 获取连接 con = DriverManager.getConnection(url, user, password); System.out.println("----> Connection Success!"); // 3. 执行SQL语句 String sql = "SELECT * FROM EMP"; pre = con.prepareStatement(sql); // 4. 获取结果集 rs = pre.executeQuery(); while (rs.next()) { System.out.println("编号:" + rs.getString("empno") + ";姓名:" + rs.getString("ename") + "; 工作:" + rs.getString("job") + "; 领导:" + rs.getString("mgr") + "; 雇佣日期:" + rs.getString("hiredate") + "; 工资:" + rs.getString("sal") + "; 奖金:" + rs.getString("comm") + "; 部门:" + rs.getString("deptno")); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pre != null) pre.close(); if (con != null) con.close(); System.out.println("----> Connection End <-----"); } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) { DBConnection db = new DBConnection(); db.OracleConnection(); } }
标签:
原文地址:http://my.oschina.net/u/2259631/blog/506063