标准DAO.

标准DAO模式代码示例
//:Account.java源码:
/**
 * @author
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class Account implements java.io.Serializable {
 /**
  * Constructor for Account.
  */
 public Account() {
  super();
 }
  /* Private Fields */
  private String userId;
  private String password;
  private String email;
  private String name;
  private String status;
  private String addr1;
  private String addr2;
  private String city;
  private String state;
  private String zip;
  private String country;
  private String phone;

  /* JavaBeans Properties */
  public String getUserId() {
    return userId;
  }
  public void setUserId(String userId) {
    this.userId = userId;
  }
  public String getPassword() {
    return password;
  }
  public void setPassword(String password) {
    this.password = password;
  }
  public String getEmail() {
    return email;
  }
  public void setEmail(String email) {
    this.email = email;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }

  public String getStatus() {
    return status;
  }
  public void setStatus(String status) {
    this.status = status;
  }
  public String getAddr1() {
    return addr1;
  }
  public void setAddr1(String addr1) {
    this.addr1 = addr1;
  }
  public String getAddr2() {
    return addr2;
  }
  public void setAddr2(String addr2) {
    this.addr2 = addr2;
  }
  public String getCity() {
    return city;
  }
  public void setCity(String city) {
    this.city = city;
  }
  public String getState() {
    return state;
  }
  public void setState(String state) {
    this.state = state;
  }
  public String getZip() {
    return zip;
  }
  public void setZip(String zip) {
    this.zip = zip;
  }
  public String getCountry() {
    return country;
  }
  public void setCountry(String country) {
    this.country = country;
  }
  public String getPhone() {
    return phone;
  }
  public void setPhone(String phone) {
    this.phone = phone;
  }
 
//这个类只用于传递值对象
}///~
 
 
 
package com.webshop.db;
import java.sql.*;
import com.webshop.domain.Account;
/**
 * @author 时金奎
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class AccountDAO {
 
 Connection con;//数据库连接
 String newAccount="insert into account values(?,?,?,?,?,?,?,?,?,?,?,?)";
    String signon="select * from account where userId=? and password=?";
    String selectAccountById="select * from account where userId=?";
 
 /**
  * 构造方法
  */
 public AccountDAO()
 {
 
 }
 
 /**
  * 增加新的帐号
  * @param Account
  * @return
  *  table_Info:
  *  userid varchar(80) not null,
     password varchar(25)  not null,
     email varchar(80) not null,
     name varchar(80) not null,
     status varchar(2),
     addr1 varchar(80) not null,
     addr2 varchar(40),
     city varchar(80) not  null,
     state varchar(80) not null,
     zip varchar(20) not null,
     country varchar(20) not null,
     phone varchar(80) not null,
  */
 public void createNewAccount(Account account)throws Exception
 {
  try
  {
   con=DatabaseConnection.getConnection();
  }
  catch(Exception e)
  {
   throw e;
  }
  try
  {
   PreparedStatement pstmt=con.prepareStatement(newAccount);
   pstmt.setString(1,account.getUserId());
   pstmt.setString(2,account.getPassword());
   pstmt.setString(3,account.getEmail());
   pstmt.setString(4,account.getName());
   pstmt.setString(5,"0");
   pstmt.setString(6,account.getAddr1());
   pstmt.setString(7,account.getAddr2());
   pstmt.setString(8,account.getCity());
   pstmt.setString(9,account.getState());
   pstmt.setString(10,account.getZip());
   pstmt.setString(11,account.getCountry());
   pstmt.setString(12,account.getPhone());
   pstmt.executeUpdate();
   con.close();
  }
  catch(SQLException e)
  {
   throw e;
  }
  finally
  {
   try
   {
    if(con!=null)
    con.close();
   }
   catch(Exception e2)
   {
   }
  }
 }
 
 /**
  * 根据用户的ID获得帐号信息
  */
 public Account getAccountById(String userId)throws Exception
 {
  Account account=null;
  try
  {
   con=DatabaseConnection.getConnection();
  }
  catch(Exception e)
  {
   throw e;
  }
  try
  {
   PreparedStatement pstmt=con.prepareStatement(selectAccountById);
   pstmt.setString(1,userId); 
   ResultSet rst=pstmt.executeQuery();
   if(rst.next())
   {
    account=new Account();
    account.setUserId(userId);
    account.setAddr1(rst.getString("addr1"));
    account.setAddr2(rst.getString("addr2"));
    account.setCity(rst.getString("city"));
    account.setState(rst.getString("state"));
    account.setZip(rst.getString("zip"));
    account.setCountry(rst.getString("country"));
    account.setName(rst.getString("name"));
    account.setEmail(rst.getString("email"));
    account.setPhone(rst.getString("phone"));
    account.setStatus(rst.getString("status"));
    account.setPassword(rst.getString("password"));
   }
   con.close();
  }
  catch(SQLException e)
  {
   throw e;
  }
  finally
  {
   try
   {
    if(con!=null)
    con.close();
   }
   catch(Exception e2)
   {
   }
  }
  return account;
 }
   
 /**
  * 用户登录,进行用户名和密码验证
  */
 public boolean signon(String userId,String password)throws Exception
 {
  boolean validate=false;
  try
  {
   con=DatabaseConnection.getConnection();
  }
  catch(Exception e)
  {
   throw e;
  }
  try
  {
   PreparedStatement pstmt=con.prepareStatement(signon);
   pstmt.setString(1,userId);
   pstmt.setString(2,password);
   ResultSet rst=pstmt.executeQuery();
   if(rst.next())
   {
    validate=true;
   }
   con.close();
  }
  catch(SQLException e)
  {
   throw e;
  }
  finally
  {
   try
   {
    if(con!=null)
    con.close();
   }
   catch(Exception e2)
   {
   }
  }
  return validate;
 }
   
 //关键是这个类进行操作数据库
}///~
 
//:NewAccountServlet.java  控制器类
package com.webshop.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.webshop.domain.Account;
import com.webshop.db.AccountDAO;

/**
 * @version  1.0
 * @author
 */
public class NewAccountServlet extends HttpServlet {
 /**
 * @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
 public void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
             doPost(req,resp);
 }
 /**
 * @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
 public void doPost(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
  String targetPage="/account/login.jsp";
 
  Account account=new Account();
  account.setUserId(req.getParameter("userId"));
        account.setPassword(req.getParameter("password"));
        account.setEmail(req.getParameter("email"));
        account.setName(req.getParameter("name"));
        account.setPhone(req.getParameter("phone"));
        account.setAddr1(req.getParameter("addr1"));
        account.setAddr2(req.getParameter("addr2"));
        account.setCity(req.getParameter("city"));
        account.setState(req.getParameter("state"));
        account.setZip(req.getParameter("zipCode"));
        account.setCountry(req.getParameter("country"));
      
        try
        {
         AccountDAO accountBean=new AccountDAO();
            accountBean.createNewAccount(account);
        }
        catch(Exception e)
        {
          e.printStackTrace();
          targetPage="/account/newUser.jsp";
          req.setAttribute("errorMessage","在创建新用户时出错!可能因为用户已经存在,请重新再试。错误信息="+e.getMessage()+"!");
        }
        javax.servlet.RequestDispatcher requestDispatcher = req.getRequestDispatcher(targetPage);
       requestDispatcher.forward(req,resp);
         return;
 }
}


login.jsp
 
<html><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><%@ page contentType="text/html;charset=gb2312"%>
<head>
<title>::用户登录:: </title>
<script language="JavaScript">
<!--
function check()
{
   if(document.form1.userId.value=="")
     {
    window.alert("请输入用户名!");
    document.form1.userId.focus();
    return false;
    }
    if(document.form1.password.value=="")
     {
    window.alert("请输入密码!");
    document.form1.password.focus();
    return false;
    }
 }
    -->
  </script>
 <%
  String errorMessage = (String)request.getAttribute("errorMessage");
 String targetPage=(String)request.getAttribute("targetPage");
 if(targetPage==null||"null".equals(targetPage))
 targetPage=request.getParameter("targetPage");
 %>
 <jsp:include page="/include/top.jspf" flush="true"/>
 <%
if ( errorMessage != null ) {
%>
<center><font color=red>
<%=errorMessage%></font>
</center>
<br>
<%
}
%>
<form action="/servlet/signonServlet" method="POST">
<input type=hidden name=targetPage value=<%=targetPage%>>
<table align="center" border="0">
<tr>
<td colspan="2">请输入用户名和密码.
<br />&nbsp;</td>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="userId" value="webshop" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" value="webshop" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="image" border="0" src="/images/button_submit.gif" /></td>
</tr>
</table>
</form>
<jsp:include page="/include/bottom.jspf" flush="true"/>
</html>
 
 
//login.jsp///
 
 
<%
session.invalidate();
response.sendRedirect("/index.jsp");
%>
 
//newUser.jsp
<html><meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<%@ page contentType="text/html;charset=gb2312"%>
<head>
<title>新用户注册 </title>
<jsp:include page="/include/top.jspf" flush="true"/>
<script language="JavaScript">
<!--
function check()
{
   if(document.form1.userId.value=="")
     {
    window.alert("请输入用户名名称!");
    document.form1.userId.focus();
    return false;
    }
    if(document.form1.country.value=="")
     {
    window.alert("请输入国别!");
    document.form1.country.focus();
    return false;
    }
    if(document.form1.name.value=="")
     {
    window.alert("请输入真实姓名!");
    document.form1.userId.focus();
    return false;
    }
     if(document.form1.addr1.value=="")
     {
    window.alert("请输入地址信息!");
    document.form1.userId.focus();
    return false;
    }
      if(document.form1.city.value=="")
     {
    window.alert("请输入城市信息!");
    document.form1.userId.focus();
    return false;
    }
     if(document.form1.state.value=="")
     {
    window.alert("请完整输入信息!");
    document.form1.userId.focus();
    return false;
    }
     if(document.form1.zipCode.value=="")
     {
    window.alert("请输入邮编信息!");
    document.form1.userId.focus();
    return false;
    }
     if(document.form1.phone.value=="")
     {
    window.alert("请完整输入信息!");
    document.form1.phone.focus();
    return false;
    }
     if(document.form1.email.value=="")
     {
    window.alert("请输入邮件地址信息!");
    document.form1.email.focus();
    return false;
    }
 
  if(document.form1.password.value!=document.form1.confirmpassword.value|document.form1.password.value=="")
     {
    window.alert("请正确输入密码!");
    document.form1.password.focus();
    return false;
    }
   
 return true;
}
//-->
</script>
<%
 String errorMessage = (String)request.getAttribute("errorMessage");
  String userId = request.getParameter("userId");
  if ( userId == null ) {
    userId = "";
  }
   String country = request.getParameter("country");
  if ( country == null ) {
    country = "";
  }
  String email = request.getParameter("email");
  if ( email == null ) {
    email = "";
  }
  String name = request.getParameter("name");
  if ( name == null ) {
    name = "";
  }
  String phone = request.getParameter("phone");
  if ( phone == null ) {
    phone = "";
  }
  String addr1 = request.getParameter("addr1");
  if ( addr1 == null ) {
    addr1 = "";
  }
   String addr2 = request.getParameter("addr2");
  if ( addr2 == null ) {
    addr2 = "";
  }
  String city = request.getParameter("city");
  if ( city == null ) {
    city = "";
  }
  String state = request.getParameter("state");
  if ( state == null ) {
    state = "";
  }
  String zipCode = request.getParameter("zipCode");
  if ( zipCode == null ) {
    zipCode = "";
  } 
%>
<%
if ( errorMessage != null ) {
%>
<span class="headingred"><font color=red>
<%=errorMessage%></font>
</span>
<br>
<%
}
%>
<form method="post" action="/servlet/newAccountServlet" name="form1">
  <TABLE class=font1 cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width="99%" align=center bgColor=#ffffff borderColorLight=#003686 border=1>
  <tr> 
  <td colspan=2><IMG height=35 alt="" src="/images/new_user.gif" width=160 border=0><br></td>
  </tr> 
  <tr><td colspan="6"><IMG height=10 alt="" src="/images/spacer.gif" width=1 border=0></td></tr>
 
  <tr>   
      <td>用户名:</td>
  <td><input size="15" name="userId" value="<%=userId%>"></input></td> 
  </tr>
 
  <tr><td colspan="6"><IMG height=3 alt="" src="/images/spacer.gif" width=1 border=0></td></tr>
  <tr> 
      <td>密码:</td>
  <td><input type="password" size="15" name="password"></input></td>
  </tr>
 
  <tr>
      <td>确认密码:</td>
  <td><input type = "password" size="15" name="confirmpassword"></input></td>
  </tr>
 
  <tr>
      <td>真实姓名:</td>
  <td><input size="15" name="name" value="<%=name%>"></input></td>
  </tr>
 
  <tr>
  <td>电话:</td>
  <td><input size="15" name="phone" value="<%=phone%>"></input></td>
  </tr>
 
  <tr>
  <td>email:</td>
  <td><input size="15" name="email" value="<%=email%>"></input></td>
  </tr>
 
  <tr>
      <td>地址1:</td>
  <td><input size="15" name="addr1" value="<%=addr1%>"></input></td>
  </tr>
 
  <tr>
      <td>地址2:</td>
  <td><input size="15" name="addr2" value="<%=addr2%>"></input></td>
  </tr>
 
  <tr>
      <td>城市:</td>
  <td><input size="15" name="city" value="<%=city%>"></input></td>
  </tr>
 
  <tr>
      <td>省:</td>
  <td><input size="15" name="state" value="<%=state%>"></input></td>
  </tr>
 
  <tr>
      <td>邮编:</td>
  <td><input size="15" name="zipCode" value="<%=zipCode%>"></input></td>
  </tr>
 
  <tr>
        <td>国别/地区:</td>
  <td><select name="country">
     <option value="中国大陆">中国大陆</option>
     <option value="中国香港">中国香港</option>
           <option value="美国">美国</option>
     </select>
        </td>
  </tr>
  
  <tr>
  <td colspan=2 align=center><input type="image" src="/images/submit.gif" name="submit" onClick="return check()"></input></td>   
  </tr> 
  </table>
 
</form>
<jsp:include page="/include/bottom.jspf" flush="true"/>

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值