sqlserverDBUtil

public class DBUtil {
    protected static final Logger logger = LoggerFactory.getLogger(DBUtil.class);

    /**
     * 数据库连接
     *
     * @return
     */
    public static Connection getConn() {
        Connection conn = null;
        String url = "jdbc:sqlserver://localhost:1433;databaseName=MyBook";
        String user = "sqlserver";
        String pwd = "password";
        String className = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        try {
            Class.forName(className);
            conn = DriverManager.getConnection(url, user, pwd);
        } catch (ClassNotFoundException e) {
            logger.error("getConn method occur ClassNotFoundException error:" + e);
        } catch (SQLException e) {
            logger.error("getConn method occur SQLException error:" + e);
        }
        return conn;
    }

    /**
     * 数据库关闭
     *
     * @param conn
     * @param ps
     * @param rs
     */
    public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                logger.error("conn close occur SQLException error:" + e);
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                logger.error("ps close occur SQLException error:" + e);
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                logger.error("rs close occur SQLException error:" + e);
            }
        }
    }
}

 



    com.microsoft.sqlserver
    sqljdbc4
    4.0

你可能感兴趣的:(sqlserverDBUtil)