首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

Jdbc 数据库连接池简易实现跟JdbcUtils

2014-01-22 
Jdbc 数据库连接池简易实现和JdbcUtilspublic class JdbcUtils {/*private static String url jdbc:mys

Jdbc 数据库连接池简易实现和JdbcUtils

public class JdbcUtils {/*private static String url = "jdbc:mysql://localhost:3306/riverevaluationsys";private static String user = "root";private static String password = "root";*/private static MyDataSource myDataSource ;// 装入虚拟机,立即执行static {try {Class.forName("com.mysql.jdbc.Driver");myDataSource = new MyDataSource();} catch (ClassNotFoundException e) {throw new ExceptionInInitializerError(e);}}/** *  * getConnection:<br /> * 获取数据库的连接 * * @author zhangzhaoyu * @return * @throws SQLException */public static Connection getConnection() throws SQLException {//return DriverManager.getConnection(url, user, password);return myDataSource.getConnection();}/** *  * close:<br /> * 关闭资源 * * @author zhangzhaoyu * @param conn * @param rs * @param st */public static void close(Connection conn, ResultSet rs, Statement st) { try {              if (rs != null)                  rs.close();          } catch (SQLException e) {              e.printStackTrace();          } finally {              try {                  if (st != null)                      st.close();              } catch (SQLException e) {                  e.printStackTrace();              } finally {              try {                          myDataSource.free(conn);                     } catch (Exception e) {                          e.printStackTrace();                      }             }          }  }/** *  * close:<br /> * 关闭数据库重载函数 * * @author zhangzhaoyu * @param conn * @param rs * @param pst */public static void close(Connection conn, ResultSet rs, PreparedStatement pst) { try {              if (rs != null)                  rs.close();          } catch (SQLException e) {              e.printStackTrace();          } finally {              try {                  if (pst != null)                  pst.close();              } catch (SQLException e) {                  e.printStackTrace();              } finally {                  if (conn != null)                      try {                          myDataSource.free(conn);                     } catch (Exception e) {                          e.printStackTrace();                      }              }          }  }/** *  * close:<br /> * 关闭资源 * * @author zhangzhaoyu * @param conn * @param pst */public static void close(Connection conn, PreparedStatement pst) {    try {          if (pst != null)          pst.close();      } catch (SQLException e) {          e.printStackTrace();      } finally {          if (conn != null)          try {                          myDataSource.free(conn);                     } catch (Exception e) {                          e.printStackTrace();                      }     }   }  }

热点排行