jndi是命名和目录借口,用来统一访问外部资源
可在tomcat5.5的server.xml进行配置,然后在你项目的web.xml中引用一下
<resource-ref>
<res-ref-name>jdbc/web</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
可在tomcat5.5的server.xml进行配置,然后在你项目的web.xml中引用一下
<resource-ref>
<res-ref-name>jdbc/web</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
在tomcat中可以通过配置片段的方式配置jndi.
在tomcat_home\conf\Catalina\localhost\下创建一个xml文件,名字随便,最好和工程一样。
<Context path="/CSDNOne" docBase="CSDNOne"
debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_DBTest_log." suffix=".txt"
timestamp="true"/>
<Resource name="jdbc/db2DataSource"
auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/db2DataSource">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<!-- MySQL dB username and password for dB connections -->
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value>123456</value>
</parameter>
<!-- Class name for mm.mysql JDBC driver -->
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<parameter>
<name>url</name> <value>jdbc:mysql://localhost:3306/CSDNOne?autoReconnect=true</value>
</parameter>
</ResourceParams>
</Context>注意: path属性为访问路径如:http://localhost:8080/CSDNOne/index.jsp
docBase属性为web工程文件路径如:D:\workspace\webapp_telecom\fff_Backend\web
jndi名称为jdbc/db2DataSource
我用的是mysql数据库,别忘了在tomcat_home/common/lib/下放驱动包调用:
import java.sql.Connection;
import java.sql.SQLException;import javax.sql.DataSource;import javax.naming.InitialContext;
import javax.naming.Context;/**
* <p>提供数据库连接</p>
* @author zhangjian
*
*/public class DBConnection {
public static Connection getConnection(){
Connection con = null;
try{
Context initContext = new InitialContext();
//在tomcat中配置的数据源名jdbc/db2DataSource
Context envContext = (Context)initContext.lookup("java:comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/db2DataSource");
con = ds.getConnection();
}catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
return con;
}
}