package com.tarena.ums.biz;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.tarena.ums.entity.User;
import com.tarena.util.ConnectionFactory;
import com.tarena.util.IdGenerator;
import com.tarena.util.JdbcUtil;
/**
* 系统的业务处理层 该层要独立于交互层,只负责将数据进行处理,不关心实现
*
* @author tarena
* @version 1.0
*/
public class UserBiz {
public List<User> findAll() {
Connection con = null;
Statement st = null;
ResultSet rs = null;
List<User> users = null;
try {
con = ConnectionFactory.getConnection();
String sql = "select id,uname,pwd,gender,degree,birthday,hobbies,introduction from user_0807";
System.out.println(sql);
st = con.createStatement();
rs = st.executeQuery(sql);
users = getUser(rs);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
JdbcUtil.close(rs, st, con);
}
return users;
}
private List<User> getUser(ResultSet rs) throws SQLException {
List<User> users = new ArrayList<User>();
while (rs.next()) {
User user = new User();
user.setId(rs.getInt(1));
user.setUserName(rs.getString(2));
user.setPassword(rs.getString(3));
user.setGender(rs.getString(4));
user.setDegree(rs.getString(5));
user.setBirthday(rs.getDate(6));
user.setHobbies(rs.getString(7));
user.setIntroduction(rs.getString(8));
users.add(user);
}
return users;
}
public void add(User user) {
Connection con = null;
PreparedStatement ps = null;
try {
con = ConnectionFactory.getConnection();
user.setId(IdGenerator.generateIdBySequence(con, "user_sequence"));
String sql = "insert into user_0807(id,uname,pwd,gender,degree,birthday,hobbies,introduction) values(?,?,?,?,?,?,?,?)";
System.out.println(sql);
ps = con.prepareStatement(sql);
int index = 1;
ps.setInt(index++, user.getId());
ps.setString(index++, user.getUserName());
ps.setString(index++, user.getPassword());
ps.setString(index++, user.getGender());
ps.setString(index++, user.getDegree());
ps.setDate(index++, user.getBirthday());
ps.setString(index++, user.getHobbies());
ps.setString(index++, user.getIntroduction());
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
JdbcUtil.close(ps, con);
}
}
public void modify(User user) {
Connection con = null;
PreparedStatement ps = null;
try {
con = ConnectionFactory.getConnection();
String sql = "update user_0807 set pwd=?,gender=?,degree=?,birthday=?,hobbies=?,introduction=? where id = ?";
System.out.println(sql);
ps = con.prepareStatement(sql);
int index = 1;
ps.setString(index++, user.getPassword());
ps.setString(index++, user.getGender());
ps.setString(index++, user.getDegree());
ps.setDate(index++, user.getBirthday());
ps.setString(index++, user.getHobbies());
ps.setString(index++, user.getIntroduction());
ps.setInt(index++, user.getId());
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
JdbcUtil.close(ps, con);
}
}
public void remove(int id) {
Connection con = null;
PreparedStatement ps = null;
try {
con = ConnectionFactory.getConnection();
String sql = "delete from user_0807 where id = ?";
System.out.println(sql);
ps = con.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
JdbcUtil.close(ps, con);
}
}
public User find(String userName, String password) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<User> users = null;
try {
con = ConnectionFactory.getConnection();
String sql = "select id,uname,pwd,gender,degree,birthday,hobbies,introduction from user_0807 ";
sql += "where uname=? and pwd=?";
System.out.println(sql);
ps = con.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, password);
rs = ps.executeQuery();
users = getUser(rs);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
JdbcUtil.close(rs, ps, con);
}
return users.isEmpty() ? null : users.iterator().next();
}
public User findById(int id) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<User> users = null;
try {
con = ConnectionFactory.getConnection();
String sql = "select id,uname,pwd,gender,degree,birthday,hobbies,introduction from user_0807 ";
sql += "where id=?";
System.out.println(sql);
ps = con.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
users = getUser(rs);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
JdbcUtil.close(rs, ps, con);
}
return users.isEmpty() ? null : users.iterator().next();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
用java工具MyEclipse实现的ums项目代码!
共50个文件
class:16个
java:11个
jar:7个
4星 · 超过85%的资源 需积分: 10 14 下载量 114 浏览量
2009-08-08
01:29:40
上传
评论
收藏 3.49MB RAR 举报
温馨提示
用java工具MyEclipse实现的ums项目代码! 有Action,filter的部分
资源推荐
资源详情
资源评论
收起资源包目录
ums.rar (50个子文件)
ums
.project 1KB
.mymetadata 282B
WebRoot
WEB-INF
web.xml 563B
lib
struts2-core-2.0.11.jar 2.22MB
jstl.jar 17KB
ognl-2.6.11.jar 164KB
commons-logging-1.0.4.jar 37KB
freemarker-2.3.8.jar 784KB
xwork-2.0.4.jar 445KB
standard.jar 287KB
classes
com
tarena
ums
biz
UserBiz.class 6KB
action
SystemAction.class 2KB
UserAction.class 2KB
interceptor
SessionInterceptor.class 1KB
filter
SessionFilter.class 2KB
entity
User.class 2KB
util
ConnectionFactory.class 3KB
IdGenerator.class 2KB
StringUtil.class 781B
HttpFilter.class 2KB
JdbcUtil.class 1KB
db.properties 105B
struts.xml 654B
user.xml 1KB
index.jsp 724B
META-INF
MANIFEST.MF 39B
user
add.jsp 2KB
modify.jsp 3KB
list.jsp 1KB
registerSuccess.jsp 0B
.myeclipse
src
com
ums
biz
UserBiz.java 5KB
action
UserAction.java 1KB
SystemAction.java 1KB
interceptor
SessionInterceptor.java 577B
filter
SessionFilter.java 911B
SessionFilter.class 2KB
entity
User.java 1KB
util
ConnectionFactory.class 3KB
JdbcUtil.java 759B
IdGenerator.class 2KB
ConnectionFactory.java 2KB
StringUtil.java 406B
IdGenerator.java 912B
StringUtil.class 781B
JdbcUtil.class 1KB
HttpFilter.java 1KB
db.properties 105B
struts.xml 654B
user.xml 1KB
.classpath 2KB
共 50 条
- 1
资源评论
- matever2012-06-29挺好的资源,注释很好
- boyanvicys2019-10-12这个还可以吧!~
zchunyang
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功