日期:2014-05-16  浏览次数:20650 次

MS SQL server JDBC 连接驱动
package op.db;

import java.sql.*;

public class ConnectionManager {

private static final String DRIVER_CLASS = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static final String DATABASE_URL="jdbc:sqlserver://localhost:1433;DatabaseName=BBMS";
//private static final String DATABASE_URL = "jdbc:sqlserver://192.168.4.220\\sql2005;DatabaseName=news";
private static final String DATABASE_USRE = "sa";
private static final String DATABASE_PASSWORD = "123456";

/**
* 返回连接
*
* @return Connection
*/
public static Connection getConnection() {
Connection dbConnection = null;
try {
Class.forName(DRIVER_CLASS);
dbConnection = DriverManager.getConnection(DATABASE_URL,
DATABASE_USRE, DATABASE_PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}

return dbConnection;
}

/**
* 关闭连接
*
* @param dbConnection
*            Connection
*/
public static void closeConnection(Connection dbConnection) {
try {
if (dbConnection != null && (!dbConnection.isClosed())) {
dbConnection.close();
}
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}

}

/**
* 关闭结果集
*/
public static void closeResultSet(ResultSet res) {
try {
if (res != null) {
res.close();
res = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* 关闭语句
*/

public static void closeStatement(PreparedStatement pStatement) {
try {
if (pStatement != null) {
pStatement.close();
pStatement = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}

}