利用配置文件连接数据库

Driver=oracle.jdbc.OracleDriver
Con=jdbc:oracle:thin:@175.3.15.9:1521:orcl
User=scott
Pwd=tiger             //properties配置文件


package Java;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class Shuju {
	public static void Select(String sql) throws Exception {
		Properties a = new Properties();
	    a.load(new FileInputStream("src/Java/properties"));        //配置文件地址
	    String driver =a.getProperty("Driver");
	    String con =a.getProperty("Con");
	    String user =a.getProperty("User");
	    String pwd =a.getProperty("Pwd");                   //获取配置文件内容
	    Class.forName(driver);	    
	    Connection con1;
		con1 = DriverManager.getConnection(con, user, pwd);			
		Statement sta=con1.createStatement();
		ResultSet rs = sta.executeQuery(sql);
		while(rs.next()){
			String str1=rs.getString(1);
			String str2=rs.getString(2);
			String str3=rs.getString(3);
			System.out.println("ename: "+str1+" deptno: "+ str2+" sal: "+str3 );
		}	
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Shuju a = new Shuju();
		try {
			a.Select("select ename,a.deptno,sal from emp,(select deptno,min(sal) as minsal from emp GROUP BY deptno)a where emp.deptno = a.deptno and emp.sal = a.minsal");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}

  

posted @ 2017-08-08 09:05  王。雄  阅读(427)  评论(0编辑  收藏  举报