2个回答
2014-01-01
展开全部
package y2javaee.xmal1.common;import java.sql.*;public class DBConnection {
private static Connection conn; private static final String DRIVER_CLASS = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; private static final String DATABASE_URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=addressBook"; private static final String DATABASE_USRE = "sa"; private static final String DATABASE_PASSWORD = "120010"; /**
* 返回连接
*
* @return Connection
*/
public static Connection getConnection() { try {
Class.forName(DRIVER_CLASS);
conn = DriverManager.getConnection(DATABASE_URL,
DATABASE_USRE, DATABASE_PASSWORD); } catch (Exception e) {
e.printStackTrace();
} return conn;
} /**
* 关闭数据库连接
*
*/
public static void closeConnection(){
try{
if(conn!=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 关闭Statement对象
*
* @param stm
*/
public static void closeStatement(Statement stm){
try{
if(stm!=null)
stm.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 关闭PreparedStatement对象
*
* @param ps
*/
public static void closeStatement(PreparedStatement ps){
try{
if(ps!=null)
ps.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 关闭ResultSet结果集对象
*
* @param rs
*/
public static void closeResultSet(ResultSet rs){
try{
if(rs!=null)
rs.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
private static Connection conn; private static final String DRIVER_CLASS = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; private static final String DATABASE_URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=addressBook"; private static final String DATABASE_USRE = "sa"; private static final String DATABASE_PASSWORD = "120010"; /**
* 返回连接
*
* @return Connection
*/
public static Connection getConnection() { try {
Class.forName(DRIVER_CLASS);
conn = DriverManager.getConnection(DATABASE_URL,
DATABASE_USRE, DATABASE_PASSWORD); } catch (Exception e) {
e.printStackTrace();
} return conn;
} /**
* 关闭数据库连接
*
*/
public static void closeConnection(){
try{
if(conn!=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 关闭Statement对象
*
* @param stm
*/
public static void closeStatement(Statement stm){
try{
if(stm!=null)
stm.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 关闭PreparedStatement对象
*
* @param ps
*/
public static void closeStatement(PreparedStatement ps){
try{
if(ps!=null)
ps.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 关闭ResultSet结果集对象
*
* @param rs
*/
public static void closeResultSet(ResultSet rs){
try{
if(rs!=null)
rs.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
2014-01-01
展开全部
联系Q二七四二三三六七二..
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |