微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQLServer数据库连接一

属性文件db.properties

driver=com.microsoft.sqlserver.jdbc.sqlServerDriver
url=jdbc:sqlserver://localhost:1433;DatabaseName=addressBook
username=sa
password=accp

 

读取属性文件

public class ReadProperties extends Properties {

 // 静态的私有的静态变量
 private static ReadProperties readPro = null;

 // 私有构造函数
 private ReadProperties() {
  try {
   InputStream is = getClass().getResourceAsstream("/db.properties");
   load(is);
  } catch (IOException e) {
   e.printstacktrace();
  }
 }

 // 获取类实例方法(同步锁,只能创建一个实例)
 public static  ReadProperties getInstance() {
  if (readPro == null) {
   readPro=new ReadProperties();
   
  }
  return readPro;
 }
}

Servlet控制器

public void doPost(HttpServletRequest request,HttpServletResponse response)
   throws servletexception,IOException {
  try {
   // 获取数据完整路径
   String path = request.getServletPath();
   // 截取完整路径
   path = path.substring(1,path.indexOf("."));
   // 得到数据类型(类)
   String straction = pro.getProperty(path);
   // 加载数据(类)
   Class classaction = Class.forName(straction);
   // 实例化(类)对象
   IAction action = (IAction) classaction.newInstance();
   // 获取目的地
   String dest = action.execute(request,response);
   // 进行转发
   request.getRequestdispatcher(dest).forward(request,response);
  } catch (ClassNotFoundException e) {
   e.printstacktrace();
  } catch (InstantiationException e) {
   e.printstacktrace();
  } catch (illegalaccessexception e) {
   e.printstacktrace();
  }
 }

@Override public void init(ServletConfig config) throws servletexception {  InputStream is = this.getClass().getResourceAsstream(    config.getinitParameter("pro"));  try {   pro.load(is);  } catch (IOException e) {   e.printstacktrace();  } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐