java中数据库通用层

/**  

* 数据库通用类  

* */

public class ConnDB {

 /**  

* 获取数据库连接对象  

* @return 数据库连接对象  

* */  

public static Connection getConn() {

Connection conn=null;
  try {
   //加载驱动
   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
   //获取数据库连接对象
   conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=StuInfo", "sa", "sasa");
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return conn;

}

 

/**
  * 关闭资源
  * @param pstm 预编译命令对象
  * @param rs 结果集
  * @param stm 普通命令对象
  * @param conn 数据库连接对象
  */
 public static void close(PreparedStatement pstm,ResultSet rs,Statement stm,Connection conn) {

try {
   if(rs!=null){
    rs.close();
   }
   if(pstm!=null){
    pstm.close();
   }
   if(stm!=null){
    stm.close();
   }
   if(conn!=null){
    conn.close();
   }
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }
 }

}

posted @ 2014-07-06 12:54  单方面  阅读(436)  评论(0编辑  收藏  举报