📄
字号:
范例PoolBean.Java源代码:
001 package com;
002 import Java.io.Serializable;
003 import Java.sql.*;
004 import Java.util.*;
005 import com.ConnBean;
006 public class PoolBean implements Java.io.Serializable
007 {
008 private String driver = null;
009 private String url = null;
010 private int size = 0;
011 private String username = new String("");
012 private String password = new String("");
013 private Vector pool = null;
014 private ConnBean connBean=null;
015 public PoolBean()
016 {
017 driver = "sun.jdbc.odbc.JdbcOdbcDriver";
018 url = "jdbc:odbc:student";
019 size = 5;
020 username = "";
021 password = "";
22 }
023 public void setDriver(String value){
024 if (value!=null) driver=value;
025 }
026 public String getDriver(){
027 return driver;
028 }
029 public void setURL(String value ){
030 if (value!=null) url=value;
031 }
032 public String getURL(){
033 return url;
034 }
035 public void setSize(int value){
036 if (value>1) size=value;
037 }
038 public int getSize(){
039 return size;
040 }
041 public void setUsername(String value){
042 if (value!=null) username=value;
043 }
044 public String getUserName(){
045 return username;
046 }
047 public void setPassword(String value){
048 if (value!=null) password=value;
049 }
050 public String getPassword(){
051 return password;
052 }
053 public void setConnBean(ConnBean cb) {
054 if (cb!=null) connBean=cb;
055 }
056 public ConnBean getConnBean() throws Exception{
057 Connection con = getConnection();
058 ConnBean cb = new ConnBean(con);
059 cb.setInuse(true);
060 return cb;
061 }
062 private Connection createConnection() throws Exception
063 {
064 Connection con = null;
065 con = DriverManager.getConnection(url,username,password);
066 return con;
067 }
068 public synchronized void initializePool() throws Exception
069 {
070 if (driver==null)
071 throw new Exception("No Driver Name Specified!");
072 if (url==null)
073 throw new Exception("No URL Specified!");
074 if (size<1)
075 throw new Exception("Pool size is less than 1!");
076 try{
077 Class.forName(driver);
078 for (int x=0; x<size; x++)
079 {
080 Connection con = createConnection();
081 if (con!=null)
082 {
083 ConnBean pcon = new ConnBean(con);
084 addConnection(pcon);
085 }
086 }
087 }
088 catch (Exception e)
089 {
090 System.err.println(e.getMessage());
091 throw new Exception(e.getMessage());
092 }
093 }
094 private void addConnection(ConnBean value)
095 {
096 if (pool==null) pool=new Vector(size);
097 pool.addElement(value);
098 }
099 public synchronized void releaseConnection(Connection con)
100 {
101 for (int x=0; x<pool.size(); x++)
102 {
103 ConnBean pcon =
104 (ConnBean)pool.elementAt(x);
105 if (pcon.getConnection()==con)
106 {
107 System.err.println("Releasing Connection " + x);
108 pcon.setInuse(false);
109 break;
110 }
111 }
112 }
113 public synchronized Connection getConnection()
114 throws Exception
115 {
116 ConnBean pcon = null;
117 for (int x=0; x<pool.size(); x++)
118 {
119 pcon = (ConnBean)pool.elementAt(x);
120 if (pcon.getInuse()==false)
121 {
122 pcon.setInuse(true);
123 return pcon.getConnection();
124 }
125 }
126 try
127 {
128 Connection con = createConnection();
129 pcon = new ConnBean(con);
130 pcon.setInuse(true);
131 pool.addElement(pcon);
132 }
133 catch (Exception e)
134 {
135 System.err.println(e.getMessage());
136 throw new Exception(e.getMessage());
137 }
138 return pcon.getConnection();
139 }
140 public synchronized void emptyPool()
141 {
142 for (int x=0; x<pool.size(); x++)
143 {
144 System.err.println("Closing JDBC Connection " + x);
145 ConnBean pcon =
146 (ConnBean)pool.elementAt(x);
147 if (pcon.getInuse()==false)
148 pcon.close();
149 else
150 {
151 try{
152 Java.lang.Thread.sleep(20000);
153 pcon.close();
154 }
155 catch (InterruptedException ie){
156 System.err.println(ie.getMessage());
157 }
158
159 }
160 }
161 public int getConnectionSize()
162 {
163 if (pool == null)
164 return 0;
165 else
166 return pool.size();
167 }
168 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -