class="java" name="code"> /** * 建链接池 */ public class DB_Pools { private final static String user = "数据库账号"; private final static String password = "数据库密码"; private final static String url = "jdbc:mysql://localhost:3306/数据库名"; private final static String driverClass = "com.mysql.jdbc.Driver"; private static LinkedList<Connection> pools = new LinkedList<Connection>(); private static Connection con = null; /** * 注册驱动 */ static { try { System.out.println("---Load static---"); Class.forName(driverClass); createPools(5, pools); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 获得Pools * * @return Pools */ public static LinkedList<Connection> getPools() { return pools; } /** * 显示Pools * * @param pools */ public static void setPools(LinkedList<Connection> pools) { DB_Pools.pools = pools; } /** * 创建数据库一个连接 * * @return con */ public static Connection poolsConnection() { try { con = DriverManager.getConnection(url, user, password); } catch (SQLException e) { e.printStackTrace(); } return con; } /** * 创建数据库number个连接,把连接存放在LinkedList<Connection>数组中的一个方法 * * @param number * 数 * @param linkedList * 链表 */ public static void createPools(int number, LinkedList<Connection> linkedList) { for (int i = 0; i < number; i++) { con = poolsConnection(); linkedList.addLast(con); } } /** * 从连接池中取出最前面的数据库连接 * * @return con */ public static Connection getSingleConnection() { con = pools.removeFirst(); return con; } /** * 把使用结束后的数据库连接放回连接池的末尾 * * @param con */ public static void freeConnection(Connection con) { pools.addLast(con); } }
/** * 关闭资源 */ public class DB extends DB_Pools { private static Connection con = null; /** * 获取连接 * * @return con */ public static Connection getConnection() { con = getSingleConnection(); return con; } /** * 关闭资源 * @param con * @param pst * @param rst */ public static void close(Connection con, Statement pst, ResultSet rst) { try { if (rst != null) { rst.close(); rst = null; } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (pst != null) { pst.close(); pst = null; } } catch (SQLException e) { e.printStackTrace(); } finally { if (con != null) { freeConnection(con); } } } } }
/** * 继承该Class调用Methods */ public class DB_Utils extends DB { private static Connection con = null; private static PreparedStatement pst = null; private static ResultSet rst = null; /** * 数据库的操作,添加 修改 删除 * * @param sql * 插入SQL语句 * @param objects * 插入对象 * @return b (true / false) */ public static boolean Operation_Create_Update_Delete(String sql, Object[] objects) { boolean b = false; con = getConnection(); try { pst = con.prepareStatement(sql); if (objects != null) { for (int i = 0; i < objects.length; i++) { pst.setObject(i + 1, objects[i]); } } int j = pst.executeUpdate(); if (j > 0) { b = true; } } catch (SQLException e) { e.printStackTrace(); } return b; } /** * 数据的插入 * * @param sql * 插入SQL语句 * @param objects * 插入对象 * @return id 返回Id */ public static int Operation_Create(String sql, Object[] objects) { int id = 0; con = getConnection(); try { pst = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); if (objects != null) { for (int i = 0; i < objects.length; i++) { pst.setObject(i, objects[i]); } } int j = pst.executeUpdate(); if (j > 0) { rst = pst.getGeneratedKeys(); if (rst.next()) { id = rst.getInt(1); } } } catch (SQLException e) { e.printStackTrace(); } finally { close(con, pst, rst); } return id; } /** * 查询需要的信息获得需要的信息 * * @param sql * 插入SQL语句 * @param objects * 插入对象 * @return rst 结果 */ public static ResultSet Operation_Search(String sql, Object[] objects) { con = getConnection(); try { pst = con.prepareStatement(sql); if (objects != null) { for (int i = 0; i < objects.length; i++) { pst.setObject(i + 1, objects[i]); } } rst = pst.executeQuery(); } catch (SQLException e) { e.printStackTrace(); } return rst; } public static void close(ResultSet rst) { close(con, pst, rst); } }