数据库缓冲池在tomcat6.0中的配置
2017-06-05 10:23
405 查看
一、创建Servlet,在web.xml文件中加入下面配置
之后,(使用的数据库是sqlserver2005)在tomcat安装目录下面的lib文件夹中加入连接sqlserver的sqljdbc.jar,网上一些例子说要加到common/lib文件夹下面,其实tomcat6.0安装目录下默认没有common文件夹,这个版本直接把驱动加入到安装目录下面的lib文件夹中就行了.)
(注:提醒一些大意的朋友,同时在项目的WebRoot/WEB-INF/lib下面也应加入了sqljdbc.jar)
----------------------------------------------------------------------
二、在WebRoot/META-INFO/下创建context.xml,如下
----------------------------------------------------------------
三、写Db连接数据库类
------------------
写dao:
--------------------------------------------
四、写servlet实现,关键代码如下:
----------------------------------------
五、jsp测试页面
------------------------------------
<resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/john</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
之后,(使用的数据库是sqlserver2005)在tomcat安装目录下面的lib文件夹中加入连接sqlserver的sqljdbc.jar,网上一些例子说要加到common/lib文件夹下面,其实tomcat6.0安装目录下默认没有common文件夹,这个版本直接把驱动加入到安装目录下面的lib文件夹中就行了.)
(注:提醒一些大意的朋友,同时在项目的WebRoot/WEB-INF/lib下面也应加入了sqljdbc.jar)
----------------------------------------------------------------------
二、在WebRoot/META-INFO/下创建context.xml,如下
<?xml version="1.0" encoding="GBK"?> <Context> <Resource name="jdbc/john" type="javax.sql.DataSource" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://127.0.0.1:1433;databaseName=test" username="john" password="123" maxIdle="2" maxWait="5000" validationQuery="select 1" maxActive="4" /> </Context>
----------------------------------------------------------------
三、写Db连接数据库类
import java.sql.Connection; import java.sql.SQLException; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; public class Db { java.sql.Connection con = null; public Connection getCon(){ try{ Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/john"); con = ds.getConnection(); }catch(Exception e){ e.printStackTrace(); } return con; } public void conClose(){ if(con != null){ try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
------------------
写dao:
package com.tonghu.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.tong.DBUtil.Db; import com.tonghu.vo.DeptVO; public class DaoTest { //DeptVO dept = new DeptVO(); public boolean login(DeptVO dept){ Db db = new Db(); Connection conn = db.getCon(); ResultSet rs = null; PreparedStatement ps = null; String sql = "select * from dept where dname=? and loc=?"; try{ ps = conn.prepareStatement(sql); ps.setString(1, dept.getDname()); ps.setString(2, dept.getLoc()); rs = ps.executeQuery(); if(rs.next()){ dept.setDeptno(rs.getString("deptno")); return true; } }catch(SQLException e){ e.printStackTrace(); }finally{ try { rs.close(); ps.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return false; } }
--------------------------------------------
四、写servlet实现,关键代码如下:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //生成会话session HttpSession session = request.getSession(); response.setContentType("text/html;charset=utf-8"); ServletContext sc = getServletContext(); // PrintWriter out = response.getWriter(); DaoTest da = new DaoTest(); String dname = request.getParameter("dname"); String loc = request.getParameter("loc"); DeptVO dept = new DeptVO(); dept.setDname(dname); dept.setLoc(loc); if(da.login(dept)){ //设置把数据保存到session中 // session.setAttribute("deptno", dept.getDeptno()); request.setAttribute("deptno", dept.getDeptno()); // System.out.println("==="+dept.getDeptno()); //使用跳转方式 sc.getRequestDispatcher("/loginsuccess.jsp").forward(request, response); //重定向 // response.sendRedirect("../loginsuccess.jsp"); // return; }else{ //使用重定向则应该加上一个return,因为后面的内容不需要再加载了 response.sendRedirect("../error.jsp"); return; } }
----------------------------------------
五、jsp测试页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'testjndi02.jsp' starting page</title> </head> <body> <center> <h1>登录界面</h1><br> <form action="servlet/Test" method="post"> 用户名:<input type="text" name="dname" size="20" /><br /> 密 码:<input type="password" name="loc" size="20" /><br /> <input type="submit" value="submit" /> </form> </center> </body> </html>
------------------------------------
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'loginsuccess.jsp' starting page</title> </head> <body> <h1>登录成功!!</h1> <!-- <%=session.getAttribute("deptno")%> --> <h1>欢迎:${requestScope.deptno}</h1> </body> </html>
相关文章推荐
- TomCat6.0配置连接池连接SQL Server 2005数据库
- 配置数据库连接池,Tomcat6.0 连接池的配置
- Tomcat6.0配置连接池连接SQL Server 2008数据库
- 怎样配置Tomcat6.0的数据库连接池详解
- Tomcat6.0配置MySql5.0数据库的步骤
- 怎样配置Tomcat6.0的数据库连接池详解
- tomcat6.0 + Spring 配置JNDI连接数据库
- 在Tomcat 6.0中配置数据库JNDI的方法
- Tomcat6.0配置连接池连接SQL Server 2008数据库
- tomcat配置数据库连接池 [摘抄]
- 使用Tomcat-5.5.20配置数据库连接池详细介绍
- Tomcat-5.5.20下配置MySQL-5.0.27的数据库连接池(二)
- tomcat数据库 jndi文件配置
- 使用Tomcat-5.5.20配置数据库连接池详细介绍
- Tomcat jndi 数据库配置 连接池配置
- JDBC+Tomcat+sqlServer配置数据库联接池
- Tomcat中数据库连接池的配置
- Tomcat jndi 数据库配置 连接池配置
- Jboss3.0-tomcat4.03的配置-4(数据库的配置)
- Tomcat5.5.x的数据库连接池的配置与Tomcat5.0.X不同