7、java jdbc如何連接oracle12


1、導入依賴

<dependency>
     <groupId>com.oracle.database.jdbc</groupId>
     <artifactId>ojdbc8</artifactId>
     <version>12.2.0.1</version>
</dependency>

2、創建jdbc連接實現查詢

public class Test01 {
    public static void main(String[] args) throws Exception {
        //加載數據庫驅動
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //得到Connection連接
        Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL",
                "c##boat", "c##boat");
        //得到預編譯的Statement對象
        PreparedStatement pstm = connection.prepareStatement("select * from student where id = ?");
        //給參數賦值
        pstm.setObject(1, 1);
        //執行數據庫查詢操作
        ResultSet rs = pstm.executeQuery();
        //輸出結果
        while(rs.next()){
            System.out.println(rs.getString("name"));
        }
        //釋放資源
        rs.close();
        pstm.close();
        connection.close();
    }
}

3、jdbc調用存儲函數

       //加載數據庫驅動
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //得到Connection連接
        Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL","c##boat", "c##boat");
//得到預編譯的Statement對象,調用存儲函數f_yearsal( ? ) PreparedStatement pstm = connection.prepareStatement("select f_yearsal( ? ) NAME from dual"); //給參數賦值 pstm.setObject(1, 2); //執行數據庫查詢操作 ResultSet rs = pstm.executeQuery(); //輸出結果 while(rs.next()){ System.out.println(rs.getString("NAME")); } //釋放資源 rs.close(); pstm.close(); connection.close();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM