首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

数据库缓冲池在tomcat6.0中的配备

2012-11-21 
数据库缓冲池在tomcat6.0中的配置一、创建Servlet,在web.xml文件中加入下面配置resource-refdescription

数据库缓冲池在tomcat6.0中的配置
一、创建Servlet,在web.xml文件中加入下面配置

<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 blocke.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 blocke.printStackTrace();}}return false;}}

--------------------------------------------
四、写servlet实现,关键代码如下:
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//生成会话sessionHttpSession 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>

是的,网上一些例子说要加到common/lib文件夹下面,其实tomcat6.0安装目录下默认没有common文件夹,这个版本直接把驱动加入到安装目录下面的lib文件夹中就行了.

热点排行