docker(1) : 安裝oracle並用JDBC連接

參考 :

    https://segmentfault.com/a/1190000020633619?utm_source=tag-newest

    https://blog.csdn.net/qq_35035078/article/details/88855621

    https://www.cnblogs.com/OliverQin/p/9765808.html

    https://blog.csdn.net/qq_38380025/article/details/80647620

    https://www.baikedb.com/post-30.html

oracle 教程 : https://www.oraclejsq.com/article/010100139.html

安裝oracle

下載鏡像 

docker pull registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g

創建容器

docker run -d -p 1521:1521 -v /Users/leyili/oracle:/u01/app/oracle/ --name oracle  registry.aliyuncs.com/helowin/oracle_11g

注 : /Users/leyili/oracle 是本機路徑(不知道對不對)

 

啓動容器

docker start oracle

配置

進入容器

docker exec -it oracle bash

 

切換到root用戶

su root

 

輸入密碼

helowin

 

配置環境變量

vi /etc/profile

按G跳到最後一行,加入以下配置

export ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/dbhome_2

export ORACLE_SID=helowin

export PATH=$ORACLE_HOME/bin:$PATH

 

使配置文件生效

source /etc/profile

注 : 第一次以後進入容器使用 sqlplus 會找不到,重新運行該命令即可

 

建立軟連接

ln -s $ORACLE_HOME/bin/sqlplus /usr/bin

 

切換爲oracle用戶

su oracle

 

登錄sqlplus並修改sys、system用戶密碼

sqlplus /nolog
conn /as sysdba

 

修改system用戶的密碼爲 system

alter user system identified by system;

 

修改sys用戶的密碼爲 sys

alter user sys identified by sys;

 

創建test用戶,密碼爲test

create user test identified by test;

 

賦予權限

grant connect,resource,dba to test;

 

刷新表

ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;

 

查看一下oracle 的 lsnrctl 服務

lsnrctl status

 

查看SID(應該要oracle用戶下)

sqlplus / as sysdba

執行以下兩個SQL其中一個即可

select value from v$parameter where name='instance_name';
select instance_name from v$instance;

JDBC連接

public static void main(String[] args) {
        ResultSet rs = null;
        PreparedStatement stmt = null;
        Connection conn = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            // helowin爲SID
            String dbURL = "jdbc:oracle:thin:@localhost:1521:helowin";
            conn = DriverManager.getConnection(dbURL, "test", "test");
            String sql = "select count(*) from all_col_comments";
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while (rs.next()) {
                ResultSetMetaData rsmd = rs.getMetaData();
                int columnCount = rsmd.getColumnCount();
                for (int i = 1; i <= columnCount; i++) {
                    System.out.print(rs.getString(i) + " ");
                }
                System.out.println();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

 

 

 

END。

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