该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
ClientDao.java
package com.jim.mystore.dao;
import java.sql.*;
import com.jim.mystore.dbc.DatabaseConnection;
import com.jim.mystore.model.Client;
public class ClientDao {
private DatabaseConnection dbc = null;
private Connection conn = null;
private PreparedStatement pstmt = null;
public ClientDao(){
conn = dbc.getConnection();
}
public boolean doInsert(Client cli)throws Exception{
boolean flag = false;
try{
String sql = "insert into client (name,password,phonenumber,emailaddress,regdate) values(?,?,?,?,NOW())";
this.pstmt = this.conn.prepareStatement(sql);
this.pstmt.setString(1,cli.getName());
this.pstmt.setString(2,cli.getPassword());
this.pstmt.setString(3,cli.getPhonenumber());
this.pstmt.setString(4,cli.getEmailAddres());
if(this.pstmt.executeUpdate()>0){
flag = true;
}
} catch (SQLException e){
e.printStackTrace();
} finally {
try{
this.pstmt.close();
this.conn.close();
} catch(SQLException e ){
e.printStackTrace();
}
}
return flag;
}
public ResultSet findAll(){
ResultSet rs = null;
try{
String sql = "select name from client order by name asc";
this.pstmt = this.conn.prepareStatement(sql);
rs = this.pstmt.executeQuery();
} catch(SQLException e){
e.printStackTrace();
} finally{
try{
this.pstmt.close();
this.conn.close();
} catch(SQLException e){
e.printStackTrace();
}
}
return rs;
}
public ResultSet findOne(String cn){
ResultSet rs = null;
try{
String sql = "select * from client where name=?";
this.pstmt = this.conn.prepareStatement(sql);
this.pstmt.setString(1, cn);
rs = this.pstmt.executeQuery();
} catch(SQLException e){
e.printStackTrace();
} finally{
try{
this.pstmt.close();
this.conn.close();
} catch(SQLException e){
e.printStackTrace();
}
}
return rs;
}
}
DatabaseConnection.java
package com.jim.mystore.dbc;
import java.sql.Connection;
import java.sql.DriverManager;
public class DatabaseConnection {
private Connection conn = null;
public DatabaseConnection(){
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost/mystore","root", "jim1");
} catch(Exception e){
e.printStackTrace();
}
}
public Connection getConnection(){
return this.conn;
}
public void close(){
if(this.conn!=null){
try{
this.conn.close();
} catch(Exception e){
e.printStackTrace();
}
}
}
}
ClientService.java
package com.jim.mystore.service;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jim.mystore.model.Client;
import com.jim.mystore.dao.ClientDao;
public class ClientService {
private ClientDao cd = null;
public ClientService(){
this.cd = new ClientDao();
}
public boolean doInsert(HttpServletRequest request, HttpServletResponse response)throws Exception{
Client cli = new Client();
try{
cli.setName(request.getParameter("name"));
cli.setPassword(request.getParameter("password"));
cli.setEmailaddress(request.getParameter("emailaddress"));
cli.setPhonenumber(request.getParameter("phonenumber"));
} catch(Exception e){
e.printStackTrace();
}
return cd.doInsert(cli);
}
public List findAll()throws Exception{
List all = new ArrayList();
ResultSet rs = null;
rs = cd.findAll();
Client cli = null;
try{
while(rs.next()){
cli = new Client();
cli.setName(rs.getString(1));
all.add(cli);
}
rs.close();
} catch(SQLException e){
e.printStackTrace();
}
return all;
}
public Client findOne(String cn)throws Exception{
Client cli = null;
ResultSet rs = null;
rs = cd.findOne(cn);
if(rs.next()){
cli = new Client();
cli.setId(rs.getInt(1));
cli.setName(rs.getString(2));
cli.setPassword(rs.getString(3));
cli.setPhonenumber(rs.getString(4));
cli.setEmailaddress(rs.getString(5));
cli.setAddress(rs.getString(6));
cli.setBalance(rs.getDouble(7));
cli.setRegdate(rs.getString(8));
}
rs.close();
return cli;
}
}
LoginCheckServlet.java
package com.jim.mystore.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.jim.mystore.model.Client;
import com.jim.mystore.service.ClientService;
/**
* Servlet implementation class LoginCheck
*/
@WebServlet("/Client/LoginCheckServlet")
public class LoginCheckServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Client cli = null;
ClientService cs = new ClientService();
public LoginCheckServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String pw = request.getParameter("password");
String pw2 = null;
String ref = null;
try{
this.cli = cs.findOne(name);
if(cli != null){
pw2 = cli.getPassword();
if(!pw2.equals(pw)){
ref = "用户名不存在或者密码错误!";
request.setAttribute("ref", ref);
request.getRequestDispatcher("Login.jsp").forward(request,response);
} else {
HttpSession session = request.getSession();
session.setAttribute("name", name);
session.setAttribute("password", pw2);
request.getRequestDispatcher("loginsuccess.jsp").forward(request,response);
}
} else {
ref = "此用户不存在!";
request.setAttribute("ref", ref);
request.getRequestDispatcher("Login.jsp").forward(request,response);
}
} catch(Exception e){
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
分包情况
跪求各位大神指教。。。我看了一个下午都找不到错误。。。