Experience the real struts development (2 period) 2

zhaozj2021-02-17  47

Username

root

Password

driverclassname

com.mysql.jdbc.driver

Database address

->

URL

jdbc: mysql: // localhost / test? Useunicode = true

Set up a good connection pool, we test the connection pool

We write a servlet test to use the connection pool in servlet

code show as below

Package com.test.Web;

Import java.sql.connection;

Import java.sql.preparedStatement;

Import java.sql.resultset;

Import javax.naming.context;

Import javax.naming.initialcontext;

Import javax.naming.namingexception;

Import javax.servlet.servletException;

Import javax.servlet.http.httpservlet;

Import javax.sql.datasource;

Import org.apache.log4j.logger;

/ **

* @Author yanghx

* To change the template for this generated type comment Go to window -

* Preferences - Java - Code Generation - Code and Comments

* /

Public class testservlet extends httpservlet {

Private static logger log = logger.getlogger;

Public void init () throws servletexception {

Super.init ();

String JNDINAME = "Java: comp / env / jdbc / mysql";

Try {

Context ctx = new initialContext ();

IF (CTX == NULL) {

Log.Error ("Can't find context");

Throw new Exception ("Can't find context");

}

DataSource DS = (Datasource) ctx.lookup (JNDINAME);

Connection conn = ds.getConnection ();

PreparedState psmt = conn

.preparestatement ("Select * from TBL_TESTHBM);

ResultSet RS = psmt.executeQuery ();

Log.info ("connecting pool OK"); rs.close ();

} catch (namingexception e) {

Log.Error ("Can't find the context" E.getMessage ());

} catch (exception e) {

Log.Error ("Error" E.getMessage ());

}

}

}

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

New Post(0)