JDBC

来源:互联网 发布:王源同款衣服淘宝 编辑:程序博客网 时间:2024/04/30 22:17

 package com.hbsoft.MyServlet;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

 

public class Connt {
 private String driver;
 private String url;
 private String username;
 private String pwd;

 
 public Connt() {
  super();
  Properties p  = new Properties();
  try {
   p.load(this.getClass().getResourceAsStream("pz.properties"));
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  this.driver=p.getProperty("driver");
  this.url=p.getProperty("url");
  this.username=p.getProperty("username");
  this.pwd=p.getProperty("pwd");

 }


 public Connection getConnt(){
  Connection cont=null;
  try {
   Class.forName(driver);
    cont=DriverManager.getConnection(url,username,pwd);
  } catch (ClassNotFoundException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }
   catch (SQLException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }
  return cont;
  
  
 }
 
 

}

配置:

driver = com.microsoft.jdbc.sqlserver.SQLServerDriver
url = jdbc:microsoft:sqlserver://localhost:1433;databaseName=hbdb
username = sa
pwd = sa

原创粉丝点击