package com.bjjdsy.demo.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.bjjdsy.demo.dao.UserDao;
import com.bjjdsy.demo.entity.User;
import com.bjjdsy.demo.utils.DBUtil;
public class UserDaoImpl implements UserDao {
// 添加一个用户
public void addUser(User user) throws Exception {
String sql = "insert into AASX1(NAME,SEX,BIRTHDAY,IDTYPE,IDNO,ADDRESS,ZIPCODE,PHONE,EMAIL,MAKEDATE)"
+ " values(?,?,?,?,?,?,?,?,?,?)";
Connection conn = null;
try {
conn = DBUtil.getConnection();
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setString(1, user.getName());
psmt.setString(2, user.getSex());
psmt.setDate(3, new java.sql.Date(user.getBirthday().getTime()));
psmt.setString(4, user.getIdType());
psmt.setString(5, user.getIdNo());
psmt.setString(6, user.getAddress());
psmt.setString(7, user.getZipcode());
psmt.setString(8, user.getPhone());
psmt.setString(9, user.getEmail());
psmt.setDate(10, new java.sql.Date(new java.util.Date().getTime()));
psmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(conn);
}
}
// 删除一个用户
public void deleteUser(int id) throws Exception {
String sql = "delete from AASX1 where ID=" + id;
Connection conn = null;
try {
conn = DBUtil.getConnection();
Statement stat = conn.createStatement();
stat.executeUpdate(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(conn);
}
}
// 查询所有用户
public List<User> findAll() throws Exception {
String sql = "select * from AASX1";
List<User> users = new ArrayList<User>();
Connection conn = null;
try {
conn = DBUtil.getConnection();
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("ID");
String name = rs.getString("NAME");
String sex = rs.getString("SEX");
Date birthday = rs.getDate("BIRTHDAY");
String idType = rs.getString("IDTYPE");
String idNo = rs.getString("IDNO");
String address = rs.getString("ADDRESS");
String zipcode = rs.getString("ZIPCODE");
String phone = rs.getString("PHONE");
String email = rs.getString("EMAIL");
Date makeDate = rs.getDate("MAKEDATE");
User u = new User();
u.setId(id);
u.setName(name);
u.setSex(sex);
u.setBirthday(birthday);
u.setIdType(idType);
u.setIdNo(idNo);
u.setAddress(address);
u.setZipcode(zipcode);
u.setPhone(phone);
u.setEmail(email);
u.setMakeDate(makeDate);
users.add(u);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(conn);
}
return users;
}
// 按照指定的id查询一个用户
public User findById(int id) throws Exception {
String sql = "select * from AASX1 where ID=" + id;
Connection conn = null;
User u = null;
try {
conn = DBUtil.getConnection();
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
if (rs.next()) {
String name = rs.getString("NAME");
String sex = rs.getString("SEX");
Date birthday = rs.getDate("BIRTHDAY");
String idType = rs.getString("IDTYPE");
String idNo = rs.getString("IDNO");
String address = rs.getString("ADDRESS");
String zipcode = rs.getString("ZIPCODE");
String phone = rs.getString("PHONE");
String email = rs.getString("EMAIL");
Date makeDate = rs.getDate("MAKEDATE");
u = new User();
u.setId(id);
u.setName(name);
u.setSex(sex);
u.setBirthday(birthday);
u.setIdType(idType);
u.setIdNo(idNo);
u.setAddress(address);
u.setZipcode(zipcode);
u.setPhone(phone);
u.setEmail(email);
u.setMakeDate(makeDate);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(conn);
}
return u;
}
// 修改一个用户信息
public void modify(User user) throws Exception {
String sql = "update AASX1 set IDTYPE=?,IDNO=?,ADDRESS=?,ZIPCODE=?,PHONE=?,EMAIL=? where id="+user.getId();
Connection conn = null;
try {
conn = DBUtil.getConnection();
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setString(1, user.getIdType());
psmt.setString(2, user.getIdNo());
psmt.setString(3, user.getAddress());
psmt.setString(4, user.getZipcode());
psmt.setString(5, user.getPhone());
psmt.setString(6, user.getEmail());
psmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(conn);
}
}
//根据条件查询记录信息
public List<User> findByCondition(String condition) throws Exception {
String sql = "select * from AASX1 where 1=1"+condition;
List<User> users = new ArrayList<User>();
Connection conn = null;
try {
conn = DBUtil.getConnection();
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("ID");
String name = rs.getString("NAME");
String sex = rs.getString("SEX");
Date birthday = rs.getDate("BIRTHDAY");
String idType = rs.getString("IDTYPE");
String idNo = rs.getString("IDNO");
String address = rs.getString("ADDRESS");
String zipcode = rs.getString("ZIPCODE");
String phone = rs.getString("PHONE");
String email = rs.getString("EMAIL");
Date makeDate = rs.getDate("MAKEDATE");
User u = new User();
u.setId(id);
u.setName(name);
u.setSex(sex);
u.setBirthday(birthday);
u.setIdType(idType);
u.setIdNo(idNo);
u.setAddress(address);
u.setZipcode(zipcode);
u.setPhone(phone);
u.setEmail(email);
u.setMakeDate(makeDate);
users.add(u);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(conn);
}
return users;
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.bjjdsy.demo.dao.UserDao;
import com.bjjdsy.demo.entity.User;
import com.bjjdsy.demo.utils.DBUtil;
public class UserDaoImpl implements UserDao {
// 添加一个用户
public void addUser(User user) throws Exception {
String sql = "insert into AASX1(NAME,SEX,BIRTHDAY,IDTYPE,IDNO,ADDRESS,ZIPCODE,PHONE,EMAIL,MAKEDATE)"
+ " values(?,?,?,?,?,?,?,?,?,?)";
Connection conn = null;
try {
conn = DBUtil.getConnection();
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setString(1, user.getName());
psmt.setString(2, user.getSex());
psmt.setDate(3, new java.sql.Date(user.getBirthday().getTime()));
psmt.setString(4, user.getIdType());
psmt.setString(5, user.getIdNo());
psmt.setString(6, user.getAddress());
psmt.setString(7, user.getZipcode());
psmt.setString(8, user.getPhone());
psmt.setString(9, user.getEmail());
psmt.setDate(10, new java.sql.Date(new java.util.Date().getTime()));
psmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(conn);
}
}
// 删除一个用户
public void deleteUser(int id) throws Exception {
String sql = "delete from AASX1 where ID=" + id;
Connection conn = null;
try {
conn = DBUtil.getConnection();
Statement stat = conn.createStatement();
stat.executeUpdate(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(conn);
}
}
// 查询所有用户
public List<User> findAll() throws Exception {
String sql = "select * from AASX1";
List<User> users = new ArrayList<User>();
Connection conn = null;
try {
conn = DBUtil.getConnection();
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("ID");
String name = rs.getString("NAME");
String sex = rs.getString("SEX");
Date birthday = rs.getDate("BIRTHDAY");
String idType = rs.getString("IDTYPE");
String idNo = rs.getString("IDNO");
String address = rs.getString("ADDRESS");
String zipcode = rs.getString("ZIPCODE");
String phone = rs.getString("PHONE");
String email = rs.getString("EMAIL");
Date makeDate = rs.getDate("MAKEDATE");
User u = new User();
u.setId(id);
u.setName(name);
u.setSex(sex);
u.setBirthday(birthday);
u.setIdType(idType);
u.setIdNo(idNo);
u.setAddress(address);
u.setZipcode(zipcode);
u.setPhone(phone);
u.setEmail(email);
u.setMakeDate(makeDate);
users.add(u);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(conn);
}
return users;
}
// 按照指定的id查询一个用户
public User findById(int id) throws Exception {
String sql = "select * from AASX1 where ID=" + id;
Connection conn = null;
User u = null;
try {
conn = DBUtil.getConnection();
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
if (rs.next()) {
String name = rs.getString("NAME");
String sex = rs.getString("SEX");
Date birthday = rs.getDate("BIRTHDAY");
String idType = rs.getString("IDTYPE");
String idNo = rs.getString("IDNO");
String address = rs.getString("ADDRESS");
String zipcode = rs.getString("ZIPCODE");
String phone = rs.getString("PHONE");
String email = rs.getString("EMAIL");
Date makeDate = rs.getDate("MAKEDATE");
u = new User();
u.setId(id);
u.setName(name);
u.setSex(sex);
u.setBirthday(birthday);
u.setIdType(idType);
u.setIdNo(idNo);
u.setAddress(address);
u.setZipcode(zipcode);
u.setPhone(phone);
u.setEmail(email);
u.setMakeDate(makeDate);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(conn);
}
return u;
}
// 修改一个用户信息
public void modify(User user) throws Exception {
String sql = "update AASX1 set IDTYPE=?,IDNO=?,ADDRESS=?,ZIPCODE=?,PHONE=?,EMAIL=? where id="+user.getId();
Connection conn = null;
try {
conn = DBUtil.getConnection();
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setString(1, user.getIdType());
psmt.setString(2, user.getIdNo());
psmt.setString(3, user.getAddress());
psmt.setString(4, user.getZipcode());
psmt.setString(5, user.getPhone());
psmt.setString(6, user.getEmail());
psmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(conn);
}
}
//根据条件查询记录信息
public List<User> findByCondition(String condition) throws Exception {
String sql = "select * from AASX1 where 1=1"+condition;
List<User> users = new ArrayList<User>();
Connection conn = null;
try {
conn = DBUtil.getConnection();
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("ID");
String name = rs.getString("NAME");
String sex = rs.getString("SEX");
Date birthday = rs.getDate("BIRTHDAY");
String idType = rs.getString("IDTYPE");
String idNo = rs.getString("IDNO");
String address = rs.getString("ADDRESS");
String zipcode = rs.getString("ZIPCODE");
String phone = rs.getString("PHONE");
String email = rs.getString("EMAIL");
Date makeDate = rs.getDate("MAKEDATE");
User u = new User();
u.setId(id);
u.setName(name);
u.setSex(sex);
u.setBirthday(birthday);
u.setIdType(idType);
u.setIdNo(idNo);
u.setAddress(address);
u.setZipcode(zipcode);
u.setPhone(phone);
u.setEmail(email);
u.setMakeDate(makeDate);
users.add(u);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(conn);
}
return users;
}
}