北大青鳥北京,北大青鳥學(xué)校學(xué)術(shù)部:Java連接SQLServer2008數(shù)據(jù)庫(kù)的簡(jiǎn)單工具類

以下內(nèi)容由北大青鳥北京校區(qū)學(xué)術(shù)部提供:

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=數(shù)據(jù)庫(kù)名稱";
 private static final String USER = "數(shù)據(jù)庫(kù)用戶名";
 private static final String PASS = "密碼";
 
 /**
  * 獲取數(shù)據(jù)庫(kù)連接對(duì)象
  * @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;
 }
 /**
  * 關(guān)閉數(shù)據(jù)庫(kù)相關(guān)的對(duì)象(北大青鳥北京校區(qū))
  * @param conn 數(shù)據(jù)庫(kù)連接對(duì)象
  * @param stat 語(yǔ)句對(duì)象
  * @param resultSet 結(jié)果集對(duì)象
  */
 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();
  }
 }
}
北大青鳥北京校區(qū)

北大青鳥網(wǎng)上報(bào)名
北大青鳥招生簡(jiǎn)章