JDBC 连接 Oracle 数据库

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * JDBC 连接 Oracle 数据库
 * 
 * @author zhanqi
 * 
 */
public class ORACLE {

    private static String driver = "oracle.jdbc.driver.OracleDriver";
    private static String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";

    public static void main(String[] args) {
        Connection ct = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // 加载驱动类
            Class.forName(driver);
            // 得到连接
            ct = DriverManager.getConnection(url, "scott", "scott");
            // 得到预编译的 SQL 语句的对象
            ps = ct.prepareStatement("select count(*) from emp");
            // 得到结果集
            rs = ps.executeQuery();
            // 循环取出记录
            while (rs.next()) {
                System.out.println(rs.getInt(1));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (ct != null) {
                    ct.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

  • Oracle JDBC driver:下载
  • Oracle JDBC driver:Oracle安装目录:F:\app\zhanqi\product\11.2.0\dbhome_1\jdbc\lib

posted on 2011-01-06 07:25  zhanqi  阅读(491)  评论(0编辑  收藏  举报