java通過設定檔(Properties類)串連Oracle資料庫程式碼範例

來源:互聯網
上載者:User

標籤:use   串連數   java   ada   cat   count()   代碼   import   ack   

  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(); 14     private String driver; 15     private String url; 16     private String user; 17     private String psw; 18      19     public JdbcDemo(){ 20         try { 21             //通過反射擷取資料流 22             pro.load(this.getClass().getResourceAsStream("MyOrcDb.properties")); 23             //擷取設定檔資料 24             this.setDriver(pro.getProperty("driver")); 25             this.setUrl(pro.getProperty("url")); 26             this.setUser(pro.getProperty("user")); 27             this.setPsw(pro.getProperty("psw")); 28         } catch (IOException e) { 29             e.printStackTrace(); 30         } 31     } 32      33     private void minSalName(){ 34          35         //串連資料庫 36         try { 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             //獲得結果集的列數 52             int n = rst.getMetaData().getColumnCount(); 53             //遍曆結果集 54             String str = ""; 55             for(;rst.next();){ 56                 for(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         } 69         catch (SQLException e) { 70             e.printStackTrace(); 71         } 72     } 73         public String getDriver() { 74         return driver; 75     } 76     public void setDriver(String driver) { 77         this.driver = driver; 78     } 79     public String getUrl() { 80         return url; 81     } 82     public void setUrl(String url) { 83         this.url = url; 84     } 85     public String getUser() { 86         return user; 87     } 88     public void setUser(String user) { 89         this.user = user; 90     } 91     public String getPsw() { 92         return psw; 93     } 94     public void setPsw(String psw) { 95         this.psw = psw; 96     } 97     public static void main(String[] args) { 98         JdbcDemo jdbc = new JdbcDemo(); 99         jdbc.minSalName();100     }101 102 }

 

java通過設定檔(Properties類)串連Oracle資料庫程式碼範例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.