Java连接Oracle(基础篇)

 

JDBC 编程的基本步骤:

(1) 向系统注册所需的JDBC驱动程序(加载驱动程序)

(2) 建立到指定数据库德连接

(3) 提交数据库查询

(4) 取得查询结果

 

具体代码如下:

public class JDBCDemo {
    public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
	public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:demo";
	public static final String DBUSER = "scott";
	public static final String DBPASSWORD = "tiger";

    public static void main(String[] args) throws Exception {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select empno, ename from emp";

        Class.forName(DBDRIVER);
        conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();

        while (rs.next()) {
            int empno = rs.getInt(1);
            String ename = rs.getString(2);

            System.out.println(empno + "\t" + ename);
        }

        rs.close();
        ps.close();
        conn.close();
    }
}

你可能感兴趣的:(Java连接Oracle(基础篇))