日期:2014-05-17  浏览次数:20697 次

将一个对人员增删改查的程序改成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,nam