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="root" /> <property name="password" value="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