首页 > 代码库 > JDBC连接池
JDBC连接池
DBConnection.java
package com.test; import java.io.IOException; import java.sql.*; public class DBConnection { //private DBConnectionPoolManager dbc = null; public DBConnection(){ //dbc = DBConnectionPoolManager.getInstance(); } /*public Connection newConnection() throws SQLException{ return dbc.getConnection(); }*/ // private Connection newConnection() throws SQLException { // //Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); // conn = DriverManager.getConnection("proxool.originDB"); // if (conn == null) { // throw new SQLException("Connection failed !"); // }else{ // System.out.print("Connection Success !"); // } // return conn; // } public static Connection getMySQLConnection() { Connection conn = null; try { conn = DriverManager.getConnection("proxool.dm"); } catch (Exception e) { System.out.println("Connection failed ! " + e.getMessage()); } if (conn == null) { try { throw new SQLException("Connection failed !"); } catch (SQLException e) { e.printStackTrace(); } }else{ System.out.println("Connection Success !"); } return conn; } public static Connection getConnection() { Connection conn = null; try { conn = DriverManager.getConnection("proxool.mysql"); } catch (Exception e) { // e.printStackTrace(); System.out.println("Connection failed ! " + e.getMessage()); } if (conn == null) { try { throw new SQLException("Connection failed !"); } catch (SQLException e) { e.printStackTrace(); } }else{ System.out.print("Connection Success !"); } return conn; } public static Connection getNewConnection() { Connection conn = null; try { //Run as Java Application鐨勬椂鍊欓?氳繃杩欑鏂瑰紡寤虹珛杩炴帴 Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test","root","123456"); } catch (Exception e) { //System.out.println("Connection failed ! " + e.getMessage()); } if (conn == null) { try { throw new SQLException("Connection failed !"); } catch (SQLException e) { e.printStackTrace(); } }else{ // System.out.print("Connection Success !"); } return conn; } public static void close(ResultSet rs, Statement stmt, Connection conn) { if (rs != null) try { rs.close(); } catch (java.sql.SQLException ex) { ex.printStackTrace(); } if (stmt != null) try { stmt.close(); } catch (java.sql.SQLException ex) { ex.printStackTrace(); } if (conn != null) try { conn.close(); } catch (java.sql.SQLException ex) { ex.printStackTrace(); } } public static void close(ResultSet rs) { if(rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(Statement st) { if(st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(Connection conn) { if(conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(ResultSet rs, Statement st) { if(rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(Statement st, Connection conn) { if(st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) { DBConnection db = new DBConnection(); //DBconn.setConfigFile("waterDB.properties"); // System.out.println(db.getPath()); System.out.println(DBConnection.getNewConnection()); } }
proxool.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- the proxool configuration can be embedded within your own application‘s. Anything outside the "proxool" tag is ignored. --> <something-else-entirely> <proxool> <alias>mysql</alias> <driver-url>jdbc:mysql://127.0.0.1:3306/test</driver-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <driver-properties> <property name="user" value="http://www.mamicode.com/root" /> <property name="password" value="http://www.mamicode.com/123456" /> </driver-properties> <maximum-connection-count>1000</maximum-connection-count> <minimum-connection-count>2</minimum-connection-count> <house-keeping-sleep-time>90000</house-keeping-sleep-time> </proxool> </something-else-entirely>
web.xml
<servlet> <servlet-name>ServletConfigurator</servlet-name> <servlet-class> org.logicalcobwebs.proxool.configuration.ServletConfigurator </servlet-class> <init-param> <param-name>xmlFile</param-name> <param-value>WEB-INF/classes/proxool.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
JSP(懒得写Servlet测试了,直接在JSP中测试)
<%@page import="com.test.User"%> <%@page import="java.sql.*"%> <%@page import="com.test.DBConnection"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> Hello world! <% out.println("123"); User user; //DBConnection //DBConnection dbConnection = new DBConnection(); //User user = new User(); Connection connection = DBConnection.getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("select * from t_user"); while(resultSet.next()){ out.print("<br/>"); out.print(resultSet.getInt(1) + " "); out.print(resultSet.getString(2) + " "); out.print(resultSet.getString(3)); } DBConnection.close(resultSet); DBConnection.close(statement); DBConnection.close(connection); %> </body> </html>
运行结果perfect
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1537190
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。