Tomcat Access Database via JNDI

zhaozj2021-02-16  91

1, the JAR file required: commons-pool.jar, commons-dbcp.jar

2, Server.xml configuration:

factory Org.apache.commons.dbcp.basicdataasourcefactory removeabandones true Logabandoned true URL jdbc: mysql: // localhost: 3306 / Test driverclassname org.gjt. mm.mysql.driver username root password

maxwait 3000 maxidle 100 < / value> maxactive 10 3, calls in programs:

Context ctx = new initialContext (); if (ctx == null) throw new exception ("Boom - No Context");

DataSource DS = (Datasource) CTX.lookup ("java: comp / env / jdbc / hellohibernate");

IF (ds! = null) {connection conn = ds.getConnection ();

IF (conn! = null) {foo = "got connection" conn.tostring (); statement stmt = conn.createstatement (); resultset = stmt.executeQuery ("SELECT Username, ID from user"; if (RST) .next ()) {foo = rst.getstring (1); bar = Rst.Getint (2);} conn.close ();}}

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

New Post(0)