Dbutil.java
-----------------------------------------------------------------------------------
package com.ceshi.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 封装数据常用操作
* @author Administrator
*
*/
public class DbUtil {
/**
* 取得Connection
* @return
*/
public static Connection getConnection(){
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String username = "drp1";
String password = "drp1";
conn = DriverManager.getConnection(url,username,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭conn
* @param conn
*/
public static void close(Connection conn){
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 关闭PreparedStatement
* @param pstmt
*/
public static void close(PreparedStatement pstmt){
if(pstmt != null){
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 关闭ResultSet
* @param pstmt
*/
public static void close(ResultSet rs ) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 测试数据库是否连接成功
* @param args
*/
public static void main(String args[]){
System.out.println("数据库连接成功!"+DbUtil.getConnection());
}
}
-----------------------------------------------------------------------------------
PageModel.java
-----------------------------------------------------------------------------------
package com.ceshi.util;
import java.util.List;
/**
* 封装分页信息
* @author Administrator
*
*/
public class PageModel<E> {
//结果集
private List<E> list;
//查询记录数
private int totalRecords;
//每页多少条数据
private int pageSize;
//第几页
private int pageNo;
/**
* 总页数
* @return
*/
public int getTotalPages() {
return (totalRecords + pageSize - 1) / pageSize;
}
/**
* 取得首页
* @return
*/
public int getTopPageNo() {
return 1;
}
/**
* 上一页
* @return
*/
public int getPreviousPageNo() {
if (pageNo <= 1) {
return 1;
}
return pageNo - 1;
}
/**
* 下一页
* @return
*/
public int getNextPageNo() {
if (pageNo >= getBottomPageNo()) {
return getBottomPageNo();
}
return pageNo + 1;
}
/**
* 取得尾页
* @return
*/
public int getBottomPageNo() {
return getTotalPages();
}
public List<E> getList() {
return list;
}
public void setList(List<E> list) {
this.list = list;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
}
-----------------------------------------------------------------------------------
UserManager.java
-----------------------------------------------------------------------------------
package com.ceshi.manager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.ceshi.entity.User;
import com.ceshi.util.DbUtil;
import com.ceshi.util.PageModel;
/**
* 采用单例管理用户
* @author Administrator
*
*/
public class UserManager {
private static UserManager instance = new UserManager();
private UserManager(){}
/**
* 提供一个入口方法
* @return
*/
public static UserManager getInstance(){
return instance;
}
/**
* 添加用户的方法
* @param user
*/
public void addUser(User user){
String sql = "insert into t_user (user_id, user_name, password, contact_tel, email, create_date) values (?,?,?,?,?,?)";
Connection conn = null;
PreparedStatement pstmt = null;
try{
conn = DbUtil.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getUserId());
pstmt.setString(2, user.getUserName());
pstmt.setString(3, user.getPassword());
pstmt.setString(4, user.getContactTel());
pstmt.setString(5, user.getEmail());
pstmt.setTimestamp(6, new Timestamp(new Date().getTime()));//能保存年月日 时分秒
pstmt.executeQuery();
} catch(SQLException e) {
e.printStackTrace();
}finally {
DbUtil.close(pstmt);
DbUtil.close(conn);
}
}
/**
* 修改用户
* @param user
*/
public void modifyUser(User user) {
StringBuilder sbSql = new StringBuilder();
sbSql.append("update t_user ")
.append("set user_name = ?, ")
.append("password = ?, ")
.append("contact_tel = ?, ")
.append("email = ? ")
.append("where user_id = ? ");
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DbUtil.getConnection();
pstmt = conn.prepareStatement(sbSql.toString());
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getContactTel());
pstmt.setString(4, user.getEmail());
pstmt.setString(5, user.getUserId());
pstmt.executeUp