当前位置: 代码迷 >> Java Web开发 >> 将一个对人员增删改查的程序改成Hibernate+spring+struts方式解决思路
  详细解决方案

将一个对人员增删改查的程序改成Hibernate+spring+struts方式解决思路

热度:113   发布时间:2016-04-17 10:16:58.0
将一个对人员增删改查的程序改成Hibernate+spring+struts方式
Java code
package com.experiment.Dao;import java.util.ArrayList;import java.util.List;import com.experiment.vo.UserInfo;public interface UserManagerDao {    public void insert(UserInfo user);    public void updateById(UserInfo userInfo);    public void deleteById(int id);    public UserInfo queryByAccount(String account);    public ArrayList<UserInfo> queryAll();       public UserInfo queryById(int id);}[color=#FF0000]package com.experiment.Dao.DapImpl;[/color]import java.io.UnsupportedEncodingException;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import com.experiment.Dao.UserManagerDao;import com.experiment.Db.DataBaseConnectionOracle;import com.experiment.vo.UserInfo;public class UserManagerDaoImpl implements UserManagerDao {    public void insert(UserInfo user) {        String sql = "INSERT INTO usertable (id,account,name,birthday,sex) VALUES (?,?,?,?,?)";        PreparedStatement pstmt = null;        DataBaseConnectionOracle dbc = null;        try {    dbc = new DataBaseConnectionOracle();            pstmt = dbc.getConnection().prepareStatement(sql);            pstmt.setInt(1, user.getId());            pstmt.setString(2, user.getAccount());            try { pstmt.setString(3, new String(user.getName().getBytes(),                                "UTF-8"));            } catch (UnsupportedEncodingException e) {                e.printStackTrace(); }            pstmt.setString(4, user.getBirthday());            pstmt.setInt(5, user.getSex());            pstmt.executeUpdate();            pstmt.close();        } catch (SQLException e) {            e.printStackTrace();        } finally {            dbc.close();        }}    public void deleteById(int id) {        String sql = "DELETE FROM usertable WHERE id=?";        PreparedStatement pstmt = null;        DataBaseConnectionOracle dbc = null;        try {            dbc = new DataBaseConnectionOracle();            pstmt = dbc.getConnection().prepareStatement(sql);            pstmt.setInt(1, id);            pstmt.executeUpdate();            pstmt.close();        } catch (Exception e) {            e.printStackTrace();        } finally {            dbc.close();        }}    public ArrayList<UserInfo> queryAll() {        ArrayList<UserInfo> all = new ArrayList<UserInfo>();        String sql = "SELECT id,account,name,birthday,sex FROM usertable";        PreparedStatement pstmt = null;        DataBaseConnectionOracle dbc = null;        try {    dbc = new DataBaseConnectionOracle();            pstmt = dbc.getConnection().prepareStatement(sql);            ResultSet rs = pstmt.executeQuery();            while (rs.next()) {                UserInfo userInfo = new UserInfo();                userInfo.setId(rs.getInt(1));                userInfo.setAccount(rs.getString(2));                userInfo.setName(rs.getString(3));                userInfo.setBirthday(rs.getString(4));                userInfo.setSex(rs.getShort(5));                all.add(userInfo);            }rs.close();            pstmt.close();        } catch (Exception e) {            e.printStackTrace();        } finally {            dbc.close();}        return all;    }public UserInfo queryById(int id) {        UserInfo userInfo = null;        String sql = "SELECT id,account,name,birthday,sex FROM usertable WHERE id=?";        PreparedStatement pstmt = null;        DataBaseConnectionOracle dbc = null;        try {    dbc = new DataBaseConnectionOracle();            pstmt = dbc.getConnection().prepareStatement(sql);            pstmt.setInt(1, id);            ResultSet rs = pstmt.executeQuery();            if (rs.next()) {                userInfo = new UserInfo();                userInfo.setId(rs.getInt(1));                userInfo.setAccount(rs.getString(2));                userInfo.setName(rs.getString(3));                userInfo.setBirthday(rs.getString(4));                userInfo.setSex(rs.getInt(5));            }rs.close();            pstmt.close();        } catch (Exception e) {            e.printStackTrace();        } finally {            dbc.close();        }        return userInfo;    }public UserInfo queryByAccount(String account) {        UserInfo userInfo = null;        String sql = "SELECT id,account,name,birthday,sex FROM usertable WHERE account=?";        PreparedStatement pstmt = null;        DataBaseConnectionOracle dbc = null;        try {    dbc = new DataBaseConnectionOracle();            pstmt = dbc.getConnection().prepareStatement(sql);            pstmt.setString(1, account);            ResultSet rs = pstmt.executeQuery();            if (rs.next()) {                userInfo = new UserInfo();                userInfo.setId(rs.getInt(1));                userInfo.setAccount(rs.getString(2));                userInfo.setName(rs.getString(3));                userInfo.setBirthday(rs.getString(4));                userInfo.setSex(rs.getInt(5));            }            rs.close();            pstmt.close();        } catch (Exception e) {            e.printStackTrace();        } finally {            dbc.close();        }return userInfo;    }    public void updateById(UserInfo user) {        String sql = "UPDATE usertable SET account=?,name=?,birthday=?,sex=? WHERE id=?";        PreparedStatement pstmt = null;        DataBaseConnectionOracle dbc = null;        try {    dbc = new DataBaseConnectionOracle();            pstmt = dbc.getConnection().prepareStatement(sql);            pstmt.setString(1, user.getAccount());            pstmt.setString(2, user.getName());            pstmt.setString(3, user.getBirthday());            pstmt.setInt(4, user.getSex());            pstmt.setInt(5, user.getId());            pstmt.executeUpdate();            pstmt.close();        } catch (Exception e) {            e.printStackTrace();        } finally {            dbc.close();        }}    public static void main(String[] args) {        UserManagerDaoImpl um = new UserManagerDaoImpl();        ArrayList<UserInfo> userlist = um.queryAll();        UserInfo u = um.queryById(1);        System.out.println(userlist.size());    }}[color=#FF0000]package com.experiment.Db;[/color]import java.sql.*;public class DataBaseConnectionOracle {        private final String dbUrl = "jdbc:oracle:thin:@Bard0117:1522:ORCL";    private final String user="system";    private final String password="system";        private Connection conn = null;        public DataBaseConnectionOracle() {        try {            Class.forName("oracle.jdbc.driver.OracleDriver");            this.conn = DriverManager.getConnection(dbUrl,user,password);        } catch (Exception e) {        }    }     public Connection getConnection() {        return this.conn;    }        public void close() {        try {            this.conn.close();        } catch (Exception e) {        }    }   public static void main(String [] args){        DataBaseConnectionOracle dbc = new DataBaseConnectionOracle();        @SuppressWarnings("unused")        Connection conn = dbc.getConnection();        System.out.println("Connection is success!");        dbc.close();    }}[color=#FF0000]package com.experiment.usermanger;[/color]import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.experiment.Dao.UserManagerDao;import com.experiment.Dao.DapImpl.UserManagerDaoImpl;import com.experiment.vo.UserInfo;public class UserManagerServlet extends HttpServlet {    private static final long serialVersionUID = 1L;    public UserManagerServlet() {        super();}    protected void doGet(HttpServletRequest request,            HttpServletResponse response) throws ServletException, IOException {        System.out.println("do get.........");        doPost(request, response);    }protected void doPost(HttpServletRequest request,            HttpServletResponse response) throws ServletException, IOException {        request.setCharacterEncoding("UTF-8");        String action = request.getParameter("action");        System.out.println(action);        if ("add".equals(action)) {            addUser(request,response);        }        if ("update".equals(action)){            updateUser(request,response);        }        if ("delete".equals(action)){            deleteUser(request,response);        }}    private void deleteUser(HttpServletRequest request,            HttpServletResponse response) throws IOException {        System.out.println("删除数据!");        int deleteId = Integer.parseInt(request.getParameter("deleteid"));        UserManagerDao umd = new UserManagerDaoImpl();        umd.deleteById(deleteId);        response.sendRedirect("index.jsp");    }    private String parseBirthday(HttpServletRequest request,            HttpServletResponse response) {        String year = request.getParameter("year");        String month = request.getParameter("month");        String day = request.getParameter("day");        String date = String.valueOf(year + "-" + month + "-" + day);        return date;    }    private void addUser(HttpServletRequest request,            HttpServletResponse response) throws IOException {        System.out.println("提交数据bbb");        UserInfo useradd = new UserInfo();        String userAccount = request.getParameter("userId");        String userName = request.getParameter("nameAll");        String birthday = parseBirthday(request, response);        String sex = request.getParameter("sex");        int sexnum = 0;        if ("b".equals(sex)) {            sexnum = 0;        } else {    sexnum = 1;        }useradd.setAccount(userAccount);        useradd.setName(userName);        useradd.setBirthday(birthday);        useradd.setSex(sexnum);        UserManagerDao umd = new UserManagerDaoImpl();        umd.insert(useradd);        System.out.println("存储用户" + userName);        response.sendRedirect("index.jsp");    }private void updateUser(HttpServletRequest request,            HttpServletResponse response) throws IOException {        System.out.println("更新用户");        UserInfo userUpdate = new UserInfo();        int userId = Integer.parseInt(request.getParameter("userid"));        String userAccount = request.getParameter("updateAccount");        String userName = request.getParameter("updateName");        String birthday = request.getParameter("updateBirthday");        String sex = request.getParameter("sex");        int sexnum = 0;        if ("g".equals(sex)) {            sexnum = 0;        } else {    sexnum = 1;}        userUpdate.setId(userId);        userUpdate.setAccount(userAccount);        userUpdate.setName(userName);        userUpdate.setBirthday(birthday);        userUpdate.setSex(sexnum);        UserManagerDao umd = new UserManagerDaoImpl();        umd.updateById(userUpdate);        System.out.println("更新id为"+ userId+"的用户" );        response.sendRedirect("index.jsp");    }}
  相关解决方案