以下内容由北大青鸟北京校区学术部提供:
package com.accp.util;
import java.sql.*;
public class ConnectionManager {
private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=数据库名称";
private static final String USER = "数据库用户名";
private static final String PASS = "密码";
/**
* 获取数据库连接对象
* @return Connection
*/
public static Connection getConnection(){
Connection connection = null;
try {
Class.forName(DRIVER);
connection = DriverManager.getConnection(URL, USER,PASS);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
/**
* 关闭数据库相关的对象(北大青鸟北京校区)
* @param conn 数据库连接对象
* @param stat 语句对象
* @param resultSet 结果集对象
*/
public static void closeAll(Connection conn,Statement stat,ResultSet resultSet) {
try {
if(resultSet!=null) resultSet.close();
if(stat!=null) stat.close();
if(conn!=null && !conn.isClosed()){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
北大青鸟北京校区