JDBC连接池

wKiom1PjixLxfknCAAEbp-e9BqY598.jpg

DBConnection.java

packagecom.test;


importjava.io.IOException;
importjava.sql.*;

publicclassDBConnection{
	//privateDBConnectionPoolManagerdbc=null;

	publicDBConnection(){
		//dbc=DBConnectionPoolManager.getInstance();
	}

	
	
	
	/*publicConnectionnewConnection()throwsSQLException{
		
		returndbc.getConnection();		
		
	}*/
//	privateConnectionnewConnection()throwsSQLException{
//		//Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
//		conn=DriverManager.getConnection("proxool.originDB");
//	if(conn==null){
//		thrownewSQLException("Connectionfailed!");
//	}else{
//		System.out.print("ConnectionSuccess!");
//	}
//	returnconn;
//	}
	
	
	publicstaticConnectiongetMySQLConnection(){

		Connectionconn=null;
		try{
			conn=DriverManager.getConnection("proxool.dm");
		}catch(Exceptione){
			System.out.println("Connectionfailed!"+e.getMessage());
		}
		if(conn==null){
		try{
				thrownewSQLException("Connectionfailed!");
			}catch(SQLExceptione){
				e.printStackTrace();
			}
	}else{
		System.out.println("ConnectionSuccess!");
	}
		returnconn;
	}
	
	publicstaticConnectiongetConnection(){

		Connectionconn=null;
		try{
			conn=DriverManager.getConnection("proxool.mysql");
		}catch(Exceptione){
//			e.printStackTrace();
			System.out.println("Connectionfailed!"+e.getMessage());
		}
		if(conn==null){
		try{
				thrownewSQLException("Connectionfailed!");
			}catch(SQLExceptione){
				e.printStackTrace();
			}
	}else{
		System.out.print("ConnectionSuccess!");
	}
		returnconn;
	}
	publicstaticConnectiongetNewConnection(){

		Connectionconn=null;
		try{
			//RunasJavaApplication鐨勬椂鍊欓?氳繃杩欑鏂瑰紡寤虹珛杩炴帴
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test","root","123456");
		}catch(Exceptione){
			//System.out.println("Connectionfailed!"+e.getMessage());
		}
		if(conn==null){
		try{
				thrownewSQLException("Connectionfailed!");
			}catch(SQLExceptione){
				e.printStackTrace();
			}
	}else{
		//System.out.print("ConnectionSuccess!");
	}
		returnconn;
	}

	publicstaticvoidclose(ResultSetrs,Statementstmt,Connectionconn){
		if(rs!=null)
			try{
				rs.close();
			}catch(java.sql.SQLExceptionex){
				ex.printStackTrace();
			}
		if(stmt!=null)
			try{
				stmt.close();
			}catch(java.sql.SQLExceptionex){
				ex.printStackTrace();
			}
		if(conn!=null)
			try{
				conn.close();
			}catch(java.sql.SQLExceptionex){
				ex.printStackTrace();
			}
	}

	publicstaticvoidclose(ResultSetrs){
		if(rs!=null){
			try{
				rs.close();
			}catch(SQLExceptione){
				e.printStackTrace();
			}
		}
	}
	
	
	publicstaticvoidclose(Statementst){
		if(st!=null){
			try{
				st.close();
			}catch(SQLExceptione){
				e.printStackTrace();
			}
		}
	}
	
	
	publicstaticvoidclose(Connectionconn){
		if(conn!=null){
			try{
				conn.close();
			}catch(SQLExceptione){
				e.printStackTrace();
			}
		}
	}
	
	
	publicstaticvoidclose(ResultSetrs,Statementst){
		if(rs!=null){
			try{
				rs.close();
			}catch(SQLExceptione){
				e.printStackTrace();
			}
		}
		if(st!=null){
			try{
				st.close();
			}catch(SQLExceptione){
				e.printStackTrace();
			}
		}
	}
	
	
	publicstaticvoidclose(Statementst,Connectionconn){
		if(st!=null){
			try{
				st.close();
			}catch(SQLExceptione){
				e.printStackTrace();
			}
		}
		if(conn!=null){
			try{
				conn.close();
			}catch(SQLExceptione){
				e.printStackTrace();
			}
		}
	}
	
	
	
	
	publicstaticvoidmain(String[]args){
		DBConnectiondb=newDBConnection();
		//DBconn.setConfigFile("waterDB.properties");
//		System.out.println(db.getPath());
		System.out.println(DBConnection.getNewConnection());

	}
	
}

proxool.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<!--theproxoolconfigurationcanbeembeddedwithinyourownapplication's.
	Anythingoutsidethe"proxool"tagisignored.-->
	

<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>
			<propertyname="user"value="root"/>
			<propertyname="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中测试)

<%@pageimport="com.test.User"%>
<%@pageimport="java.sql.*"%>
<%@pageimport="com.test.DBConnection"%>
<%@pagelanguage="java"contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
<title></title>
</head>
<body>
Helloworld!
<%
out.println("123");
Useruser;
//DBConnection

//DBConnectiondbConnection=newDBConnection();
//Useruser=newUser();
Connectionconnection=DBConnection.getConnection();
Statementstatement=connection.createStatement();
ResultSetresultSet=statement.executeQuery("select*fromt_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

wKiom1Pji5mAJWIHAABVTGjBE64854.jpg

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1537190

你可能感兴趣的:(jdbc)