Tomcat jndi 数据库怎么配置 连接池配置

machiatto_Job 2010-01-12 05:15:51
Tomcat jndi 数据库怎么配置 连接池配置
...全文
113 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
道光2008 2010-01-13
  • 打赏
  • 举报
回复
tomcat6下

tomcat6 \server\context.xml

XML code

<!-- The contents of this file will be loaded for each web application -->
<Context>

<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>META-INF/context.xml</WatchedResource>

<!-- Uncomment this to disable session persistence across Tomcat restarts -->

<Resource
name="jdbc/mydb"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="30"
maxWait="5000"
username="root"
password="root"
wait_timeout ="2800000"
url="jdbc:mysql://192.168.0.152/test?autoReconnect=true"
maxActive="100"/>

</Context>
TD.Lemon 1 9 0 0 2010-01-13
  • 打赏
  • 举报
回复
GOOGLE!
liguangwen86 2010-01-12
  • 打赏
  • 举报
回复
wad12302 2010-01-12
  • 打赏
  • 举报
回复
或者配成 全局的:
jakarta-tomcat-5.5.7\conf

context.xml


<!-- The contents of this file will be loaded for each web application -->
<Context>

<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>META-INF/context.xml</WatchedResource>

<!-- Uncomment this to disable session persistence across Tomcat restarts -->

<Resource
name="jdbc/mydb"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="30"
maxWait="5000"
username="root"
password="root"
wait_timeout ="2800000"
url="jdbc:mysql://192.168.0.152/test?autoReconnect=true"
maxActive="100"/>

</Context>


然后在项目 WEB-INF目录中web.xml
添加
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mydb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

最后在程序中:
String myname = "java:comp/env/jdbc/juddiDB";
DataSource ds = (DataSource) ctx.lookup(myname);
  • 打赏
  • 举报
回复
web的配置xml文件) 中添加resource

<Resource name="MYSQL"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
password="********"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://localhost:3306/jnestore"
maxActive="10"/>

以上是tomcat 5.5版本的配置,5.5版本只有这样陪才有效果,以前的版本可以按照paramter的方法添加;

Resource 一定要包含在<Context ......> </Context> 之间

在你的web的路近下,WEB-INF中找到web.xml,添加资源映射

<resource-ref>
<description>connection</description>
<res-ref-name>MYSQL</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

把mysql的jdbc驱动.jar包放到tomcat的common/lib下去,也可以放到WEB-INF/lib下,

重新启动tomcat.

就可以用Context ctx = new InitialContext();

DataSource ds = (DataSource) ctx.lookup("java:comp/env/MYSQL");

Connection con = ds.getConnection(); 来连接数据库连接池
CXYZJ 2010-01-12
  • 打赏
  • 举报
回复
希望我的回答能够带给楼主帮助

给楼主个链接吧 自己去看看 那个解释很详细 http://www.sunxin.org/forum/thread/21457.html
CXYZJ 2010-01-12
  • 打赏
  • 举报
回复
占沙发~

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧