1. 在数据库中建立表tab_stud:
CREATE TABLE tab_stud
(
stud_no INT primary key,
stud_name VARCHAR(20) not null,
birthday DATE not null
);
2. 编写Student实体:
package com.huey.entity;
import java.text.DateFormat;
import java.util.Date;
/**
* @version 2013-08-01
* @author huey2672
*
*/
public class Student {
private int number; // 学生的学号
private String name; // 学生的名字
private Date birthday; // 学生的生日
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Student() {
}
public Student(int number, String name, Date birthday) {
setNumber(number);
setName(name);
setBirthday(birthday);
}
@Override
public int hashCode() {
return name.hashCode() * 11 + birthday.hashCode();
}
@Override
public boolean equals(Object otherObject) {
// 检测this和otherObject是否引用同一对象;
// 这只是一个优化,计算等式要比一个一个地比较类中所有域所付出的代价小得多
if (this == otherObject)
return true;
// 如果otherObject为null,返回false
if (otherObject == null)
return false;
// 如果equals的语义在每个子类中有所改变,就使用getClass检测
if (getClass() != otherObject.getClass())
return false;
// 如果所有的子类都拥有统一的语义,就使用instanceof检测
// if (!(otherObject instanceof Student)) return false;
// 将otherObject转换为相应的类类型变量
Student other = (Student) otherObject;
// 对需要比较的域进行比较
return number == other.getNumber() && name.equals(other.getName())
&& birthday.equals(other.getBirthday());
}
@Override
public String toString() {
if (this == null) {
return super.toString();
}
return String.format("No.%d: %s, %s", number, name, DateFormat
.getDateInstance().format(birthday));
}
}
3. JDBC的一些配置信息,jdbc.properties:
driverclass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb
username=root
password=123456
4. 编写JDBC工具类, DBUtil:
package com.huey.util;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Properties;
/**
* JDBC工具类
* @version 2013-08-01
* @author huey2672
*
*/
public class DBUtil {
/**
* 获得数据库连接
* @return
*/
public static Connection getConnection() {
Connection conn = null;
try {
// 加载properties文件,读取JDBC的一些配置参数
Properties properties = new Properties();
properties.load(ClassLoader.getSystemResourceAsStream("jdbc.properties"));
String driverclass = properties.getProperty("driverclass");
String url = properties.getProperty("url");
String username = properties.getProperty("username");
String password = properties.getProperty("password");
// 加载驱动
Class.forName(driverclass);
// 获得数据库连接
conn = DriverManager.getConnection(url, username, password);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭资源
* @param rs 结果集
* @param st Statement实例
* @param conn 数据库连接
*/
public static void closeResource(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 执行数据库的更新操作
* @param sql SQL语句
* @param params 参数
* @return 数据库中记录的更新数
*/
public static int executeUpdate(String sql, Object... params) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
setParameter(ps, params);
return ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeResource(null, ps, conn);
}
return 0;
}
/**
* 为PreparedStatement对象设置参数
* @param ps PreparedStatement对象
* @param params 参数
*/
public static void setParameter(PreparedStatement ps, Object... params) {
try {
for (int i = 0; i < params.length; i++) {
if (params[i] instanceof Date) {
// 处理Date类型的数据
ps.setDate(i + 1, new java.sql.Date(((Date) params[i]).getTime()));
} else {
ps.setObject(i + 1, params[i]);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
5. Student的DAO层接口:
package com.huey.dao;
import com.huey.entity.Student;
/**
* Student的DAO层接口
* @version 2013-08-01
* @author huey2672
*
*/
public interface StudentDao {
/**
* 添加新的学生记录
* @param student 要添加的学生记录信息
* @return true表示添加记录成功,false表示添加记录失败
*/
public boolean addStudent(Student student);
/**
* 根据学生学号删除该学生记录
* @param studNo 要删除的学生的学号
* @return true表示删除记录成功,false表示删除记录失败
*/
public boolean deleteStudentByNumber(int studNo);
/**
* 更新学生记录
* @param student 要更新的学生记录信息
* @return true表示更新记录成功,false表示更新记录失败
*/
public boolean updateStudent(Student student);
/**
* 根据学生学号查找该学生信息
* @param studNo 要查找的学生的学号
* @return 学生的信息,若查找不到该学生记录则返回null
*/
public Student findStudentByNumber(int studNo);
}
6. Student的DAO层实现:
package com.huey.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import com.huey.dao.StudentDao;
import com.huey.entity.Student;
import com.huey.util.DBUtil;
/**
* Student的DAO层实现
* @version 2013-08-01
* @author huey2672
*
*/
public class StudentDaoImpl implements StudentDao {
/**
* 添加新的学生记录
* @param student 要添加的学生记录信息
* @return true表示添加记录成功,false表示添加记录失败
*/
@Override
public boolean addStudent(Student student) {
String sql = "insert into tab_stud values(?,?,?)";
int number = student.getNumber();
String name = student.getName();
Date birthday = student.getBirthday();
DBUtil.executeUpdate(sql, number, name, birthday);
return true;
}
/**
* 根据学生学号删除该学生记录
* @param studNo 要删除的学生的学号
* @return true表示删除记录成功,false表示删除记录失败
*/
@Override
public boolean deleteStudentByNumber(int studNo) {
String sql="delete from tab_stud where stud_no=?";
DBUtil.executeUpdate(sql, studNo);
return true;
}
/**
* 更新学生记录
* @param student 要更新的学生记录信息
* @return true表示更新记录成功,false表示更新记录失败
*/
@Override
public boolean updateStudent(Student student) {
String sql="update tab_stud set stud_name=?,birthday=? where stud_no=?";
int number = student.getNumber();
String name = student.getName();
Date birthday = student.getBirthday();
DBUtil.executeUpdate(sql, name, birthday, number);
return true;
}
/**
* 根据学生学号查找该学生信息
* @param studNo 要查找的学生的学号
* @return 学生的信息,若查找不到该学生记录则返回null
*/
@Override
public Student findStudentByNumber(int studNo) {
Connection conn = DBUtil.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
Student student = null;
try {
ps = conn.prepareStatement("select * from tab_stud where stud_no=?");
ps.setInt(1, studNo);
rs = ps.executeQuery();
if (rs.next()) {
student = new Student();
student.setNumber(rs.getInt("stud_no"));
student.setName(rs.getString("stud_name"));
student.setBirthday(rs.getDate("birthday"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeResource(rs, ps, conn);
}
return student;
}
}
7. Student的Service层接口:
package com.huey.service;
import com.huey.entity.Student;
/**
* Student的Service层接口
* @version 2013-08-01
* @author huey2672
*
*/
public interface StudentService {
/**
* 添加新的学生记录
* @param student 要添加的学生记录信息
* @return true表示添加记录成功,false表示添加记录失败
*/
public boolean addStudent(Student student);
/**
* 根据学生学号删除该学生记录
* @param studNo 要删除的学生的学号
* @return true表示删除记录成功,false表示删除记录失败
*/
public boolean deleteStudentByNumber(int studNo);
/**
* 更新学生记录
* @param student 要更新的学生记录信息
* @return true表示更新记录成功,false表示更新记录失败
*/
public boolean updateStudent(Student student);
/**
* 根据学生学号查找该学生信息
* @param studNo 要查找的学生的学号
* @return 学生的信息,若查找不到该学生记录则返回null
*/
public Student findStudentByNumber(int studNo);
}
8. Student的Service层实现:
package com.huey.service.impl;
import com.huey.dao.StudentDao;
import com.huey.dao.impl.StudentDaoImpl;
import com.huey.entity.Student;
import com.huey.service.StudentService;
/**
* Student的Service层实现
* @version 2013-08-01
* @author huey2672
*
*/
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao = new StudentDaoImpl();
/**
* 添加新的学生记录
* @param student 要添加的学生记录信息
* @return true表示添加记录成功,false表示添加记录失败
*/
@Override
public boolean addStudent(Student student) {
if (student == null) {
return false;
}
// 数据库中如果存在该学生记录,则不能再添加新的纪录
if (findStudentByNumber(student.getNumber()) != null) {
return false;
}
return studentDao.addStudent(student);
}
/**
* 根据学生学号删除该学生记录
* @param studNo 要删除的学生的学号
* @return true表示删除记录成功,false表示删除记录失败
*/
@Override
public boolean deleteStudentByNumber(int studNo) {
// 只有存在该编号的学生记录,才能对其删除
if (findStudentByNumber(studNo) != null) {
return studentDao.deleteStudentByNumber(studNo);
}
return false;
}
/**
* 更新学生记录
* @param student 要更新的学生记录信息
* @return true表示更新记录成功,false表示更新记录失败
*/
@Override
public boolean updateStudent(Student student) {
if (student == null) {
return false;
}
// 只有存在该编号的学生记录,才能对其修改
if (findStudentByNumber(student.getNumber()) != null) {
return studentDao.updateStudent(student);
}
return false;
}
/**
* 根据学生学号查找该学生信息
* @param studNo 要查找的学生的学号
* @return 学生的信息,若查找不到该学生记录则返回null
*/
@Override
public Student findStudentByNumber(int studNo) {
return studentDao.findStudentByNumber(studNo);
}
}
9. 测试用例:
package com.huey.test;
import java.util.Date;
import org.junit.Test;
import com.huey.entity.Student;
import com.huey.service.StudentService;
import com.huey.service.impl.StudentServiceImpl;
/**
* Student模块测试用例
* @version 2013-08-01
* @author huey2672
*
*/
public class StudentModuleTest {
/**
* 测试添加学生记录
*/
@Test
public void testAddStudent() {
StudentService studentService = new StudentServiceImpl();
Student student = new Student(1004, "kid", new Date());
if (studentService.addStudent(student)) {
System.out.println("success");
} else {
System.out.println("fail");
}
}
/**
* 测试删除学生记录
*/
@Test
public void testDeleteStudent() {
StudentService studentService = new StudentServiceImpl();
int studNo = 1001;
if (studentService.deleteStudentByNumber(studNo)) {
System.out.println("success");
} else {
System.out.println("fail");
}
}
/**
* 测试更新学生记录
*/
@Test
public void testUpdateStudent() {
StudentService studentService = new StudentServiceImpl();
Student student = new Student(1006, "Sugar", new Date());
if (studentService.updateStudent(student)) {
System.out.println("success");
} else {
System.out.println("fail");
}
}
/**
* 测试查找学生记录
*/
@Test
public void testFindStudent() {
StudentService studentService = new StudentServiceImpl();
int studNo = 1001;
Student student = studentService.findStudentByNumber(studNo);
System.out.println(student);
}
}