系统有四张表:t_college、t_status、t_tudent与t_user表,对这四张表的操作就在相应的数据访问接口里进行规定
有四个数据访问接口:CollegeDao、StatusDao、StudentDao与UserDao。将这些接口放到dao包里。
其中
CollegeDao
package net.wlm.student.dao;
import net.wlm.student.bean.College;
public interface CollegeDao {
College findById(int id);
int update(College college);
}
StatusDao
package net.wlm.student.dao;
import net.wlm.student.bean.Status;
public interface StatusDao {
Status findById(int id);
int update(Status status);
}
StudentDao
package net.wlm.student.dao;
import net.wlm.student.bean.Student;
import java.util.List;
import java.util.Vector;
public interface StudentDao {
int insert(Student student);//查询学生信息
int deleteById(String id);//通过id删除信息
int deleteByClass(String clazz);//通过班级删除信息
int deleteByDepartment(String department);//通过系部删除信息
int update(Student student);//增加信息
Student findById(String id);//通过id查找信息
List<Student> findByName(String name);//通过名字查找信息
List<Student> findByClass(String clazz);//通过班级查找信息
List<Student> findByDepartment(String department);//通过系部查找信息
List<Student> findAll();//查找所有信息
Vector findRowsBySex();
Vector findRowsByClass();
Vector findRowsByDepartment();
}
UserDao
package net.wlm.student.dao;
import net.wlm.student.bean.User;
import java.util.List;
public interface UserDao {
int insert(User user);
int deleteById(String id);
int update(User user);
User findById(int id);
List<User> findAll();
User login(String username, String password);
boolean isUsernameExisted(String username);
}
在dao包里创建impl包
在impl包中进行
package net.wlm.student.dao.impl;
import net.wlm.student.bean.College;
import net.wlm.student.dao.CollegeDao;
import net.wlm.student.dbutil.ConnectionManager;
import java.sql.*;
public class CollegeDaoimpl implements CollegeDao {
@Override
public College findById(int id) {
College college = null;
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_college where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
college = new College();
college.setId(rs.getInt("id"));
college.setName(rs.getString("name"));
college.setPresident(rs.getString("president"));
college.setStartTime(rs.getTimestamp("start_time"));
college.setTelephone(rs.getString("telephone"));
college.setEmail(rs.getString("email"));
college.setAddress(rs.getString("address"));
college.setProfile(rs.getString("profile"));
}
pstmt.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return college;
}
@Override
public int update(College college) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "update t_college set name = ?, president = ?, start_time = ?,"
+ " telephone = ?, email = ?, address = ?, profile = ? where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, college.getName());
pstmt.setString(2, college.getPresident());
pstmt.setTimestamp(3, new Timestamp(college.getStartTime().getTime()));
pstmt.setString(4, college.getTelephone());
pstmt.setString(5, college.getEmail());
pstmt.setString(6, college.getAddress());
pstmt.setString(7, college.getProfile());
pstmt.setInt(8, college.getId());
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
}
在text包中建立TestCollegeDaoImpl
并在其中编写测试方法testFindById()
编写测试方法testUpdate()
package net.wlm.student.test;
import net.wlm.student.bean.College;
import net.wlm.student.dao.CollegeDao;
import net.wlm.student.dao.impl.CollegeDaoimpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TextCollegeDaoimpl {
@Test
public void textFindById() {
CollegeDao dao = new CollegeDaoimpl();
College college = dao.findById(1);
if (college != null) {
System.out.println("校名:" + college.getName());
System.out.println("校章:" + college.getPresident());
System.out.println("地址:" + college.getAddress());
System.out.println("邮箱:" + college.getEmail());
System.out.println("电话:" + college.getTelephone());
} else {
System.out.println("没有查询到!");
}
}
@Test
public void testUpdate(){
CollegeDao dao = new CollegeDaoimpl();
College college = dao.findById(1);
college.setPresident("李晓杰");
college.setTelephone("0830-123456");
int count = dao.update(college);
if (count>0) {
System.out.println("学校记录更新成功");
System.out.println("新校长:" + dao.findById(1).getPresident());
System.out.println("新电话:" + dao.findById(1).getTelephone());
}else {
System.out.println("学校记录更新失败");
}
}
}
在dao包里创建impl包
在impl包中进行
package net.wlm.student.dao.impl;
import net.wlm.student.bean.Status;
import net.wlm.student.dao.StatusDao;
import net.wlm.student.dbutil.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class StatusDaoImpl implements StatusDao {
@Override
public Status findById(int id) {
Status status = null;
Connection conn = ConnectionManager.getConnection();
String strSQL = "SELECT * FROM t_status WHERE id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
status = new Status();
status.setId(rs.getInt("id"));
status.setCollege(rs.getString("college"));
status.setVersion(rs.getString("version"));
status.setAuthor(rs.getString("author"));
status.setTelephone(rs.getString("telephone"));
status.setAddress(rs.getString("address"));
status.setEmail(rs.getString("email"));
}
pstmt.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return status;
}
@Override
public int update(Status status) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "update t_status set college = ?, version = ?, author = ?,"
+ " telephone = ?, address = ?, email = ? where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, status.getCollege());
pstmt.setString(2, status.getVersion());
pstmt.setString(3, status.getAuthor());
pstmt.setString(4, status.getTelephone());
pstmt.setString(5, status.getAddress());
pstmt.setString(6, status.getEmail());
pstmt.setInt(7, status.getId());
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
}
在text包中建立TestStatusDaoImpl
同时编写测试方法testFindById()
编写测试方法testUpdate()
package net.wlm.student.test;
import net.wlm.student.bean.Status;
import net.wlm.student.dao.StatusDao;
import net.wlm.student.dao.impl.StatusDaoImpl;
import org.junit.Test;
public class TextStatusDaoImpl {
@Test
public void testFindById() {
StatusDao dao = new StatusDaoImpl();
Status status = dao.findById(1);
if (status != null) {
System.out.println("作者:" + status.getAuthor());
System.out.println("学校:" + status.getCollege());
System.out.println("版本:" + status.getVersion());
System.out.println("地址:" + status.getAddress());
System.out.println("电话:" + status.getTelephone());
System.out.println("邮箱:" + status.getEmail());
} else {
System.out.println("没有查询到!");
}
}
@Test
public void testUpdate() {
StatusDao dao = new StatusDaoImpl();
Status status = dao.findById(1);
status.setAuthor("朱虹菊");
status.setTelephone("12345689710");
status.setEmail("[email protected]");
int count = dao.update(status);
if ( count >0 ){
System.out.println("状态记录更新成功");
System.out.println(dao.findById(1));
}else {
System.out.println("状态记录更新失败");
}
}
}
在dao包里创建impl包
在impl包中进行
package net.wlm.student.dao.impl;
import net.wlm.student.bean.Student;
import net.wlm.student.dao.StudentDao;
import net.wlm.student.dbutil.ConnectionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class StudentDaoImpl implements StudentDao {
@Override
public int insert(Student student) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "insert into t_student (id, name, sex, age, department, class, telephone)"
+ " values (?, ?, ?, ?, ?, ?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, student.getId());
pstmt.setString(2, student.getName());
pstmt.setString(3, student.getSex());
pstmt.setInt(4, student.getAge());
pstmt.setString(5, student.getDepartment());
pstmt.setString(6, student.getClazz());
pstmt.setString(7, student.getTelephone());
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public int deleteById(String id) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "delete from t_student where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, id);
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public int deleteByClass(String clazz) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "delete from t_student where class = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, clazz);
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public int deleteByDepartment(String department) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "delete from t_student where department = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, department);
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public int update(Student student) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "update t_student set name = ?, sex = ?, age = ?,"
+ " department = ?, class = ?, telephone = ? where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getSex());
pstmt.setInt(3, student.getAge());
pstmt.setString(4, student.getDepartment());
pstmt.setString(5, student.getClazz());
pstmt.setString(6, student.getTelephone());
pstmt.setString(7, student.getId());
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public Student findById(String id) {
// 声明学生对象
Student student = null;
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = "select * from t_student where id = ?";
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, id);
// 5. 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 6. 判断结果集是否有记录
if (rs.next()) {
// 创建学生实体
student = new Student();
// 利用当前记录各字段值设置学生实体属性
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return student;
}
@Override
public List<Student> findByName(String name) {
List<Student> students = new ArrayList<Student>();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_student where name like ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, name + "%");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
students.add(student);
}
rs.close();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return students;
}
@Override
public List<Student> findByClass(String clazz) {
List<Student> students = new ArrayList<Student>();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_student where class like ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, clazz + "%");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
students.add(student);
}
rs.close();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return students;
}
@Override
public List<Student> findByDepartment(String department) {
List<Student> students = new ArrayList<Student>();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_student where department like ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, department + "%");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
students.add(student);
}
rs.close();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return students;
}
@Override
public List<Student> findAll() {
List<Student> students = new ArrayList<Student>();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_student";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next()) {
Student student = new Student();
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
students.add(student);
}
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return students;
}
@Override
public Vector findRowsBySex() {
Vector rows = new Vector();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select sex as '性别', count(*) as '人数'"
+ " from t_student group by sex order by sex desc";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next()) {
Vector<String> currentRow = new Vector();
currentRow.addElement(rs.getString("性别"));
currentRow.addElement(rs.getInt("人数") + "");
rows.addElement(currentRow);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return rows;
}
@Override
public Vector findRowsByClass() {
Vector rows = new Vector();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select class as '班级', count(*) as '人数'"
+ " from t_student group by class order by class desc";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next()) {
Vector<String> currentRow = new Vector();
currentRow.addElement(rs.getString("班级"));
currentRow.addElement(rs.getInt("人数") + "");
rows.addElement(currentRow);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return rows;
}
@Override
public Vector findRowsByDepartment() {
Vector rows = new Vector();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select department as '系部', count(*) as '人数'"
+ " from t_student group by department order by department desc";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next()) {
Vector<String> currentRow = new Vector();
currentRow.addElement(rs.getString("系部"));
currentRow.addElement(rs.getInt("人数") + "");
rows.addElement(currentRow);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return rows;
}
}
并编写测试方法testInsert()
编写测试方法testDeleteById()
编写测试方法testDeleteByClass()
编写测试方法testFindByName()
编写测试方法testFindAll()
编写测试方法testFindRowsBySex()
编写测试方法testDeleteByDepartment()
编写测试方法testUpdate()
编写测试方法testFindById()
编写测试方法testFindByClass()
编写测试方法testFindByDepartment()
编写测试方法testFindRowsByClass()
编写测试方法testFindRowsByDepartment()
package net.wlm.student.test;
import net.wlm.student.bean.Student;
import net.wlm.student.dao.StudentDao;
import net.wlm.student.dao.impl.StudentDaoImpl;
import org.junit.Test;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
public class TextStudentDaoImpl {
StudentDao dao =new StudentDaoImpl();
@Test
public void textInsert(){
Student student = new Student();
student.setId("19206150");
student.setName("张嘉倪");
student.setSex("女");
student.setAge(19);
student.setDepartment("艺术传媒学院");
student.setClazz("2019数媒三班");
student.setTelephone("19856472332");
int count = dao.insert(student);
if (count >0){
System.out.println("恭喜,学生记录插入成功!");
System.out.println(dao.findById(student.getId()));
}else{
System.out.println("遗憾,学生记录插入失败!");
}
}
@Test
public void textDeleteByid(){
String id = "19201001";
int count = dao.deleteById(id);
if (count >0 ){
System.out.println("恭喜,删除学生记录成功!");
}else{
System.out.println("遗憾,删除学生记录失败!");
}
}
@Test
public void testDeleteByClass(){
String classs = "19级小教1班";
int count = dao.deleteByClass(classs);
if (count>0){
System.out.println("恭喜,["+classs+"]学生记录删除成功");
}else{
System.out.println("遗憾,["+classs+"]学生记录删除失败");
}
}
@Test
public void textFindByName(){
String name = "文科";
List<Student> students =dao.findByName(name);
if(students.size()>0){
for (Student student :students){
System.out.println(student);
}
}else{
System.out.println("温馨提示:查无此人");
}
}
@Test
public void textFindAll(){
List<Student> students = dao.findAll();
for (Student student : students){
System.out.println(student);
}
}
@Test
public void textFindRowsBySex(){
Vector rows = dao.findRowsBySex();
Iterator iterator = rows.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void textDeleteByDepartment(){
String dep = "艺术传媒学院";
int count = dao.deleteByDepartment(dep);
if (count>0){
System.out.println("恭喜,["+dep+"]的学生记录删除成功");
}else{
System.out.println("遗憾,["+dep+"]的学生记录删除失败");
}
}
@Test
public void testUpdate() {
Student student = dao.findById("18205088");
student.setId("18205088");
student.setName("张嘉倪");
student.setSex("女");
student.setAge(19);
student.setDepartment("艺术传媒学院");
student.setClazz("2019数媒三班");
student.setTelephone("19856472332");
int count = dao.update(student);
if ( count >0){
System.out.println("状态记录更新成功");
System.out.println(dao.findById("18205088"));
}else {
System.out.println("状态记录更新失败");
}
}
@Test
public void textFindById(){
String id = "18205088";
List<Student> students = Collections.singletonList(dao.findById(id));
if(students.size()>0){
for (Student student :students){
System.out.println(student);
}
}else{
System.out.println("温馨提示:查无此人");
}
}
@Test
public void testFindByClass(){
String classs = "2019数媒三班";
List<Student> students = dao.findByClass(classs);
if(students.size()>0){
for (Student student :students){
System.out.println(student);
}
}else{
System.out.println("温馨提示:查无此人");
}
}
@Test
public void testFindByDepartment(){
String department = "艺术传媒学院";
List<Student> students = dao.findByDepartment(department);
if(students.size()>0){
for (Student student :students){
System.out.println(student);
}
}else{
System.out.println("温馨提示:查无此人");
}
}
@Test
public void testFindRowsByClass(){
Vector rows = dao.findRowsByClass();
Iterator iterator = rows.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void testFindRowsByDepartment(){
Vector rows = dao.findRowsByDepartment();
Iterator iterator = rows.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
在dao包里创建impl包
在impl包中进行
package net.wlm.student.dao.impl;
import net.wlm.student.bean.User;
import net.wlm.student.dao.UserDao;
import net.wlm.student.dbutil.ConnectionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class UserDaoImpl implements UserDao {
public int insert(User user) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "insert into t_user (username, password, telephone, register_time)"
+ " values (?, ?, ?, ?)";
if (!isUsernameExisted(user.getUsername())) {
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getTelephone());
pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
}
return count;
}
@Override
public int deleteById(String id) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "delete from t_user where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setInt(1, Integer.parseInt(id));
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public int update(User user) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "update t_user set username = ?, password = ?, telephone = ?,"
+ " register_time = ? where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getTelephone());
pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
pstmt.setInt(5, user.getId());
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public User findById(int id) {
User user = null;
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_user where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return user;
}
@Override
public List<User> findAll() {
List<User> users = new ArrayList<User>();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_user";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
users.add(user);
}
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return users;
}
@Override
public User login(String username, String password) {
User user = null;
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_user where username = ? and password = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, username);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return user;
}
@Override
public boolean isUsernameExisted(String username) {
boolean existed = false;
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_user where username = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, username);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
existed = true;
}
pstmt.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return existed;
}
}
并编写测试方法testFindById()
编写测试方法testLogin()
编写测试方法testIsUsernameExisted()
编写测试方法testInsert()
编写测试方法testDeleteById()
编写测试方法testUpdate()
编写测试方法testFindAll()
package net.wlm.student.test;
import com.sun.javaws.security.AppContextUtil;
import net.wlm.student.bean.Student;
import net.wlm.student.bean.User;
import net.wlm.student.dao.UserDao;
import net.wlm.student.dao.impl.UserDaoImpl;
import org.junit.Test;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class TextUserDaoImpl {
UserDao dao = new UserDaoImpl();
@Test
public void testFindById() {
User user = dao.findById(2);
System.out.println("用户名:" + user.getUsername());
System.out.println("密码:" + user.getPassword());
System.out.println("电话:" + user.getTelephone());
System.out.println("注册时间:" + user.getRegisterTime());
}
@Test
public void textLongin() {
String username, password;
username = "王月";
password = "222222";
User user = dao.login(username, password);
if (user != null) {
System.out.println("恭喜,用户名与密码正确,登录成功");
} else {
System.out.println("遗憾,用户名与密码错误,登录失败");
}
}
@Test
public void testIsUsernameExisted() {
String username = "王月";
boolean result = dao.isUsernameExisted(username);
if (result) {
System.out.println("温馨提示:[" + username + "]已存在,不可用此名注册");
} else {
System.out.println("温馨提示:[" + username + "]不存在,可用此名注册");
}
}
@Test
public void testInsert() {
User user = new User();
user.setUsername("张嘉倪");
user.setPassword("111111");
user.setTelephone("11223456888");
user.setRegisterTime(new Date());
int count = dao.insert(user);
if (count > 0) {
System.out.println("用户记录插入成功");
System.out.println(dao.findById(dao.findAll().size()));
} else {
System.out.println("用户插入记录失败");
}
}
@Test
public void testDeleteById() {
String id = "191";
// 调用学生数据访问对象的按id删除方法
int count = dao.deleteById(id);
// 判断学生记录是否删除成功
if (count > 0) {
System.out.println("恭喜,学生记录删除成功!");
} else {
System.out.println("遗憾,学生记录删除失败!");
}
}
@Test
public void testUpdate() {
User user = dao.findById(10);
user.setId(10);
user.setUsername("张嘉倪");
user.setPassword("111111");
user.setTelephone("12349785155");
user.setRegisterTime(new Date());
int count = dao.update(user);
if (count > 0) {
System.out.println("状态记录更新成功");
System.out.println(dao.findById(10));
} else {
System.out.println("状态记录更新失败");
}
}
@Test
public void testFindAll() {
// 调用学生数据访问对象的查找全部方法
List<User> users = dao.findAll();
// 通过增强for循环遍历学生列表
for (User user : users) {
System.out.println(user);
}
}
}
这里的错误是我在数据访问接口对FindById的设置的问题,后来通过询问同学,按照alt+enter的提示进行了修改
通过老师已经敲打出来的代码学习,我可以更加灵活的运用,可以很快的创建和书写测试类,更加明确自己的学习目标,向着目标前进