About connecting pool

xiaoxiao2021-03-06  86

To the current, JDBC2's linked pool is just an interface. It has no real implementation. JDBC3 is under development. It has reported that the cell has been supported, but ........

JDBC3 uses JNDI technology, and the connection of the tank can make a master trouble.

At present, the connection pool that is already implemented by the third party is of course Poolman, 1.0 version is enough for general users. The configuration is simple, although the 2.0 version adds some features, but the configuration is JNDI, friends who don't understand the RMI and EJB It may be annoying. It is recommended to use 1.0.

If you are interested, you can also achieve a tank. The most critical technology is to pass the link as a parameter to a bean, and then return this parameter connection instead of close.

Here is a simple implementation:

The list of DBConnectionManager.java proceedings is as follows:

????

???? 001? Import? Java.io. *;

???? 002? Import? Java.sql. *;

???? 003? Import? Java.util. *;

???? 004? Import? Java.util.date;

???? 005

???? 006? / **

???? 007? *? Management class DBConnectionManager supports a database connection to one or more properties files

????? 008? *? Access to the pool. The client can call the GetInstance () method to access the unique instances of this class.

???? 009? * /

???? 010? Public? Class? DBConnectionManager? {

???? 011? Static? Private? DbconnectionManager? Instance;? //? Unique instance

???? 012? Static? Private? Int? Clients;

???? 013

???? 014? Private? Vector? Drivers? =? New? Vector ();

???? 015? Private ?printwriter? Log;

???? 016? Private? Hashtable? Pools? =? New? Hashtable ();

???? 017

???? 018? / **

???? 019? *? Returns the only instance. If this method is called for the first time, an instance is created.

???? 020? *

???? 021? *? @ RETURN? DBCONNECTIONMANAGER? Unique instance

???? 022? * /

???? 023? Static? Synchronized? Public? DbconnectionManager? GetInstance ()? {

???? 024? I? (Instance? ==? Null)? {

???? 025? Instance? =? New? DbconnectionManager ();

???? 026?}

???? 027? Clients ;

???? 028? RETURN? Instance;

???? 029?}

???? 030

???? 031? / **

???? 032? *? Constructing a function private to prevent other objects from creating this type of instance

???? 033? * /

???? 034? Private? DbconnectionManager ()? {

???? 035 ?init ();

???? 036?}

???? 037

???? 038? / **

???? 039? *? Return the connection object to the connection pool specified by the name

???? 040? *

???? 041? *? @ Param? Name? The connection pool name defined in the properties file

???? 042? *? @ Param? Con? Connection object // r

???? 043? * /

???? 044? Public? Void? Freeconnection (String? Name,? Connection? Con)? {

???? 045? Dbconnectionpool? Pool? =? (Dbconnectionpool)? Pools.get (name);

???? 046? If? (Pool?! =? Null)? {

???? 047? Pool.freeConnection (con);

???? 048?}

???? 049?}

???? 050

???? 051? / **

???? 052? *? Get a available (idle) connection. If there is no connection available, and the number of connections is less than the maximum connection

???? 053? *? Restriction, create and return new connection

???? 054? *

???? 055? *? @ Param? Name? The connection pool name defined in the properties file

???? 056? *? @ RETURN? Connection? Available connection or NULL

???? 057? * /

???? 058? Public? Connection? GetConnection (String? Name)? {

???? 059? Dbconnectionpool? Pool? =? (Dbconnectionpool)? Pools.get (name);

???? 060? If? (Pool?! =? NULL)? {

???? 061? Return? Pool.getConnection ();

???? 062?}

???? 063? Return? Null;

???? 064?}

???? 065

???? 066? / **

???? 067? *? Get a available connection. If there is no connection available, and the existing connection is less than the maximum number of connections,

???? 068? *? Create and return new connections. Otherwise, wait for other thread release connections within the specified time.

???? 069? *

???? 070? *? @ Param? Name? Connect pool name

???? 071? *? @ Param? Time? Waiting time // r in milliseconds

???? 072? *? @ RETURN? Connection? Available connection or NULL

???? 073? * /

???? 074? Public? Connection? GetConnection (String? Name,? Long? Time)? {

???? 075? Dbconnectionpool? Pool? =? (Dbconnectionpool)? Pools.get (name);

???? 076? If? (Pool?! =? Null? {

???? 077? Return? Pool.getConnection (TIME);

???? 078?}

???? 079? Return? Null;

???? 080?}

???? 081

???? 082? / **

???? 083? *? Turn off all connections, revoke the registration of the driver // r

???? 084? * /

???? 085? Public? Synchronized? Void? Release ()? {

???? 086? //? Waiting until the last customer program call

???? 087? I? (- clients?! =? 0)? {

???? 088? Return;

???? 089?}

???? 090

???? 091? Enumeration? Allpools? =? Pools.efficient ();

???? 092? While? (Allpools.hasmorelements ())? {

???? 093? Dbconnectionpool? Pool? =? (Dbconnectionpool)? Allpools.nexTelement ();

???? 094? Pool.release ();

???? 095?}

???? 096? Enumeration? Alldrivers? =? Drivers.efficient ();

???? 097? While? (Alldrivers.hasmorelements ())? {

???? 098? Driver? Driver? =? (Driver)? Alldrivers.nextelement ();

???? 099? Try? {

????? 100? Drivermanager.deregisterDriver (DRIVER);

???? 101? Log ("revoke JDBC drivers?"? Driver.getClass (). GetName () "Registration ///");

???? 102?}

???? 103? Catch? (SQLEXCEPTION? E)? {???? 104? Log (e ,? "Unable to revoke the following JDBC driver registration:?"? Driver.getClass (). Getname () );

???? 105?}

???? 106?}

???? 107?}

???? 108

???? 109? / **

???? 110? *? Create a connection pool instance based on the specified attribute.

???? 111? *

???? 112? *? @ Param? PROPS? Connection pool properties

???? 113? * /

???? 114? Private? Void? Createpools (Properties? PROPS)? {

???? 115? Enumeration? PROPNAMES? =? PROPS.PROPERTYNAMES ();

???? 116? While? (PropNames.hasMoreElements ())? {

???? 117? String? Name? =? (STRING)? PROPNAMES.NEXTELEMENT ();

???? 118? If? (Name.endswith (". URL"))? {

???? 119? String? Poolname? =? Name.substring (0,? Name.lastindexof ("."));

???? 120? String? Url? =? PrOPs.getProperty (poolname? ? "; URL");

???? 121? If? (URL? ==? Null)? {

???? 122? Log ("No Pool"? ? Poolname? ? "Specify URL");

???? 123? Contractue;

???? 124?}

???? 125? String? User? =? PROPS.GETPROPERTY (POOLNAME? ? ";");

???? 126? String? Password? =? PrOPs.getProperty (POOLNAME? ? ". Password");

???? 127? String? Maxconn? =? PrOPs.getProperty (poolname? ? ". MaxConn," 0 ");

???? 128? Int? Max;

???? 129? Try? {

???? 130? Max? =? Integer.Valueof (MaxConn) .intValue ();

???? 131?}

???? 132? Catch? (NUMBERFORMATEXCEPTION? E)? {

???? 133? Log ("The maximum number of connections for the error:?"? ? Maxconn? ? "?. Connect pool:?" ? Poolname);

???? 134? Max? =? 0;

???? 135?}

???? 136? Dbconnectionpool? Pool? =

???? 137? New? Dbconnectionpool (poolname, url,? User,? Password,? Max);

???? 138? Pools.put (poolname,? Pool;

???? 139? Log ("successfully created the connection pool"? ? Poolname);

???? 140?}

???? 141?}

???? 142?}

???? 143

???? 144? / **

???? 145? *? Read attribute completes initialization

???? 146? * /

???? 147? Private? Void? Init ()? {

???? 148? InputStream? Is? =? Getclass (). GetResourceAsStream ("/ db.properties");

???? 149? Properties? Dbprops? =? New? Proties (); ???? 150? Try? {

???? 151? Dbprops.load (IS);

???? 152?}

????? 153? Catch? (Exception? E)? {

???? 154? System.err.println ("You can't read the property file.?"?

???? 155? "Please make sure DB.Properties is in the path specified by the classpath");

???? 156? Return;

???? 157?}

???? 158? String? Logfile? =? Dbprops.getProperty ("logfile", "dbconnectionmanager.log");

???? 159? Try? {

???? 160? Log? =? New? PrintWriter (new? FileWriter (logfile,? True) ,? true);

???? 161?}

???? 162? Catch? (IoException? E)? {

???? 163? System.err.println ("Unable to open the log file:?" ? Logfile;

???? 164? Log? =? New? Printwriter (system.err);

???? 165?}

????? 166? Loadingdrivers (dbprops);

???? 167? Createpools (dbprops);

???? 168?}

???? 169

???? 170? / **

???? 171? *? Loading and register all JDBC drivers // r

???? 172? *

???? 173? *? @ Param? PROPS? Property

???? 174? * /

???? 175? Private? Void? LoadDrivers (Properties? PROPS)? {

???? 176? String? Driverclasses? =? PrOPs.getProperty ("drivers");

???? 177? StringTokenizer? ST? =? New? StringTokenizer (driverclasses);

???? 178? While? (St.hasmorelements ())? {

???? 179? String? Driverclassname? =? St.nextToken (). Trim ();

???? 180? Try? {

???? 181? Driver? Driver? =? (Driver)

???? 182? Class.Forname (driverclassname) .newinstance ();

???? 183? Drivermanager.RegisterDriver; DRIVER;

???? 184? Drivers.addeelent (driver);

???? 185? Log ("Successfully Registered JDBC Driver ///"? DRIVERCLASSNAME);

???? 186?}

???? 187? Catch? (Exception? E)? {

???? 188? Log ("Unable to register the JDBC driver:?"?

???? 189? Driverclassname? ? ",? Error:?"? ? E);

???? 190?}

???? 191?}

???? 192?}

???? 193

???? 194? / **

???? 195? *? Write text information to log files

???? 196? * /

???? 197? Private? Void? Log (string? Msg)? {

???? 198? Log.println (new? DATE ()? ? ":?" ? Msg);

???? 199?}

???? 200

???? 201? / **

???? 202? *? Write text information with exceptions to log files ???? 203? * /

???? 204? Private? Void? Log (throwable? E,? String? Msg)? {

???? 205? Log.println (new? DATE ()? ? ":?" ? Msg);

???? 206? E.PrintStackTrace (log);

???? 207?}

???? 208

???? 209? / **

???? 210? *? This internal class defines a connection pool. It can create a new connection as required until the predetermined least // r

???? 211? *? Dalian Digital. Before returning to the client, it can verify the validity of the connection.

???? 212? * /

???? 213? Class? Dbconnectionpool? {

???? 214? Private? Int? Checkedout;

???? 215? Private? Vector? FreeConnections? =? New? Vector ();

???? 216? Private? Int? Maxconn;

???? 217? Private? String? Name;

???? 218? Private? String? Password;

???? 219? Private? String? URL;

???? 220? Private? String? User;

???? 221

???? 222? / **

???? 223? *? Create a new connection pool

???? 224? *

???? 225? *? @ Param? Name? Connect pool name

???? 226? *? @ Param? URL? JDBC? URL of the database

???? 227? *? @ Param? User? Database account, or? Null

???? 228? *? @ Param? Password? Password, or? Null

???? 229? *? @ Param? Maxconn? This connection pool allows the maximum number of connections to be established

???? 230? * /

???? 231? Public? Dbconnectionpool (String? Name,? String? URL,? String? User,? String? Password,

???? 232? Int? Maxconn? {

???? 233? This.name? =? Name;

???? 234? This.URL? =? URL;

???? 235? This.user? =? User;

???? 236? This.password? =? Password;

???? 237? This.maxconn? =? Maxconn;

???? 238?}

???? 239

???? 240? / **

???? 241? *? Back to the connection pool will not be used.

???? 242? *

???? 243? *? @ Param? Con connection with the client

???? 244? * /

???? 245? Public? Synchronized? Void? FreeConnection (connection? Con)? {

???? 246? //? Add the specified connection to the end of the vector

???? 247? Freeconnections.addelement (con);

???? 248? Checkedout -;

???? 249? Notifyall ();

???? 250?}

???? 251

???? 252? / **

???? 253? *? A available connection from the connection pool. If there is no idle connection and the current number of connections is less than the maximum connection

???? 254? *? Number restrictions, create a new connection. If the connection to the available is no longer valid, it is deleted from the vector.

???? 255? *? Then recursively call yourself to try new available connections.

???? 256? * /

???? 257? Public? SYNCHRONIZED? Connection? GetConnection ()? {

???? 258? Connection? Con? =? Null;

???? 259? If? (Freeconnections.size ()?>? 0)? {

???? 260? //? Get the first available connection in the vector ???? 261? Con? =? (Connection)? FreeConnections.FirstEth ();

???? 262? Freeconnections.removelelementat (0);

???? 263? Try? {

???? 264? If? (Con?isclosed ())? {

???? 265? Log ("From the connection pool"? ? Name "deletes an invalid connection");

???? 266? //? Recursive calls yourself, try again to get available connections

???? 267? Con? =? GetConnection ();

???? 268?}

???? 269?}

???? 270? Catch? (SQLEXCEPTION? E)? {

???? 271? Log ("From the connection pool"? ? Name "deletes an invalid connection");

???? 272? //? Recursive calls yourself, try again to get available connections

???? 273? Cond? =? GetConnection ();

???? 274?}

???? 275?}

???? 276? Else? If? (Maxconn? ==? 0? ||? Checkedout?

???? 277? Con? =? NewConnection ();

???? 278?}

???? 279? If? (Con?! =? Null)? {

???? 280? Checkedout ;

???? 281?}

???? 282? Return? Con;

???? 283?}

???? 284

???? 285? / **

???? 286? *? Acquisition of available connections from the connection pool. You can specify the maximum time // r of the client to wait

???? 287? *? See the previous getConnection () method.

???? 288? *

???? 289? *? @ Param? Timeout? Waiting time limit in milliseconds

???? 290? * /

???? 291? Public? SYNCHRONIZED? Connection? GetConnection (long? Timeout)? {

???? 292? Long? Starttime? =? New? Date (). Gettime ();

???? 293? Connect? Con;

???? 294? While? ((Con? =? GetConnection ())? ==? Null)? {

???? 295? Try? {

????? 296? Wait (timeout);

???? 297?}

???? 298? Catch? (InterruptedException? E)? {}

???? 299? If? (New? Date (). GetTime ()? -? Starttime)?> =? Timeout? {

???? 300? //? Wait () returned reason is timeout

???? 301? Return? Null;

???? 302?}

???? 303?}

???? 304? Return? Con;

???? 305?}

???? 306

???? 307? / **

???? 308? *? Close all connections

???? 309? * /

???? 310? Public? Synchronized? Void? Release ()? {

???? 311? Enumeration? AllConnections? =? FREECONNECTIONS.ELEMENTS ();

???? 312? While? (AllConnections.hasMoreElements ())? {

???? 313? Connection? Con? =? (Connection)? AllConnections.nexTelement ();

???? 314? Try? {

???? 315? Con.close ();

???? 316? Log ("Close a connection in the connection pool"? ? Name "); ???? 317?}

???? 318? Catch? (SQLEXCEPTION? E)? {

???? 319? Log (e ,? "Unable to close the connection in the connection pool"? Name ");

???? 320?}

???? 321?}

???? 322? Freeconnections.removealleleference ();

???? 323?}

???? 324

???? 325? / **

???? 326? *? Create a new connection

???? 327? * /

???? 328? Private? Connection? Newconnection ()? {

???? 329? Connection? Con? =? NULL;

???? 330? Try? {

???? 331? If? (User? ==? NULL)? {

???? 332? Con? =? Drivermanager.getConnection (URL);

???? 333?}

???? 334? Else? {

???? 335? Con? =? Drivermanager.getConnection (URL,? User,? Password);

???? 336?}

???? 337? Log ("Connection Pool"? ? Name "Creates a new connection");

???? 338?}

???? 339? Catch? (SQLEXCEPTION? E)? {

???? 340? Log (e ,? "Unable to create the following URL connections:?"? ? Url);

???? 341? Return? Null;

???? 342?}

???? 343? Return? Con;

???? 344?}

???? 345?}

???? 346?}

???? Third, class dbconnectionpool Description /

????

???? This class is implemented in 209 to 345 lines, which indicates the connection pool to a database. The database is identified by a JDBC? URL. A JDBC? URL consists of three parts: protocol identifier (always jdbc), driver ID (such as? ODBC, IDB, Oracle, etc.), database identity (its format is dependent on the driver). For example, JDBC: ODBC: Demo is a JDBC® URL pointing to the DEMO database, and access the database to use the JDBC-ODBC driver. Each connection pool has a name for the client and an optional user account, password, maximum connection limit. If some database operations supported by the web application can be executed by all users, while other operations should be performed by special licensed users, they can define the connection pool for two types of operations, and the two connecting pools use the same JDBC? URL , But use different accounts and passwords.

???? The construction function of class DBConnectionPool requires all the above data as its parameters. As shown in 222 to 238, these data are saved as their instance variable:

Such as 252 to 283 lines, 285 to 305 lines, the client program can get the available connections using two methods provided by the DBConnectionPool class. The commonality is that there is a available connection in the connection pool, but it will return directly, otherwise create a new connection and returns. If there is no connection available and the total number of connections is equal to the maximum limit, the first method will return directly to NULL, and the second method will wait until there is available connection.

???? All available connection objects are registered in the vector (Vector) named FreeConnections. If there are more connections in the vector, getConnection () always selects the first. At the same time, since the new available connection is always added from the tail, the database connection is reduced to the lowest degree due to long-term idleness. ???? The first getConnection () Call the isclosed () method verify connection is still valid before returning to the client. If the connection is turned off or triggered, getConnection () recursively calls yourself to attempt to get additional available connections. If there is no available connection in the vector freeConnections, the getConnection () method checks if the maximum connection limit is specified. If you have already specified, check if the current connection has reached the limit. There is no limit to the MAXCONN 0 here. If the maximum number of connections is not specified or the current connection is less than this value, the method attempts to create a new connection. If you create success, add a count that has been used and returned, otherwise return null value.

???? As shown in 325 to 345, create a new connection is implemented by the newconnection () method. The creation process is related to whether the database account has been specified, the password is related.

??????? JDBC's DriverManager class provides multiple getConnection () methods, these methods are used to use the JDBC? URL and other parameters such as user accounts and passwords. DriverManager uses the specified JDBC® URL to determine the driver and establish a connection that is suitable for the target database.

???? The second getConnection () method implemented in the 285 to 305 rows requires a time parameter in milliseconds, which indicates the longest time for the client to wait. The specific operation of establishing a connection is still implemented by the first getConnection () method.

???? This method is executed to initialize StartTime to the current time. Try a connection in the While loop. If it fails, call WAIT () is called with a given time value. The return of Wait () may be due to other threads to call notify () or notifyall (), or may be due to the predetermined time. To find the real cause of Wait () returned, the program is used to reduce the start time (startTime), if the difference is greater than the predetermined time, returns a null value, otherwise getConnection () again.

???? Register the idle connection to the connection pool to the freeConnection () method of the 240 to 250 rows, and its parameters are the connection object that is returned to the connection pool. This object is added to the end of the FreeConnections vectors, and then reduces the use of the connection count. Calling NotifyAll () is to inform the other thread that is waiting for the available connection.

???? Many servlet engines provide a variety of methods for implementing security shutdown. The database connection pool needs to be aware of the event to ensure that all connections can be turned off. DBConnectionManager class negative coordinates the closing process, but the task of all connected connections is turned off by the DBConnectionPool class. The Release () method implemented at 307 to 323 rows is called by DBConnectionManager. This method traverses the FreeConnections vector and closes all connections, then removes these connections from the vector.

????

????

???? four, class DBCONNECTIONMANAGER? Description /

????

???? This class can only create an instance, and other objects can call their static methods (also known as class methods) to get the unique instance reference. As shown in the 031 to 036, the construction function of the DBConnectionManager class is private, which is to avoid instances of other objects to create this class.

???? The client of the DBConnectionManager class can call the getInstance () method to get a reference to the unique instance of this class. As shown in the 018 to 029 line, the unique example of the class is created during the GetInstance () method, which is created during the call, which will now be stored in the static variable instance. Each time you call getInstance () add a DBConnectionManager's customer count. That is, the count represents the total number of client programs that reference the DBConnectionManager unique instance, which will be used to control the closing operation of the connection pool. ???? The initialization of this class is completed by the private method init () between 146 to 168 lines. Where? GetResourceAsStream () method is used to locate and open an external file. The positioning method of external documents depends on the implementation of the class loader. The standard local type loader lookup operation always begins the path where the class file is located, and it is also possible to search the path declared in classpath. Db.properties is a property file that contains the key-value pair that defines the connection pool. The common attributes available for definition are as follows:

????

???? drivers? Separated JDBC driver class list /

???? logfile? The absolute path of the log file

????

???? Other attributes and specific connection pools, should add the connection pool name before its attribute name:

????

????

Database? JDBC? URL?

????

.maxconn? Allow the maximum number of connections to be established, 0 means no limitations?

????

.user? Database account for this connection pool

????

.password? Corresponding password /

????

???? The URL attribute is required, while other properties are optional. The database account account and password must be legitimate. DB.Properties file for Windows platforms is as follows:

????

???? driver = sun.jdbc.odbc.jdbcodbcdriver? JDBC.IDBDRIVER

???? logfile = d: //user/src//java//dbconnectionManager//log.txt

????

???? idb.url = jdbc: idb: c: //local//javawebserver1.1//db//db.prp

???? idb.maxconn = 2

????

???? access.url = jdbc: ODBC: Demo

???? access.user = demo

???? access.password = demopw

????

???? Note that the backslash in the Windows path must be input 2, which is because the backslash in the properties file is also a escape character.

???? init () method After creating a property object and reading the DB.Properties file, start checking the logfile property. If you do not specify a log file in the properties file, the DBConnectionManager.log file in the current directory is default. If the log file cannot be used, the log record is output to System.err.

???? Loading and register all the JDBC drivers specified in the Drivers property implementation of the loadDrivers () method between the 170 to 192 rows. This method first uses StringTokenizer to split the drivers attribute value into a string corresponding to the driver name, and then load these classes and create its instance, and finally register this instance in the DRIVERMANAGER and add it to a private vector Drivers. The vector drivers will cancel all JDBC? The registration of all JDBC? Drivers from DriverManager when the service is turned off.

????? The last task of the init () method is to call the private method createPools () Create a connection pool object. For example, the CreatePools () method first creates an enumeration object of all attribute names (ie ENUMERATION object, the object can be imagined as a element series, and call its nextElement () method to return each element), then The search name is the attribute ending with ".url". For each of the qualified properties, first extract its connection pool name, and then read all properties belonging to the connection pool, and finally create the connection pool object and save it in the instance variable pools. Hash Table (Hashtable Class?) Pools implements the mapping between the connection pool name to the connection pool object, here the connection pool name is key, the connection pool object is the value. ???? To facilitate customer programs to get available connections from the specified connection pool or return the connection to the connection pool, class DBConnectionManager provides methods getConnection () and FreeConnection (). All of these methods require that the connection pool name is specified in the parameter, the specific connection acquisition or return operation calls the corresponding connection pool object to complete. Their implementations are from 051 to 064, 066 to 080 rows, 038 to 049.

Such as 082 to 107, the security shutdown of the connection pool is implemented, and DBConnectionManager provides method (). In the above, we have mentioned that all DBConnectionManager's clients should call the static method getInstance () to get a reference to the manager, which will increase the customer count. The client calls Release () can decrement the count when shutting down. When the last customer program is called Release (), the decremented reference count is 0, you can call the Release () method of each connection pool to close all connections. The Last task of the management class Release () method is to revoke all JDBC drivers registration.

????

????

???? 5, servlet uses the connection pool example

????

Servlet lifecycle, such as the servlet? API, is as:

????

???? 1)? Create and initialize the servlet (Init () method).

???? 2)? Respond to the service request for the client (Service () method).

???? 3)? Servlet terminates, releases all resources (Destroy () method).

????

???? This example demonstrates the connection pool application, the relevant operations in the above key steps are:

????

???? 1)? In init (), use the instance variable connmgr? Save the reference to DBConnectionManager.getInstance () returned.

???? 2)? In Service (), call getConnection (), execute the database operation, and return the connection to the connection pool with freeConnection.

???? 3)? In DESTROY (), Release () Turse all connections, release all resources.

????

???? Example program list is as follows:

????

Import? java.io. *; import? java.sql. *; import? javax.servlet. *; import? javax.servlet.http. *; public? class? testservlet? {????priVate DBCONNECTIONMANAGER? CONNMGR; ???? public? Void? Init (servletconfig? Conf)? {???????? super.init (conf); ???????? connmgr? =? DbconnectionManager.getinstance (); ????} ???? public? Void? Service (httpservletRequest? REQ,? HttpservletResponse? Res) ???????? throws? IoException? {?????? ??? res.setContentType ("text / html"); ???????? printWriter? out? =? res. maxWriter (); ???????? connection? connmgr. GetConnection ("idb"); ???????? IF? (con? ==? null)? {???????????? Out.println ("cannot get database connections." ); ???????????? Return; ????????} ????????} ???????}? rs? =? null; ???????? ResultSetMetadata ? md? =? null; ???????? statement? stmt? =? null; ???????? Try? {???????????? STMT? =? Con.createStatement (); ???????????? rs? =? stmt.executeQuery ("SELECT? *? from? Employee"); ???????????? MD ? =? rs.getMetadata (); ???????????? Out.println ("employee data

"); ???????????? while? (rs.next ())? {?????????????????????????? Out.Println ("

"); ?????????????????? (int? i? =? 1;? i?

???????????????????? out.print (Rs.getstring (i)? ? ",?"); ??????????? ?????} ????????????} ???????????? STMT.Close (); ???????????? rs .close (); ????????}? catch? (SQLEXCEPTION? E)? {????????????? E.PrintStackTrace (out); ??????? ?} ???????? connmgr.freeConnection ("idb",? con); ????} ???? public? void? destroy ()? {???????? connmgr. Release (); ???????? super.destroy (); ????}}

转载请注明原文地址:https://www.9cbs.com/read-124157.html

New Post(0)