数据库缓冲池在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 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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值