java 与 读取 .properties文件


sqlcon.properties配置文件 连接数据库
driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
url=jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pubs
uid=sa
pwd=

public class Dbcon {

private Properties p=null;
private Connection con=null;

public Dbcon(){
p=new Properties();
}

public Connection getcon(){
try {

p.load(this.getClass().getResourceAsStream("sqlcon.properties"));
Class.forName(p.getProperty("driver"));
this.con=DriverManager.getConnection(p.getProperty("url"),p.getProperty("uid"),p.getProperty("pwd"));
if(con!=null){
return con;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public void closecon(Connection con){
try {
con.close();
} catch (Exception e) {
e.printStackTrace();
}

}

你可能感兴趣的:(java,jdbc,Microsoft)