日期: 2016-9-1
内容: 使用Struts2实现用户信息的CRUD操作
CREATE TABLE `userinfo` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`name` varchar(10) NOT NULL,
`age` int(3) NOT NULL,
`sex` varchar(2) NOT NULL,
`telephone` varchar(15) DEFAULT NULL,
`email` varchar(30) DEFAULT NULL,
`specialty` varchar(20) DEFAULT NULL COMMENT '专业',
`school` varchar(20) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
BaseDao.java:
package com.onlyone.struts.managerdemo.dao;
import java.sql.DriverManager;
import java.sql.ResultSet;
import com.mysql.jdbc.Connection;
//import com.mysql.jdbc.PreparedStatement;
import java.sql.PreparedStatement;
/**
* 数据库操作父类:连接和关闭数据库
* @author peixun
*
*/
public class BaseDao {
/**
* 获得数据库连接
* @return
*/
public Connection getConnextion()
{
//初始化一个数据库连接
Connection conn = null;
try{
//加载Mysql数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//与数据库建立连接
conn = (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/user", "root", "910214");
}catch(Exception ex){
//打印异常堆栈
ex.printStackTrace();
}
//返回数据库连接
return conn;
}
/**
* 关闭数据库相关的连接
* @param conn 数据库连接
* @param pstmt 编译执行SQL对象
* @param set 结果集
*/
public void closeAllConnection(Connection conn, PreparedStatement pstmt,ResultSet rs)
{
if(rs != null)
{
try{
//关闭结果集对象
rs.close();
}catch(Exception ex)
{
ex.printStackTrace();
}
}
if(pstmt != null)
{
try{
pstmt.close();//关闭编译执行SQL对象
}catch(Exception ex)
{
ex.printStackTrace();
}
}
if(conn != null)
{
try{
conn.close();//关闭数据库连接对象
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
User.java:
package com.onlyone.struts.managerdemo.bean;
/**
* 实现一个JavaBean
* @author peixun
*
*/
public class User {
//用户编号id
private int id;
//用户名
private String name;
//用户年龄
private int age;
//用户性别
private String sex;
//用户手机
private String telephone;
//用户邮箱
private String email;
//用户专业
private String specialty;
//用户学校
private String school;
//用户地址
private String address;
//实现getter和setter
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSpecialty() {
return specialty;
}
public void setSpecialty(String specialty) {
this.specialty = specialty;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
//无参数的构造函数
public User()
{
}
//全参数的构造函数
public User(int id, String name, int age, String sex, String telephone,
String email, String specialty, String school, String address) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.telephone = telephone;
this.email = email;
this.specialty = specialty;
this.school = school;
this.address = address;
}
//重写toString()
@Override
public String toString() {
return "User [id=" + id + "\n name=" + name + "\n age=" + age + "\n sex="
+ sex + "\n telephone=" + telephone + "\n email=" + email
+ "\n specialty=" + specialty + "\n school=" + school
+ "\n address=" + address + "]";
}
}
UserDao.java接口:
package com.onlyone.struts.managerdemo.dao;
import java.util.List;
import com.onlyone.struts.managerdemo.bean.User;
/**
* 用户操作接口:定义用户的基本操作
* @author peixun
*
*/
public interface UserDao {
//查询出所有记录
public List getAllUsers();
//根据主键查询出一条已有的记录
public User getUserById(int uid);
//添加一个用户
public int addUser(User user);
//更新用户:按照主键修改
public int updateUser(User user);
//删除用户:通过主键删除
public int deleteUser(int uid);
}
UserDaoImpl.java:
package com.onlyone.struts.managerdemo.dao;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.onlyone.struts.managerdemo.bean.User;
public class UserDaoImpl extends BaseDao implements UserDao {
//创建数据库连接
Connection conn = null;
//编译执行SQL对象创建
java.sql.PreparedStatement pstmt = null;
//获得查询结果集对象
ResultSet rs = null;
@Override
public List getAllUsers() {
List list = new ArrayList();
//查询所有用户SQL文
String sql = " select * from userinfo order by id ";
//执行查询操作
try{
//获得数据库连接
conn = this.getConnextion();
//编译执行SQL文
pstmt = conn.prepareStatement(sql);
//或的查询结果集
rs = pstmt.executeQuery();
User user = null;
//循环获得用户信息
while(rs.next()){
//获得用户实例
user = new User();
//添加用户信息
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setEmail(rs.getString("email"));
user.setAddress(rs.getString("address"));
user.setSchool(rs.getString("school"));
user.setSex(rs.getString("sex"));
user.setSpecialty(rs.getString("specialty"));
user.setTelephone(rs.getString("telephone"));
//将用户信息添加在list中
list.add(user);
}
}catch(Exception ex)
{
//打印异常堆栈
ex.printStackTrace();
}finally{
//关闭和数据库操作相关的所有连接
this.closeAllConnection(conn, pstmt, rs);
}
//返回用户信息
return list;
}
//根据用户id查询用户信息
@Override
public User getUserById(int uid) {
User user = null;
//查询SQL
String sql = " select * from userinfo where id = "+uid;
try{
//获得数据库连接
conn = this.getConnextion();
//编译执行sql文
pstmt = conn.prepareStatement(sql);
//获查询结果集
rs = pstmt.executeQuery();
if(rs.next()){
//实例化用户信息
user = new User();
//添加用户信息
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setEmail(rs.getString("email"));
user.setAddress(rs.getString("address"));
user.setSchool(rs.getString("school"));
user.setSex(rs.getString("sex"));
user.setSpecialty(rs.getString("specialty"));
user.setTelephone(rs.getString("telephone"));
}
}catch(Exception ex)
{
//打印异常堆栈
ex.printStackTrace();
}finally{
//关闭所有连接
this.closeAllConnection(conn, pstmt, rs);
}
return user;
}
@Override
public int addUser(User user) {
//定义返回结果
int result = 0;
//编写添加信息SQL文
String sql = " insert into userinfo(name,sex,age,telephone,email,specialty,school,address) values(?,?,?,?,?,?,?,?) ";
try{
//获得数据库连接
conn = this.getConnextion();
pstmt = conn.prepareStatement(sql);
//预编译处理
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getSex());
pstmt.setInt(3, user.getAge());
pstmt.setString(4, user.getTelephone());
pstmt.setString(5, user.getEmail());
pstmt.setString(6, user.getSpecialty());
pstmt.setString(7, user.getAddress());
//执行更新
result = pstmt.executeUpdate();
//查询是不是有添加成功
if(result != 0)
{
System.out.println("添加了一条用户信息!");
}
}catch(Exception ex)
{
//打印异常堆栈
ex.printStackTrace();
}
finally{
//关闭和数据库相关的连接
this.closeAllConnection(conn, pstmt, rs);
}
return result;
}
//修改用户信息
@Override
public int updateUser(User user) {
int result;
//修改sql文:根据用户id去查询用户信息
String sql = " update userinfo set name=?,sex=?,age=?,telephone=?,email=?,specialty=?,school=?,address=? where id ="+user.getId();
try
{
//获得数据库连接
conn = this.getConnextion();
//编译执行sql文
pstmt = conn.prepareStatement(sql);
//预编译处理
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getSex());
pstmt.setInt(3, user.getAge());
pstmt.setString(4, user.getTelephone());
pstmt.setString(5, user.getEmail());
pstmt.setString(6, user.getSpecialty());
pstmt.setString(7, user.getSchool());
pstmt.setString(8, user.getAddress());
//执行修改
result = pstmt.executeUpdate();
if(result != 0)
{
System.out.println("你修改了一条记录!");
}
}
catch(Exception ex)
{
//打印异常堆栈
ex.printStackTrace();
}
finally{
//关闭数据库连接
this.closeAllConnection(conn, pstmt, rs);
}
return 0;
}
//根据用户id删除数据库用户信息
@Override
public int deleteUser(int uid) {
int result = 0;
//删除用户信息sql
String sql = " delete from userinfo where id="+uid;//之前编写错误,这里应该使用占位符?,然后使用设值进行值得摄入
//修改之后 String sql = " delete from userinfo where id=?";//修改日期:2016-9-2
//执行删除try{//获得数据库连接conn = this.getConnextion();//编译执行pstmt = conn.prepareStatement(sql);pstmt.setInt(1, uid);//编译执行获得结果result = pstmt.executeUpdate();if(result != 0){System.out.println("你删除了一条记录!");}}catch(Exception ex){ex.printStackTrace();}finally{this.closeAllConnection(conn,pstmt,rs);}return result;}}
业务层接口UserBiz.java:
package com.onlyone.struts.managerdemo.biz;
import java.util.List;
import com.onlyone.struts.managerdemo.bean.User;
/**
* 数据库业务层接口:主要完成调用UserDao
* @author peixun
*
*/
public interface UserBiz {
//查询所有用户
public List getAllUsers();
//根据用户id获取用户信息
public User getUserById(int id);
//添加用户
public int addUser(User user);
//修改用户
public int updateUser(User u);
//删除用户
public int deleteUser(int id);
}
业务层接口实现类:UserBizImpl.java:
package com.onlyone.struts.managerdemo.biz;
import java.util.List;
import com.onlyone.struts.managerdemo.bean.User;
import com.onlyone.struts.managerdemo.dao.UserDao;
import com.onlyone.struts.managerdemo.dao.UserDaoImpl;
public class UserBizImpl implements UserBiz {
//获得userDao的用户实例:假如使用的是SSH框架的话,这一步应该是使用@Autowired注入UserDao的,而不是直接new一个对象,这样效率会比较低
UserDao userDao = new UserDaoImpl();
@Override
public List getAllUsers() {
return userDao.getAllUsers();
}
@Override
public User getUserById(int id) {
return userDao.getUserById(id);
}
@Override
public int addUser(User user) {
return userDao.addUser(user);
}
@Override
public int updateUser(User u) {
return userDao.updateUser(u);
}
@Override
public int deleteUser(int id) {
return userDao.deleteUser(id);
}
}
表现层实现:UserAction.java:
package com.onlyone.struts.managerdemo.action;
import java.util.List;
import com.onlyone.struts.managerdemo.bean.User;
import com.onlyone.struts.managerdemo.biz.UserBiz;
import com.onlyone.struts.managerdemo.biz.UserBizImpl;
/**
* 用户业务逻辑处理Action
* @author peixun
*
*/
public class UserAction {
//用户信息列表
private List userList;
public List getUserList() {
return userList;
}
public void setUserList(List userList) {
this.userList = userList;
}
//检索用户信息:获得所有用户数据
public String getUsers()
{
//获得业务层操作权限
UserBiz userBiz = new UserBizImpl();
//调用业务层操作查询所有用户信息
userList = userBiz.getAllUsers();
//返回业务逻辑视图
return "userList";
}
}
struts.xml配置文件配置:
/jsp/userinfo/userList.jsp
UserList.jsp画面设计:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page import="com.onlyone.struts.managerdemo.dao.*,com.onlyone.struts.managerdemo.bean.User" %>
用户信息一览
用户信息列表
增加用户
用户ID
姓名
性别
年龄
电话
学校
删除
修改
删除
有图有真相,查询用户信息成功!