• Oracle 11c下载 及连接到OracleDB的简单程序


    Oracle官网总是不太贴心。还是网友贴心。

    https://pan.baidu.com/s/1ZCFLUi4Ti_WUYOFR3gB2dA 是11g版本下载包,下载下来解压就能用了。

    安装完毕后,驱动包在【oralcehome】product11.2.0dbhome_1jdbclib下。

     访问Oracle的JDBC程序,建表请见 https://www.cnblogs.com/xiandedanteng/p/11691113.html

    package com.hy;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.text.MessageFormat;
    
    public class OracleConnector {
        
        // 连接到数据库的四大属性
        private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
        private static final String DBURL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
        private static final String USER = "system";
        private static final String PSWD = "Ufo12345678";
        
        public static void testConnToDB() {
            Connection conn = null;
            Statement stmt = null;
            
            try{
                Class.forName(DRIVER).newInstance();
                conn = DriverManager.getConnection(DBURL, USER, PSWD);
                stmt = conn.createStatement();
                
                String sql = "select id,name,age,createdtime from firsttb";
                ResultSet rs = stmt.executeQuery(sql);
    
                int index = 0;
                while (rs.next()) {
                    index++;
    
                    String id = rs.getString("id");
                    String name = rs.getString("name");
                    String age = rs.getString("age");
                    String cdate = rs.getString("createdtime");
    
                    String raw = "#{0},{1},{2},{3},{4}";
                    Object[] arr = { index, id, name, age, cdate };
                    String outStr = MessageFormat.format(raw, arr);
                    System.out.println(outStr);
                }
            } catch (Exception e) {
                System.out.print(e.getMessage());
            } finally {
                try {
                    stmt.close();
                    conn.close();
                } catch (SQLException e) {
                    System.out.print("Can't close stmt/conn because of " + e.getMessage());
                }
            }
        }
        
        /**
         * 执行点
         * @param args
         */
        public static void main(String[] args) {
            testConnToDB();
        }
    }

    输出:

    #1,1,ANDY,20,2019-11-09 09:19:10
    #2,2,Bill,30,2019-11-09 09:19:39
    #3,3,Cindy,40,2019-11-09 09:19:45
  • 相关阅读:
    Windows平板优化设置
    MAC OS UI设计
    使用bat/vbs/ahk对Windows下进行自动化操作
    C#在高性能计算领域为什么性能却如此不尽人意
    自定义多重搜索
    CF797E Array Queries
    标记永久化学习笔记
    P7200 [COCI2019-2020#1] Lutrija
    P1075 [NOIP2012 普及组] 质因数分解
    基础数论重学笔记
  • 原文地址:https://www.cnblogs.com/heyang78/p/11823157.html
Copyright © 2020-2023  润新知