JDBC串連池

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.