JavaWeb 登陆操作和注册操作的实现

来源:互联网 发布:淘宝批量下架 编辑:程序博客网 时间:2024/06/05 09:05

User1.java

 

package cn.csdn.web.domain;

import java.io.Serializable;
import java.sql.Blob;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;

public class User1 implements Serializable {

 private int id;
 private String name;
 private String pass;
 private String sex;
 private Date birthday;
 private Timestamp nowday;
 private Double salary;
 private Blob photo;
 
 
 public User1() {
  super();
  // TODO Auto-generated constructor stub
 }
 
 
 public User1(int id, String name, String pass, String sex, Date birthday,
   Timestamp nowday, Blob photo) {
  super();
  this.id = id;
  this.name = name;
  this.pass = pass;
  this.sex = sex;
  this.birthday = birthday;
  this.nowday = nowday;
  this.photo = photo;
 }


 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getPass() {
  return pass;
 }
 public void setPass(String pass) {
  this.pass = pass;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }
 public Date getBirthday() {
  return birthday;
 }
 public void setBirthday(Date birthday) {
  this.birthday = birthday;
 }
 public Timestamp getNowday() {
  return nowday;
 }
 public void setNowday(Timestamp nowday) {
  this.nowday = nowday;
 }
 
 
 
 public Double getSalary() {
  return salary;
 }


 public void setSalary(Double salary) {
  this.salary = salary;
 }


 public Blob getPhoto() {
  return photo;
 }
 public void setPhoto(Blob photo) {
  this.photo = photo;
 }
 
 
 
}

 

Dao.java

 

package cn.csdn.web.dao;

import cn.csdn.web.domain.User1;

public interface Dao {

 
 boolean login(String name,String pass);
 boolean register(User1 user1);
}

 

DaoImpl.java

 

package cn.csdn.web.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.csdn.web.domain.User1;

public class DaoImpl implements Dao {

 private static Connection conn=null;
 private PreparedStatement pstmt= null;
 private ResultSet rs=null;
 
  private static String url="jdbc:mysql://localhost:3306/3g?user=root&password=1234&useUnicode=true&characterEncoding=UTF8";
 
 static {
  
  try {
   Class.forName("com.mysql.jdbc.Driver");
   
   try {
    conn= DriverManager.getConnection(url);
   } catch (SQLException e) {
   
    e.printStackTrace();
   }
   
   
   
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  
 }
 
 
 public boolean login(String name, String pass) {
  // TODO Auto-generated method stub
  
  boolean flag=false;
  
  String sql="select id,name,pass from user1 where name=? and pass=?";
  
     try {
   pstmt= conn.prepareStatement(sql);
   
   int index=1;
   pstmt.setObject(index++, name);
   pstmt.setObject(index++,pass);
   
   
   rs=  pstmt.executeQuery();
   
   if(rs.next()){
    
    flag=true;
   }
   
   
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   
   
   release(rs,pstmt);
  }
  
  
  
  
  
  
  
  return flag;
  
 }


 private void release(ResultSet rs, PreparedStatement pstmt) {
  // TODO Auto-generated method stub
  
  try {
   rs.close();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  try {
   pstmt.close();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  
 }


 public boolean register(User1 user1) {
  // TODO Auto-generated method stub
  boolean flag=false;
 String  sql="insert into user1 (id,name,pass,sex,birthday,nowday,salary,photo) values(?,?,?,?,?,?,?,?)";
 try {
  pstmt= conn.prepareStatement(sql);
   int index=1;
   pstmt.setObject(index++,user1.getId());
   pstmt.setObject(index++,user1.getName());
   pstmt.setObject(index++,user1.getPass());
   pstmt.setObject(index++,user1.getSex());
   pstmt.setObject(index++,user1.getBirthday());
   pstmt.setObject(index++,user1.getNowday());
   pstmt.setObject(index++,user1.getSalary());
   pstmt.setObject(index++,user1.getPhoto());
  
  int i= pstmt.executeUpdate();
  if(i>0){
   
   flag=true;
  }
  
  
  
  
  
  
  
 } catch (SQLException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }finally{
  if(pstmt!=null){
   try {
    pstmt.close();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
  
  
  
  
  
  return flag;
 }

}

MyServlet.java

 

package cn.csdn.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.csdn.web.dao.Dao;
import cn.csdn.web.dao.DaoImpl;

public class MyServlet extends HttpServlet {
 
 Dao dao= new DaoImpl();
 
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
           
  doPost(request, response);
 }

 
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  request.setCharacterEncoding("utf-8");
  
  
  
   String uname=request.getParameter("uname");
   String upass=request.getParameter("upass");
  
  boolean flag= dao.login(uname, upass);
  
  if(flag){
   
   response.sendRedirect("success.jsp");
  }else{
   response.sendRedirect("error.jsp");
  }
  
   
  String username=request.getParameter("uname");
  
  
  
  
  
  
  
  
  
  
  
  
 }

}

RegisterServlet.java

package cn.csdn.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.csdn.web.dao.Dao;
import cn.csdn.web.dao.DaoImpl;
import cn.csdn.web.domain.User1;

public class RegisterServlet extends HttpServlet {

 Dao dao= new DaoImpl();
 
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
       doPost(request, response);
  
 }


 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
           
  request.setCharacterEncoding("UTF-8");
  
 String name= request.getParameter("uname");
 String password= request.getParameter("upass");
 String sex=  request.getParameter("sex");
  String birthday1=request.getParameter("birthday");
  String nowday=request.getParameter("nowday");
 String salary1 = request.getParameter("salary1");
  
  
  Double salary = Double.valueOf(salary1);
  
  DateFormat dateformat= DateFormat.getDateInstance();
  Date birthday=null;
  try {
   birthday=  dateformat.parse(birthday1);
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  User1 user1= new User1();
  
  
  user1.setName(name);
  user1.setPass(password);
  user1.setSex(sex);
  user1.setBirthday(null);
  user1.setNowday(new Timestamp(System.currentTimeMillis()));
  
  user1.setSalary(salary);
  user1.setPhoto(null);
  
  boolean flag = dao.register(user1);
  
  if(flag){
   
   System.out.println("注册成功");
  }else{
   
   System.out.println("注册失败");
  }
  
  
  
  
  
  
  
  
  
  
  
  
 
 }

}