当前位置:首页 > 数据库 > Oracle

java通过配置文件(Properties类)连接Oracle数据库代码示例

            
                  1
                import
                 java.io.FileInputStream;

                  2
                import
                 java.io.IOException;

                  3
                import
                 java.io.InputStream;

                  4
                import
                 java.sql.Connection;

                  5
                import
                 java.sql.DriverManager;

                  6
                import
                 java.sql.ResultSet;

                  7
                import
                 java.sql.SQLException;

                  8
                import
                 java.sql.Statement;

                  9
                import
                 java.util.Properties;

                 10
                 11
                public
                class
                 JdbcDemo {

                 12
                //
                创建对象关联配置文件
                 13
                private Properties pro = new Properties();
 14private String driver;
 15private String url;
 16private String user;
 17private String psw;
 18 19public JdbcDemo(){
 20try {
 21//通过反射获取数据流 22             pro.load(this.getClass().getResourceAsStream("MyOrcDb.properties"));
 23//获取配置文件数据 24this.setDriver(pro.getProperty("driver"));
 25this.setUrl(pro.getProperty("url"));
 26this.setUser(pro.getProperty("user"));
 27this.setPsw(pro.getProperty("psw"));
 28         } catch (IOException e) {
 29            e.printStackTrace();
 30        }
 31    }
 32 33privatevoid minSalName(){
 34 35//连接数据库 36try {
 37            Class.forName(driver);
 38//获取连接 39            Connection con;
 40             con = DriverManager.getConnection(url,user,psw);
 41//sql语句载入数据库 42             Statement st = con.createStatement();
 43//读取各部门最低工资名单
 44//关闭数据库 45             String sql = "SELECT emp.deptno,emp.ename,emp.sal from emp,"
 46                     + "(SELECT deptno,min(sal) minSal from emp GROUP BY deptno) a "
 47                     + "where emp.deptno = a.deptno "
 48                     + "and emp.sal = a.minsal";
 49//获得结果集 50             ResultSet rst = st.executeQuery(sql );
 51//获得结果集的列数 52int n = rst.getMetaData().getColumnCount();
 53//遍历结果集 54             String str = "";
 55for(;rst.next();){
 56for(int i=1;i<=n;i++){
 57                     str += rst.getString(i)+"t";
 58                }
 59                 str += "n";
 60            }
 61            System.out.println(str);
 62//关闭JDBC(先开后关) 63            rst.close();
 64            st.close();
 65            con.close();
 66         } catch (ClassNotFoundException e) {
 67            e.printStackTrace();
 68        }
 69catch (SQLException e) {
 70            e.printStackTrace();
 71        }
 72    }
 73public String getDriver() {
 74return driver;
 75    }
 76publicvoid setDriver(String driver) {
 77this.driver = driver;
 78    }
 79public String getUrl() {
 80return url;
 81    }
 82publicvoid setUrl(String url) {
 83this.url = url;
 84    }
 85public String getUser() {
 86return user;
 87    }
 88publicvoid setUser(String user) {
 89this.user = user;
 90    }
 91public String getPsw() {
 92return psw;
 93    }
 94publicvoid setPsw(String psw) {
 95this.psw = psw;
 96    }
 97publicstaticvoid main(String[] args) {
 98         JdbcDemo jdbc = new JdbcDemo();
 99        jdbc.minSalName();
100    }
101102 }
        

 

原文:http://www.cnblogs.com/-maji/p/7301139.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐