java通过配置⽂件(Properties类)连接Oracle数据库代码⽰例 1import java.io.FileInputStream;
2import java.io.IOException;
3import java.io.InputStream;
4import java.sql.Connection;
5import java.sql.DriverManager;
6import java.sql.ResultSet;
7import java.sql.SQLException;
8import java.sql.Statement;
9import java.util.Properties;
10
11public class JdbcDemo {
12//创建对象关联配置⽂件
13private Properties pro = new Properties();
14private String driver;
15private String url;
16private String user;
17private String psw;
18
19public JdbcDemo(){
20try {
21//通过反射获取数据流
22 pro.Class().getResourceAsStream("MyOrcDb.properties"));
23//获取配置⽂件数据
24this.Property("driver"));
25this.Property("url"));
26this.Property("user"));
27this.Property("psw"));
28 } catch (IOException e) {
29 e.printStackTrace();
30 }
31 }
32
33private void minSalName(){
34
35//连接数据库
36try {
37 Class.forName(driver);
38//获取连接
39 Connection con;
40 con = Connection(url,user,psw);
41//sql语句载⼊数据库
42 Statement st = ateStatement();
43//读取各部门最低⼯资名单
44//关闭数据库
45 String sql = "SELECT emp.ame,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 = MetaData().getColumnCount();
53//遍历结果集
54 String str = "";
55for(;();){
56for(int i=1;i<=n;i++){
57 str += String(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() {
resultset 遍历74return driver;
75 }
76public void setDriver(String driver) {
77this.driver = driver;
78 }
79public String getUrl() {
80return url;
81 }
82public void setUrl(String url) {
83this.url = url;
84 }
85public String getUser() {
86return user;
87 }
88public void setUser(String user) {
89this.user = user;
90 }
91public String getPsw() {
92return psw;
93 }
94public void setPsw(String psw) {
95this.psw = psw;
96 }
97public static void main(String[] args) {
98 JdbcDemo jdbc = new JdbcDemo();
99 jdbc.minSalName();
100 }
101
102 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论