標籤:import public
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/45/0C/wKiom1PjixLxfknCAAEbp-e9BqY598.jpg" title="QQ圖片20140807221904.jpg" alt="wKiom1PjixLxfknCAAEbp-e9BqY598.jpg" />
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
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/45/0C/wKiom1Pji5mAJWIHAABVTGjBE64854.jpg" title="QQ20140807222131.png" alt="wKiom1Pji5mAJWIHAABVTGjBE64854.jpg" />
本文出自 “阿凡達” 部落格,請務必保留此出處http://shamrock.blog.51cto.com/2079212/1537190