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
mysql jdbc:mysql://127.0.0.1:3306/test com.mysql.jdbc.Driver 1000 2 90000
web.xml
ServletConfigurator org.logicalcobwebs.proxool.configuration.ServletConfigurator xmlFile WEB-INF/classes/proxool.xml 1
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"%>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("
"); out.print(resultSet.getInt(1) + " "); out.print(resultSet.getString(2) + " "); out.print(resultSet.getString(3)); } DBConnection.close(resultSet); DBConnection.close(statement); DBConnection.close(connection); %>
运行结果perfect
推荐使用C3P0...
参考 http://shamrock.blog.51cto.com/2079212/1547187 中C3P0的使用部分