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();
}
}
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进行SQL Server数据库连接的示例代码。通过加载sqlcon.properties配置文件中的参数,如驱动、URL、用户名及密码,实现了数据库连接功能。
226

被折叠的 条评论
为什么被折叠?



