In the past, the Java access database is used directly to connect to the database directly; in general, every database client application is opened one or more database connections, and we can use the Pool provided by WebLogic to access the database in WebLogic. To make each database client share one or more database connections, which can improve the execution efficiency of the program and reduce the burden on the database server. This article mainly discusses the implementation of the server configuration and code, here, do not want to discuss too many poores concept and characteristics, as for the concept and principle of pool, I suggest you look at "Increase Servlet" The information of the efficiency of the access database is the author. This article introduces the Pool provided by WebLogic 5.1 mysql, and I wrote two servlet as an example of the control. One is the JDBC provided with MySQL to access mysql directly, and the other is the power provided by WebLogic to access. database.
First, please read the JDBC provided by MySQL to access mysql code directly: Myjdbc.java ------------------------------- --------------------------------------------- Import java.io . *; import java.sql *;. import weblogic.db.jdbc *;. import java.util.Properties; import javax.servlet *;. import javax.servlet.http *;. public class myjdbc extends HttpServlet {public void service (HttpServletRequest req, HttpServletResponse res) throws IOException {res.setContentType ( "text / html"); PrintWriter out = res.getWriter (); out.println ( "
"); connection conn = null; try {// ======== ============================================================================================================================================================================================================= ======================= Class.Forname ("org.gjt.mm.mysql.driver"). NewInStance (); conn = drivermanager.getConnection "JDBC: MySQL: / / LOCALHOST / ZJWS? User = ZJXYZ & Password = XYZ"); / / ============================= =============================================================================================================================================================================================================== Statement Stmt = Conn.createStatement (); ResultSet RS = Stmt.executeQuery ("SELECT * from users"); while (rs.next ()) {Out.Println (rs.getstring ("UserID") "/ T " RS.GetString (" passwd ") " / t " rs.getstring (" name "));}} rclose (); stmt.close (); conn.close ();} catch (Exception E) {System.err.Println ("Unable to load driver."); E.PrintStackTrace ();} out.println (" pre> body> html>);}} The above code The function is to list the use of the Users table in MySQL's ZJWS database. I tag the JDBC driver. The following is to use pool to read the database, I also make a mark, please compare it.
The first is to configure the server, join in WebLogic.properties, # ==================================== ================= WebLogic.jdbc.connectionPool.Mysqlpool1 = / URL = JDBC: mysql: // localhost / zjws? User = zjxyz, / driver = org.gjt.mm .mysql.Driver, / loginDelaySecs = 1, / initialCapacity = 4, / maxCapacity = 10, / capacityIncrement = 2, / allowShrinking = true, / shrinkPeriodMins = 15, / refreshMinutes = 10, / allow = guest, / props = user = ZJXYZ; password = xyz; server = zjxyz # ========================================== ============= put mysql's JDBC driver in / WebLogic / MyServer / ServerClasses / Directory, I don't know why, I put the JAR package in their directory, add the path to classpath, When the result is started, I can't find the Class, so I have to unlocate the org directory and the files inside it, and the result is ok. MySQL must start more than WebLogic, because WebLogic is created when starting.
MySQLPool.java ----------------------------------------------- -------------------------------------------------- - Import java.io. *; import java.sql. *; Import weblogic.db.jdbc. *; Import java.util.properties; import javax.servlet. *; Import javax.servlet.http. *; Public class mysqlpool extends HttpServlet {public void service (HttpServletRequest req, HttpServletResponse res) throws IOException {res.setContentType ( "text / html"); PrintWriter out = res.getWriter (); out.println ( "
Hello World! Title> head> "); out.println (" test webLogic pool! H1>
); connection conn = null; try {// == ============================================================================================================================================================================================================= ================================================2; Props.SetProperty Props.SetProperty ("Password", "zjxyzken"); Driver MyDriver = (DRIVER) Class.Forname ("WebLogic.jdbc.Pool.Driver). NewInstance (); conn = MyDriver.Connect ("JDBC: WebLogic: Pool: MySQLPool1", PrOPS); / / ============================== =============