来源:http://mqzsweet.iteye.com/blog/144738
1、修改%TOMCAT_HONE%/CONF/SERVER.XML文件,在<host></host>节点下添加以下内容:
xml 代码
- <Context crossContext="true" debug="5" docBase="BBS"
-
path="/BBS" reloadable="true">
- <Resource name="jdbc/BBS"
- type="javax.sql.DataSource" auth="Container"
- driverClassName="com.mysql.jdbc.Driver" maxActive="100"
- maxIdle="30" maxWait="10000"
- url="jdbc:mysql://localhost:3306/userdb?autoReconnect=true"
- username="root" password="" />
- Context>
2、修改工程下的WEB.XML文件,添加以下内容:
xml 代码
- <resource-ref>
-
<description>JNDI DataSource Testdescription>
-
<res-ref-name>dbc/BBSres-ref-name>
-
<res-type>javax.sql.DataSourceres-type>
-
<res-auth>Containerres-auth>
-
<res-sharing-scope>Shareableres-sharing-scope>
-
resource-ref>
3、将MySql驱动文件添加到%TOMCAT_HONE%/comman/lib目录下
4、进行代码编写
4.1、ConnectionPool类,实现数据库操作
- import java.sql.*;
- import javax.sql.DataSource;
- import javax.naming.InitialContext;
- public class ConnectionPool {
-
public void doSomething() throws Exception{
-
Connection conn = null;
-
InitialContext ctx = new InitialContext();
-
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/BBS");
-
Statement stmt = null;
-
-
try{
- conn = ds.getConnection();
-
- stmt = conn.createStatement();
-
stmt.execute("delete from user");
-
stmt.execute("insert into user(uid,password,realName,gender,email,tel,question,validateCode,answer,loginNum) values('1','2','3',4,'5','6','7',8,'9',10)");
- stmt.close();
-
stmt = null;
- conn.close();
-
conn = null;
-
}finally{
-
if(stmt!=null){
-
try{
- stmt.close();
-
}catch(SQLException sqle){
-
- }
-
stmt = null;
- }
-
-
if(conn != null){
-
try{
- conn.close();
-
}catch(SQLException sqle){
-
- }
-
conn = null;
- }
- }
-
- }
-
-
public static void main(String args[]){
-
try{
-
new ConnectionPool().doSomething();}
-
catch(Exception e){
- e.printStackTrace();
- }
- }
- }
4.2、InsertServlet servlet文件,实现调用数据库功能类并实现跳转
java 代码
- import java.io.IOException;
- import java.io.PrintWriter;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpSession;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
-
- public class InsertServlet extends HttpServlet {
-
-
-
-
-
-
-
-
-
-
-
-
public void doGet(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
-
ConnectionPool pool = new ConnectionPool();
-
try{
- pool.doSomething();
-
request.setAttribute("success", "Success");
-
-
}catch(Exception e){
-
request.setAttribute("success", "failed");
- e.printStackTrace();
- }
-
-
this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
public void doPost(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
- doGet(request,response);
- }
-
- }
4.3 index.jsp文件 进行简单的页面输出功能
jsp 代码
</
分享到:
相关推荐
本文档说明tomcat 5.5数据源配置
图解Tomcat5.5数据源的配置
用tomcat工具配置数据源比如名为mysql。则在meta-inf/context.xml中写上<?xml version=\"1.0\" encoding=\"UTF-8\"?><br><Context><br> <ResourceLink<br> name=\"mysql\" global=\"mysql\" type=\"javax....
刚刚试过可以的,网上很多都不行,希望能帮到需要的人
基于JNDI,在Tomcat5.5中配置数据源
收集而来。非常的好@@@累死了,睡觉去@ 在struts+hibernate中使用tomcat5.5的jndi数据源
TomCat5.5数据源配置
jobss4.0 和tomcat5.5的数据源配置,是我自己配置过程中写下来的,在网上看了好多,总算配成功了,希望大家喜欢.
Tomcat5.0和5.5以及6.0数据源配置 非常实用~
NULL 博文链接:https://zhaoshijie.iteye.com/blog/903265
Tomcat JNDI 数据源 自动生成程序 Servlet 版 本程序自动判断Tomcat版本,兼容tomcat 5.0、5.5、6.0等版本, 根据项目名自动生成 JNDI 配置文件,使程序员从手动配置JNDI数据源中解脱出来。 使用说明: 1、把...
使用外置Tomcat连接数据库。JNDI 配置数据源
一个简单的tomcat6.0+mysql5.5整合spring3.0和hibernate3.3的一个简单的实例,其中的datasource是tomcat JNDI配置的 type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://...
2、Chapter21下包含了所有的源代码和可部署文件,其中.war文件为可部署文件,可以直接部署在Tomcat5.5及以上版本下。 3、如果要重新编译程序,请在MyEclipse 6.0中导入项目即可。 4、在MyEclipse 6.0中启动Tomcat ...
2. 将项目源代码复制到ECLIPSE的工程目录,在eclipse中导入项目,通过 update context definition将项目部署到tomcat5.5中。 项目编译成功后,完成了数据库的初始化! 3. 重新启动TOMCAT 5.5 系统的运行地址: ...
Struts与Hibernate结合 第三章 Sttruts 框架 3.1 Struts简单介绍 3.2 Struts简单例子 第四章 Hibernate框架 4.1 Hibernian简单介绍 ...5.3 Tomcat5.5中配置连接池和数据源 5.4 5.4 对象关联关系的映射(1对1)
火龙果软件工程技术中心 在上一篇,我们讨论了如何配置Tomcat5.5的Context.xml文件,它能够建立Tomcat5.5中的只读JNDI。通过这个JNDI,可以为你的Tomcatservlet提供数据源—无论在你的servlet中使用的是Hibernate...
编辑本段常用功能的配置...6 数据源配置 比较复杂,各个版本都有所不同,请直接查看 http://java2000. net/p1906,包括tomcat5.0,tomcat5.5x,tomcat6.0的各个版本的配置方法。 更多关于Tomcat的使用,请看参考资料