JDBC 連接Oracle

首先要導入驅動包,可以網上下對應數據庫版本的驅動包,也可以使用自帶的,

我就使用自帶的,我的目錄:C:\Oracle11g\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar。

爲了代碼邏輯好看,都把異常拋出去,

連接:原始方法

public class BaseDao {
    //定義數據庫連接屬性
    private static String user = "scott";
    private static String password = "123456";
    private static String driver = "oracle.jdbc.OracleDriver";
    private static String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";

    //Oracle 數據庫的連接
    public static Connection getOracelConnection() throws ClassNotFoundException, SQLException {
        //利用反射機制加載驅動
        Class.forName(driver);
        Connection conn = null;
        conn = DriverManager.getConnection(url, user, password);
        return conn;
    }
}

讀取外部文件,獲取 user 、password、url (框架原理)

public class BaseDao1 {
    //定義數據庫連接屬性
    private static String user;
    private static String password;
    private static String driver;
    private static String url;

    //使用靜態代碼塊,完成連接參數的初始化
    static{
        Properties properties = new Properties();
        //利用反射原理機制加載屬性文件、database.properties
        InputStream inputStream = 
             BaseDao1.class.getResourceAsStream("database.properties");
        try {
            //加載物理文件
            properties.load(inputStream);
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //Oracle 數據庫的連接
    public static Connection getOracelConnection() throws ClassNotFoundException, SQLException {
        //利用反射機制加載驅動
        Class.forName(driver);
        Connection conn = null;
        conn = DriverManager.getConnection(url, user, password);
        return conn;
    }
}

配置文件 database.properties :

# oracle數據庫連接參數
user=scott
password=123456
driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl

兩種基本調用:使用存儲過程。

public class test2 {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Connection conn = (Connection) BaseDao.getOracelConnection();
        String sql = "call pro_emp(?,?,?,?)";
        CallableStatement callableStatement = conn.prepareCall(sql);
        //設置輸入參數的值
        callableStatement.setInt(1, 7788);
        //設置java屬性與oracle數據庫屬性類型的映射關係
        callableStatement.registerOutParameter(2, oracle.jdbc.OracleTypes.VARCHAR);
        callableStatement.registerOutParameter(3,oracle.jdbc.OracleTypes.DOUBLE);
        callableStatement.registerOutParameter(4,oracle.jdbc.OracleTypes.DATE);
        //執行
        callableStatement.execute();
        //取值
        String ename = callableStatement.getString(2);
        Double sal = callableStatement.getDouble(3);
        Date hiredate = callableStatement.getDate(4);
        //輸出
        System.out.println("名字:"+ename);
        System.out.println("工資:"+sal);
        System.out.println("入職日期:"+hiredate);
    }
}
public class test3 {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Connection conn = (Connection) BaseDao.getOracelConnection();
        //佔位符,(填充符)
        String sql = "call pro_deptno(?,?)";
        CallableStatement callableStatement = conn.prepareCall(sql);
        //設置輸入參數的值
        callableStatement.setInt(1, 30);
        //設置java屬性與oracle數據庫屬性類型的映射關係
        callableStatement.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);
        //執行
        callableStatement.execute();
        //取值
        ResultSet rs = (ResultSet) callableStatement.getObject(2);
        while (rs.next()) {
            System.out.println("empno = " + rs.getInt("empno"));
            System.out.println("ename = " + rs.getString("ename"));
            System.out.println("-----------------------------------");
        }
    }
}

表對應類:

public class Test_oracle {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Connection conn = null;
        conn = BaseDao.getOracelConnection();
        if (conn != null) {
            System.out.println("oracle數據庫連接成功");
        }else {
            System.out.println("oracle數據庫連接失敗");
        }

        PreparedStatement pre = conn.prepareStatement
                ("select * from emp where empno = ?");
        pre.setInt(1,7788);
        ResultSet rs = pre.executeQuery();

        emp emp = new emp();   //一個emp 類
        while (rs.next()) {
            emp.setEmpno(rs.getInt("empno"));
            emp.setEname(rs.getString("ename"));
            emp.setMgr(rs.getInt("mgr"));
            emp.setHiredate(rs.getString("hiredate"));
            emp.setSal(rs.getDouble("sal"));
            emp.setComm(rs.getDouble("comm"));
            emp.setDeptno(rs.getInt("deptno"));
        }
        System.out.println(emp);
    }
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章