DAO

   dbconn.properties文件存放连接数据库的配置参数,内容如下:
      dbconn.properties
      #SQL Server
      driverName=com.microsoft.jdbc.sqlserver.SQLServerDriver  connString=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testDatabase
      userName=sa
      password=6599996
      DAO.connDB.readPropertie类用来读取.properties文件中的配置,其源代码如下:
      readProperties.java
      package DAO.connDB;
      import java.io.InputStream;
      import java.util.Properties;
      //读.properties文件属性类
      public class readProperties {
      String propertyFileName=null;//.properties文件名称
      public String getPropertyFileName() {
      return propertyFileName;
      }
      public void setPropertyFileName(String propertyFileName) {
      this.propertyFileName = propertyFileName;
      }
      public String getPropertyFromFile(String refName)
      {//从.properties文件中得到refName属性的值
      if(this.getPropertyFileName()==null) return null;
      else{
      try{
      InputStream fin = getClass().getResourceAsStream(this. get Property FileName());
      Properties props = new Properties();
      props.load(fin);
      return (String)props.getProperty(refName);
      }catch(Exception e){
      return null;
      }
      }
      }
      } 


      DAO.connDB.conndb封装了对数据库的连接,通过DAO.connDB.readProperties类读取连接参数,其源代码如下:
      conndb.java
      package DAO.connDB;
      import java.sql.Connection;
      import java.sql.DriverManager;
      //生成数据库连接对象类
      public class connDB {
      String driverName=null;//数据库驱动名
      String connString=null;//连接字符串
      String userName=null;//用户名
      String password=null;//密码
      public connDB() {//在构造函数中初始化数据库连接参数
      readProperties readDBProperties=new readProperties();
      readDBProperties.setPropertyFileName("/dbconn.properties");
      driverName=readDBProperties.getPropertyFromFile("driverName");
      connString=readDBProperties.getPropertyFromFile("connString");
      userName=readDBProperties.getPropertyFromFile("userName");
      password=readDBProperties.getPropertyFromFile("password");
      }
      public Connection getDBConn()
      {//得到数据库连接对象
      if(driverName==null||connString==null||userName==null) return null;
      else{ 
      try{
      Connection connDBObject=null;
      Class.forName(driverName);
      return   DriverManager.getConnection(connString, userName, password);
      }catch(Exception e){
      return null;
      } 
      }
      }
      public String getConnString() {
      return connString;
      }
      public String getDriverName() {
      return driverName;
      }
      public String getPassword() {
      return password;
      }
      public String getUserName() {
      return userName;
      }
      }
      DAO.table.userRecord类封装了数据库中的userTable表记录,源代码如下:
      UserRecord.java
      package DAO.table;
      import java.sql.Date;
      //封闭数据库userTable表记录的类
      public class userRecord {
      long userId;//用户ID
      String userName;//用户名
      String userPassword;//用户密码
      String userTrueName;//用户真实姓名
      int userAge;//用户年龄
      String userSex;//用户性别
      String userAddress;//用户地址
      String userTelephone;//用户电话号码
      Date addTime; //增加用户的时间
      public long getUserId() {
      return userId;
      }
      public void setUserId(long userId) {
      this.userId = userId;
      }
      public String getUserName() {
      return userName;
      }
      public void setUserName(String userName) {
      this.userName = userName;
      }
      public String getUserPassword() {
      return userPassword;
      }
      public void setUserPassword(String userPassword) {
      this.userPassword = userPassword;
      }
      public String getUserSex() {
      return userSex;
      }
      public void setUserSex(String userSex) {
      this.userSex = userSex;
      }
      public String getUserTelephone() {
      return userTelephone;
      }
      public void setUserTelephone(String userTelephone) {
      this.userTelephone = userTelephone;
      }
      public String getUserTrueName() {
      return userTrueName;
      }
      public void setUserTrueName(String userTrueName) {
      this.userTrueName = userTrueName;
      }
      public Date getAddTime() {
      return addTime;
      }
      public void setAddTime(Date addTime) {
      this.addTime = addTime;
      }
      public String getUserAddress() {
      return userAddress;
      }
      public void setUserAddress(String userAddress) {
      this.userAddress = userAddress;
      }
      public int getUserAge() {
      return userAge;
      }
      public void setUserAge(int userAge) {
      this.userAge = userAge;
      }
      }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值