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;
}
}
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;
}
}