以下操作只针对本案例,仅供参考!
一、数据库环境搭建
二、环境搭建
1、开发包
2、连接池配置文件
name="driverClass">com.mysql.jdbc.Driver name="jdbcUrl">jdbc:mysql:///casedemo name="user">casedemo name="password">123
3、创建好所需要的包
在utils包里有一个C3p0Utils工具类
package cn.usermanager.utils; import com.mchange.v2.c3p0.ComboPooledDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 数据源的工具类 */ public class C3p0Utils { /** * 创建私有静态数据源成员变量 */ private static ComboPooledDataSource ds = new ComboPooledDataSource(); /** * 创建公有的得到数据源的方法 * * @return */ public static DataSource getDataSource() { return ds; } /** * 创建得到连接对象的方法 * * @return */ public static Connection getConnection() { try { return ds.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return null; } /** * 创建释放资源的方法 * @param conn * @param stmt * @param rs */ public static void close(Connection conn, Statement stmt, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(Connection conn, Statement stmt) { close(conn, stmt, null); } public static void main(String[] args) { System.out.println(getConnection()); } }
在domain包里创建实体类User
package cn.usermanager.domain; public class User { private int id; private String name; private String sex; private String address; private String email; private String qq; private int age; 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 getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getQq() { return qq; } public void setQq(String qq) { this.qq = qq; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
3、导入所需要的页面原型
4、add.jsp form表单添加action="addUserServlet"
5、AddUserServlet.java接受表单用户信息
package cn.usermanager.web; import cn.usermanager.domain.User; 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 java.io.IOException; @WebServlet("/addUserServlet") public class AddUserServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); //1、请求参数的封装 TO对象中 String name = request.getParameter("name"); String sex = request.getParameter("sex"); String address = request.getParameter("address"); String email = request.getParameter("email"); String qq = request.getParameter("qq"); String age = request.getParameter("age"); User user = new User(); user.setName(name); user.setSex(sex); user.setAddress(address); user.setEmail(email); user.setQq(qq); user.setAge(Integer.parseInt(age)); //2、调用业务层 UserService userService = new UserServiceImpl(); User existUser = userService.addUser(user); //3、根据existUser ==null ?跳转到添加用户成功页面:添加页面+错误信息 if (existUser == null) { response.sendRedirect("add_ok.jsp"); }else{ //email 冲突 request.setAttribute("","邮箱已存在"); request.getRequestDispatcher("/add.jsp").forward(request,response); } } }
6、service接口和service实现类编写
package cn.usermanager.service.impl; import cn.usermanager.domain.User; import cn.usermanager.service.UserService; public class UserServiceImpl implements UserService { private UserDao userDao = new UserDaoImpl(); @Override public User addUser(User user) { //业务逻辑 先根据邮箱查询 结果 再去做Insert操作 User existUser = null; try { existUser = userDao.findUserByEmail(user.getEmail()); } catch (Exception e) { System.out.println("可以注册......dao实现添加用户操作"); userDao.addUser(user); } return existUser; } }
package cn.usermanager.service; import cn.usermanager.domain.User; public interface UserService { User addUser(User user); }
7、dao接口和dao实现类的编写
package cn.usermanager.dao.impl; import cn.usermanager.dao.UserDao; import cn.usermanager.domain.User; import cn.usermanager.utils.C3p0Utils; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; public class UserDaoImpl implements UserDao { @Override public User findUserByEmail(String email) { JdbcTemplate template = new JdbcTemplate(C3p0Utils.getDataSource()); String sql = "select * from tab_user where email=?"; User user = template.queryForObject(sql, new BeanPropertyRowMapper(User.class), email); return user; } @Override public void addUser(User user) { JdbcTemplate template = new JdbcTemplate(C3p0Utils.getDataSource()); String sql = "insert into tab_user values(null,?,?,?,?,?,?)"; template.update(sql, user.getName(), user.getSex(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail()); } }
package cn.usermanager.dao; import cn.usermanager.domain.User; public interface UserDao { User findUserByEmail(String email); void addUser(User user); }
add_ok.jsp页面完善
<%@ page contentType="text/html;charset=UTF-8" language="java" %> html> lang="zh-CN"> charset="utf-8"> http-equiv="X-UA-Compatible" content="IE=edge">添加联系人页面 恭喜你,添加员工成功! href="queryAllUserServlet">查询员工列表 href="add.jsp">继续添加。。。
8、效果图
邮箱相同
数据库用户数据已添加