-
通过file文件调用JDBC的Driver url 等参数
1、先在包内新建一个File文件
创建后将参数写入文件中;
格式:参数名=参数(diver=oracle.jdbc.OracleDriver)
注:没有"";等其他符号,换行直接回车
2、通过反射获得需要的参数(先要创建标准的JavaBeans配置,创建好成员变量后可通过Source的功能自动创建)
import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;import java.util.Vector;public class JDBC { class OracleMethod{ private Properties pro = new Properties(); private String driver=""; private String url=""; private String user=""; private String pwd=""; public void proper(){ try { //**********通过反射获得需要的参数 pro.load(this.getClass().getResourceAsStream("url.properties")); this.setDriver(pro.getProperty("driver")); this.setUrl(pro.getProperty("url")); this.setUser(pro.getProperty("user")); this.setPwd(pro.getProperty("pwd")); //**********通过反射获得需要的参数 try { Class.forName(driver); Connection con; System.out.println("加载驱动成功"); con = DriverManager.getConnection(url, user, pwd); System.out.println("连接数据库成功"); Statement sta = con.createStatement(); String strsql = "select * from emp,(select deptno,min(sal) as sal from emp group by deptno) dss where dss.deptno = emp.deptno and dss.sal = emp.sal"; ResultSet rs =sta.executeQuery(strsql); while(rs.next()){ for(int i =1;i<=10;i++){ if(i<10){ System.out.print(rs.getString(i)+"\t"); }else{ System.out.print(rs.getString(i)+"\n"); } } } } catch (ClassNotFoundException e) { e.printStackTrace(); }catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } public void oracle(){ } public String getDriver() { return driver; } public void setDriver(String driver) { this.driver = driver; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } } public static void main(String[] args) { JDBC aa = new JDBC(); JDBC.OracleMethod a = aa.new OracleMethod(); a.proper(); } }