日期:2014-05-18  浏览次数:20720 次

请问:在新建项目中如何使用Tomcat 的server.xml里连接Mysql的配置信息?
我在自己从零搞个项目学习一下,我想问一下在新建项目中如何使用Tomcat 的server.xml里连接Mysql的配置信息?连接配置已经配置好,怎么在项目的底层代码中使用这个配置信息?这个过程是怎么样的?

------解决方案--------------------
在server.xml中增加一段内容(其中{}中的内容应该用合适的值替换):
<Resource name="jdbc/shortord"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
username="{userName}"
password="{password}"
maxIdle="40"
maxWait="4000"
url="jdbc:mysql://{hostIp}/shortord?useUnicode=true&amp;characterEncoding=gbk&amp;autoReconnect=true"
maxActive="250"
removeAbandoned="true"
removeAbandonedTimeout="180"
logAbandoned="true" />
同时,<context>中插入以下内容:
<ResourceLink name="jdbc/shortord" global="jdbc/shortord" type="javax.sql.DataSource"/>

然后在程序中使用JNDI获得。
------解决方案--------------------
对的,楼上正解
------解决方案--------------------
这个是tomcat文档里的说明
Java code

// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");

// Look up our data source
DataSource ds = (DataSource)envCtx.lookup("jdbc/EmployeeDB");
// Allocate and use a connection from the pool
Connection conn = ds.getConnection();
conn.close();

------解决方案--------------------
Java code

// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");

// Look up our data source
DataSource ds = (DataSource)envCtx.lookup("jdbc/EmployeeDB");
// Allocate and use a connection from the pool
Connection conn = ds.getConnection();
conn.close();