import java.sql.*;
public class ConnectionManager {
//连接驱动
private static final String DRIVER_CLASS="com.microsoft.sqlserver.jdbc.SQLServerDriver";
//连接字符串
private static final String DATABADE_URL="jdbc:sqlserver://localhost:1433;DatabaseName=";
//用户名
private static final String DATABASE_USER="SA";
//用户密码
private static final String DATABASE_PASSWORD="";
//建立连接,返回连接对象
public static Connection getConnection(){
Connection dbConnection = null;
try {
Class.forName(DRIVER_CLASS);
dbConnection=DriverManager.getConnection(DATABADE_URL,DATABASE_USER,DATABASE_PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return dbConnection;
}
//关闭数据库连接
public static void closeConnection(Connection dbConnection){
try {
if(dbConnection != null && (!dbConnection.isClosed())){
dbConnection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//关闭结果集
public static void closeResultSet(ResultSet res){
try {
if(res != null){
res.close();
res = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//关闭Statement对象
public static void closeStatement(PreparedStatement pStatement){
try{
if(pStatement !=null){
pStatement.close();
pStatement = null;
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
public class ConnectionManager {
//连接驱动
private static final String DRIVER_CLASS="com.microsoft.sqlserver.jdbc.SQLServerDriver";
//连接字符串
private static final String DATABADE_URL="jdbc:sqlserver://localhost:1433;DatabaseName=";
//用户名
private static final String DATABASE_USER="SA";
//用户密码
private static final String DATABASE_PASSWORD="";
//建立连接,返回连接对象
public static Connection getConnection(){
Connection dbConnection = null;
try {
Class.forName(DRIVER_CLASS);
dbConnection=DriverManager.getConnection(DATABADE_URL,DATABASE_USER,DATABASE_PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return dbConnection;
}
//关闭数据库连接
public static void closeConnection(Connection dbConnection){
try {
if(dbConnection != null && (!dbConnection.isClosed())){
dbConnection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//关闭结果集
public static void closeResultSet(ResultSet res){
try {
if(res != null){
res.close();
res = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//关闭Statement对象
public static void closeStatement(PreparedStatement pStatement){
try{
if(pStatement !=null){
pStatement.close();
pStatement = null;
}
}catch (Exception e) {
e.printStackTrace();
}
}
}