`
longlovexk
  • 浏览: 5234 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

DBHelp连接数据库

    博客分类:
  • jsp
阅读更多
private static final String DRIVER="com.microsoft.sqlservlet.jdbc.SQLServlet";
private static final String URL="jdbc:sqlservlet://localhost:3306;databasename=mstanford";
private static final String USER="sa";
private static final String PASSWORD="root";

//获取连接
public static Connection getConnection(){
  Connection con = null;             //声明一个连接对象
  try {
   Class.forName(DRIVER);
   //注册驱动
   con = DriverManager.getConnection(URL,USER,PASSWORD);  //获得连接对象
  } catch (ClassNotFoundException e) {         //捕获驱动类无法找到异常
   e.printStackTrace();         
  } catch (SQLException e) {            //捕获SQL异常
   e.printStackTrace();
  }
  return con;
}
public static void close(Connection con) {//关闭连接对象
  if(con != null) {    //如果conn连接对象不为空
   try {
    con.close();   //关闭conn连接对象对象
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
}
public static void close(PreparedStatement pstmt) {//关闭预处理对象
  if(pstmt != null) {    //如果pstmt预处理对象不为空
   try {
    pstmt.close();   //关闭pstmt预处理对象
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
}

public static void close(ResultSet rs) {//关闭结果集对象
  if(rs != null) {    //如果rs结果集对象不为null
   try {
    rs.close();    //关闭rs结果集对象
   } catch (SQLException e) {
    e.printStackTrace();
   }
   
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics