问题描述
- jsp tomcat 注册为什么总出现这个问题
-
register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">注册
注册
<!-- action 提供servlet地址-->
登录名: 密码: 密码确认: 邮箱地址:
解决方案
package com.DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.pojo.User;
public class RegisterImpl implements RegisterDao {
private Connection conn = null; // 数据库连接对象
private PreparedStatement pstmt = null; // 数据库操作对象
// 通过构造方法取得数据库连接
public RegisterImpl(Connection conn) {
this.conn = conn;
}
public boolean addUser(User user) throws Exception {
boolean flag = false; // 定义标识
String sql = "insert into user(username,passwd.confirmwd,email) values(?,?,?,?)";
this.pstmt = this.conn.prepareStatement(sql);// 实例化PrepareStatement对象
this.pstmt.setString(1,user.getUsername());// 设置
this.pstmt.setString(2,user.getPasswd());// 设置
this.pstmt.setString(3,user.getConfirmwd());// 设置
this.pstmt.setString(4,user.getEmail());// 设置
if (this.pstmt.executeUpdate() > 0) { // 更新记录的行数大于0
flag = true; // 修改标识
}
this.pstmt.close(); // 关闭PreparedStatement操作
return flag;
}
/*public List<User> findAll(String username) throws Exception {
List<User> list = new ArrayList<User>();// 定义集合,接受返回的数据
String sql = "select product_id,product_name,price,info from product ";
if (username != null && !"".equals(username)) {
sql = "select username,passwd,confirmwd,email from product where product_name like? ";
this.pstmt.setString(1, "%" + username + "%");// 设置查询产品名称
} else {
this.pstmt = this.conn.prepareStatement(sql);// 实例化PreparedStatement
}
ResultSet rs = this.pstmt.executeQuery();// 执行查询操作
User user = null;
while (rs.next()) {
user = new User();// 实例化新的product对象
product.setProduct_id(rs.getString(1));
product.setProduct_name(rs.getString(2));
product.setPrice(rs.getDouble(3));
product.setInfo(rs.getString(4));
list.add(product); // 向集合中增加product对象
}
this.pstmt.close();
return list; // 返回全部结果
}*/
public User findByUsername(String username) throws Exception {
User user = null;
String sql = "select username,passwd,confirmwd,email from user where username=?";
this.pstmt = this.conn.prepareStatement(sql);
this.pstmt.setString(1, username); // 设置产品编号
ResultSet rs = this.pstmt.executeQuery();
if (rs.next()) {
user = new User();
user.setUsername(rs.getString(1));
user.setPasswd(rs.getString(2));
user.setConfirmwd(rs.getString(3));
user.setEmail(rs.getString(4));
}
this.pstmt.close();
return user; // 如果查询不到结果则返回null,默认值为null
}
}
解决方案二:
insert.jsp
<%@ page import="java.util.*,com.pojo.User" pageEncoding="UTF-8"%>
<%@ page import="com.factory.DAOFactory" %>
<%
request.setCharacterEncoding("utf-8");//
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
注册结果
<%
User user = new User(); //
user.setUsername(request.getParameter("username"));
user.setPasswd(request.getParameter("passwd"));
user.setConfirmwd(request.getParameter("confirmwd"));
user.setEmail(request.getParameter("email"));
boolean flag = DAOFactory.getIEmpDAOInstance().
addUser(user); //
if(flag){
%>
注册信息成功
<%
}else{
%>
注册失败
<%} %>
解决方案三:
package com.DAO;
import java.util.List;
import com.pojo.User;
public interface RegisterDao {
/**
* 数据库 新增数据
@param product 要增加的数据对象;
*@return 是否增加成功的标记
*@throws Exception 如果有异常交直接抛出
*/
public boolean addUser(User user)throws Exception ;
/*
* 查询全部的Product数据
@param product_name 产品名称
*@return 返回全部的查询结果,每一个product对象表示表的一行记录
*@throws Exception 如果有异常交直接抛出
*/
// public List findAll(String username)throws Exception;
/*
* 根据产品编号查询产品
*@param product_id 产品编号
*@return 产品的vo对象
*@throws Exception 如果有异常交直接抛出
*/
public User findByUsername(String username)throws Exception;
}
解决方案四:
package com.service;
import java.util.List;
import com.DAO.RegisterDao;
import com.DAO.RegisterImpl;
import com.db.DBConnect;
import com.pojo.User;
public class RegisterService implements RegisterDao{
private DBConnect dbconn = null; // 定义数据库连接类
private RegisterDao dao = null; // 声明DAO对象
// 在构造方法中实例化数据库连接,同时实例化dao对象
public RegisterService() throws Exception {
this.dbconn = new DBConnect();
this.dao = new RegisterImpl(this.dbconn.getConnection());// 实例化ProductDao的实现类
}
public boolean addUser(User user) throws Exception {
boolean flag = false; // 标识
try {
if (this.dao.findByUsername(user.getUsername()) == null) {// 如果要插入的产品编号不存在
flag = this.dao.addUser(user);// 新增一条产品信息
}
} catch (Exception e) {
throw e;
} finally {
this.dbconn.close();
}
return flag;
}
/*public List<Product> findAll(String keyWord) throws Exception {
List<Product> all = null; // 定义产品返回的集合
try {
all = this.dao.findAll(keyWord);// 调用实现方法
} catch (Exception e) {
throw e;
} finally {
this.dbconn.close();
}
return all;
}
*/
public User findByUsername(String username) throws Exception {
User user = null;
try {
user = this.dao.findByUsername(username);
} catch (Exception e) {
throw e;
} finally {
this.dbconn.close();
}
return user;
}
}
解决方案五:
看不懂。。。好厉害的样子,,,,