package com.dragon.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* 数据库访问层
* @author Administrator
*
*/
public class BaseDao {

/**
* 驱动连接字符串
*/
private static String className="com.microsoft.sqlserver.jdbc.SQLServerDriver";
/**
* 连接地址
*/
private static String url="jdbc:sqlserver://localhost:1433;DataBase=bank";
/**
* 用户名
*/
private static String user = "sa";
/**
* 密码
*/
private static String password = "";
/**
* 创建连接对象
*/
protected Connection connection = null;
/**
* 创建执行sql语句命令对象
*/
protected PreparedStatement preparedStatement = null;
/**
* 创建结果集对象
*/
protected ResultSet resultSet = null;
/**
* 打开数据库连接对象的方法
* @return 连接对象
*/
protected Connection openConnection(){

//加载驱动
try {
Class.forName(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//获得连接对象
try {
this.connection = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this.connection;
}
/**
* 关闭连接的方法
*/
protected void closeConnection(){
//关闭结果集
if(this.resultSet != null){
try {
this.resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
//关闭执行sql语句对象
if(this.preparedStatement != null){

try {
this.preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//关闭连接
if(this.connection != null){
try {
this.connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}