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();
}
}
}
}
java连接MSSQL数据库的方法
原创
©著作权归作者所有:来自51CTO博客作者口袋里的小龙的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Python实现连接mssql数据库操作
Python 想要和MSSql数据库进行交互,首先要下载名为"pymssql"的包,然后import该包Python实现连接mss
Python mssql sql sql数据库 数据库连接 -
Python实现的连接mssql数据库操作示例
这篇文章主要介绍了Python实现的连接mssql数据库操作,结合实例形式分析了Python安装pymssql模
Python mssql sql sql数据库 数据库