1.
private String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
private String url = "jdbc:odbc:fms";
整个Dao类
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package db;
/**
*
* @author Administrator
*/
import java.sql.*;
public class dao {
private Connection con;
private String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
private String url = "jdbc:odbc:fms";
public dao(){
}
public Connection getConnection() throws ClassNotFoundException,SQLException
{
Class.forName(driver);
con = DriverManager.getConnection(url);
return con;
}
public int insert(String sql,Object ... args) throws SQLException,ClassNotFoundException{
PreparedStatement pstmt = getConnection().prepareStatement(sql);
int affectedRow = 0;
for(int i=0;i<args.length;i++){
//System.out.println((String)args[i]);
pstmt.setString(i+1, (String)args[i]);
}
affectedRow = pstmt.executeUpdate();
pstmt.close();
return affectedRow;
}
public ResultSet query(String sql,Object ... args) throws SQLException,ClassNotFoundException
{
PreparedStatement pstmt = getConnection().prepareStatement(sql);
ResultSet rs;
for(int i=0;i<args.length;i++){
pstmt.setString(i+1, (String)args[i]);
}
rs=pstmt.executeQuery();
//pstmt.close();
return rs;
}
public int delete(String sql,Object ... args) throws SQLException,ClassNotFoundException{
PreparedStatement pstmt = getConnection().prepareStatement(sql);
int affectedRow;
for(int i=0;i<args.length;i++){
pstmt.setString(i+1, (String)args[i]);
}
affectedRow = pstmt.executeUpdate();
return affectedRow;
}
public int update(String sql,Object ... args) throws SQLException,ClassNotFoundException{
PreparedStatement pstmt = getConnection().prepareStatement(sql);
int affectedRow;
for(int i=0;i<args.length;i++){
pstmt.setString(i+1, (String)args[i]);
}
affectedRow = pstmt.executeUpdate();
return affectedRow;
}
public void closeConnection() throws Exception{
if(con != null && !con.isClosed()){
con.close();
}
}
public static void main(String ... args){
dao dao1 = new dao();
try{
ResultSet rs = dao1.query("select * from client");
while(rs.next())
{
System.out.println("haha");
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
2.改变encoding
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
if (debug) log("EncodingFilter:doFilter()");
doBeforeProcessing(request, response);
Throwable problem = null;
try {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}
3.main的验证登陆机制
if(session.getAttribute("account")==null) //判断是否为空,为空则表示没有登录
{
response.sendRedirect("login.jsp?error=2");
}
4.loginvalidate的jsp页,不用servlet方法,注意<setproperty>用法
<jsp:setProperty name="admin" property="*">
可以自动获取有同名的request的parameter,放进去哪个name的实例中
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%@page import="java.util.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<jsp:useBean id="adminDao" class="db.AdminDao" scope="page" />
<jsp:useBean id="admin" class="db.Admin" scope="page" />
<jsp:setProperty name="admin" property="*" />
</head>
<body>
<%
admin = adminDao.login(admin.getAccount(), admin.getPassword());
if(admin!= null){
session.setAttribute("account", admin.getAccount());
response.sendRedirect("main.jsp");
}
else{
response.sendRedirect("login.jsp?error=1");
}
%>
</body>
</html>
retrieveUserByName方法
注意那个break的用法
还有absolute之后游标就必须先读一次!
public List<Client> retrieveUserByName(String name,int neededPage) throws Exception//根据姓名查询
{
dao db = new dao();
List<Client> clients = new ArrayList<Client>();
try
{
;
ResultSet rs = db.query(sql1,"%" + name + "%" );
if(rs.next()){ //如果rs有数据则计算页数等
rs.last();
totalRow = rs.getRow();
this.computeTotalPage(totalRow);
}
else
{
return clients; //无数据则直接返回空指针
}
if(neededPage>totalPage){
neededPage = totalPage;
}
int rowStart = pageSize*(neededPage-1)+1;
rs.absolute(rowStart);
for(int i=0;i<pageSize;i++)
{
clients.add(new Client(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6)));
if(!rs.next())
break;
}
}
catch (SQLException ex)
{
throw new Exception(ex.getMessage());
}
finally
{
db.closeConnection();
}
return clients;
}
对应的servlet
注意session必须第一次的时候加回去content
request必须重新设每次的attribute
jsp给了servlet后,servlet用forward,原来的request已经消失!
要获得session,要用 httpsession 是javax。servlet。http里面的
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
HttpSession session =request.getSession(); //获取用户的Session对象
ClientDao clientDao = new ClientDao();
List<Client> clients = new ArrayList<Client>();
RequestDispatcher rd ;
String content; //获取搜索内容
String choice; //获取用户的选择
int neededPage; //用户要访问哪一页
try {
if(session.getAttribute("content")== null){ //是main传来的还是result页传来的?
content = request.getParameter("content");
}
else{
content = (String)session.getAttribute("content");
}
if(session.getAttribute("choice") == null){
choice = request.getParameter("choice");
}
else{
choice = (String)session.getAttribute("choice");
}
if(request.getParameter("neededPage")==null){ //初始时候没有需求页,就取第一页
neededPage = 1;
}
else{
neededPage = Integer.parseInt(request.getParameter("neededPage"));
}
if(choice.equals("name"))
{
clients = clientDao.retrieveUserByName(content,neededPage);
}
else if(choice.equals("unit"))
{
clients = clientDao.retrieveUserByUnit(content,neededPage);
}
else if(choice.equals("address"))
{
clients = clientDao.retrieveUserByAddress(content,neededPage);
}
else if(choice.equals("tel"))
{
clients = clientDao.retrieveUserByTel(content,neededPage);
}
if(session.getAttribute("content") == null)
session.setAttribute("content", content); //原来session里面没有content和choice的时候就设置,设置过久不用了
if(session.getAttribute("choice")==null)
session.setAttribute("choice", choice);
request.setAttribute("clients", clients);
request.setAttribute("currentPage", String.valueOf(neededPage)); //回传当前页
request.setAttribute("totalPage", String.valueOf(clientDao.getTotalPage())); //为了实现最后一页
rd = request.getRequestDispatcher("queryResult.jsp");
rd.forward(request, response);
}
catch(Exception ex){
ex.printStackTrace();
}
finally {
out.close();
}
}
搞完后main页记得记得要清除掉session里面的content,不然悲剧