jdbc.properties,以及读取配置信息

public void getValue()
{
//System.out.println("..//"+System.getProperty("user.dir")+"//webapps");

try {
String path = this.getClass().getClassLoader().getResource("/").getPath();
String url = path.replaceAll("%20", " ");
FileInputStream fis = new FileInputStream(url+"config.properties");
Properties prop = System.getProperties();
try {
prop.load(fis);
//赋值
this.DRIVER = prop.getProperty("driver");
this.URL = prop.getProperty("url");
this.USER = prop.getProperty("user");
this.PASSWORD =prop.getProperty("password");

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


.jdbc.properties
driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
url=jdbc:microsoft:sqlserver://Ip地址:1433;databaseName=test
user=test
password=test

2.一个Servlet
import java.io.PrintStream;
import java.sql.*;
import java.util.ResourceBundle;

public class DBOperator
{

public DBOperator()
{
}

public static void main(String args[])
{
Connection con = getConnection();
System.out.println("connection success");
closeConnection(con);
System.out.println("close success");
}

public static Connection getConnection()
{
try
{
return DriverManager.getConnection(url, user,

password);
}
catch(SQLException e)
{
e.printStackTrace();
}
return null;
}

public static void closeConnection(Connection con)
{
try
{
if(con != null)
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}

private static String url;
private static String user;
private static String password;

static
{
ResourceBundle bundle = ResourceBundle.getBundle("路径");
try
{
Class.forName(bundle.getString("driver"));
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
url = bundle.getString("url");
user = bundle.getString("user");
password = bundle.getString("password");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值