package com.sec.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 数据库辅助类
*
* @author Administrator
*
*/
public class ConnDB {
public static String URL = "jdbc:sqlserver://localhost:1434;databaseName=ShoppingDB";// 连接字符串
public static String USER = "sa";// 登录名
public static String PWD = "123456";// 密码
// 类加载时,加载数据库驱动(这里加载一次后,就不需要再加载了)
static {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取连接
*
* @return
*/
public static Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, USER, PWD);
return conn;
} catch (SQLException e) {
// 写日志
e.printStackTrace();
}
return null;
}
/**
* 关闭相关资源
*
* @param conn
* @param stm
* @param pstm
* @param rs
*/
public static void closeAll(Connection conn,
PreparedStatement pstmt, ResultSet rs) {
try {
if (conn != null) {
conn.close();
}
if (pstmt != null) {
pstmt.close();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}