学生数据访问接口实现类StudentDaoImpl
代码如下:
package net.lcn.student.bean.dao.impl;
import net.lcn.student.bean.bean.Student;
import net.lcn.student.bean.dao.StudentDao;
import net.lcn.student.bean.dbutil.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;
/**
public class StudentDaoImpl implements StudentDao {
/**
* 插入学生记录
*
* @param student
* @return
*/
@Override
public int insert(Student student) {
//定义插入记录
int count = 0;
//1.获得数据库连接
Connection conn = ConnectionManager.getConnection();
//2.定义SQL字符串
String strSQL = "insert into t_student (id, name, sex, age, department, class, telephone)"
+ " values(?,?,?,?,?,?,?)";
try {
//3.创建预备对象语句
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//4.设置占位符
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());
//5.执行SQL返回插入记录数
count = pstmt.executeUpdate();
//6.关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回插入记录
return count;
}
/**
* 按学号删除学生记录
*
* @param id
* @return
*/
@Override
public int deleteById(String id) {
//定义删除记录数
int count = 0;
//1.获取数据库连接
Connection conn = ConnectionManager.getConnection();
//2.定义SQL字符串
String strSQL = "delete from t_student where id = ?";
try {
//3.创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//4.设置占位符的值
pstmt.setString(1,id);
//5.执行SQL,返回删除记录数
count = pstmt.executeUpdate();
//6.关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回删除记录
return count;
}
/**
* 按班级删除学生记录
*
* @param clazz
* @return
*/
@Override
public int deleteByClass(String clazz) {
//定义删除记录
int count = 0;
//1.获取数据库连接
Connection conn = ConnectionManager.getConnection();
//2.定义SQL字符串
String strSQL = "delete from t_student where class = ?";
//3.创建预备语句对象
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//4.设置占位符的值
pstmt.setString(1,clazz);
//5.执行SQL,返回删除记录
count = pstmt.executeUpdate();
//6.关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public int deleteByDepartment(String department) {
return 0;
}
@Override
public int update(Student student) {
return 0;
}
@Override
public Student findById(String id) {
return null;
}
@Override
public List findByname(String name) {
return null;
}
@Override
public List findByClass(String clazz) {
return null;
}
@Override
public List findByDepartment(String department) {
return null;
}
@Override
public List findAll() {
return null;
}
@Override
public Vector findRowsBySex() {
return null;
}
@Override
public Vector findRowsByclass() {
return null;
}
@Override
public Vector findRowsByDepartment() {
return null;
}
}
在net.lcn.student.test包里创建测试类TestStudentDaoImpl:
测试结果如下:
数据插入成功:
数据的删除:
运行结果如下:
数据删除成功:
在StudentDaoImpl完成按姓名查询学生记录的代码的书写
代码如下:
/**
* 按姓名查询学生记录
*
* @param name
* @return
*/
@Override
public List findByname(String name) {
//声明学生列表
List students = new ArrayList();
//1.获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
//2.定义SQL字符串
String strSQL = "select * from t_student where name like ?";
try {
//3.创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//4.设置占位符的值
pstmt.setString(1,name + "%");
//5.执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
//6.遍历结果集
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);
}
//7.关闭结果集
rs.close();
//8.关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回数据库连接
return students;
}
编写测试方法testFindByName()
运行结果如下:
再此运行:
编写测试方法testFindByName():查询出所有姓“李”的人
运行结果如下:
编写测试方法testFindByName():按姓查询人,假入该姓氏在表中不存在的情况
判断是否找到记录,可以修改代码如下:
运行结果如下:
在StudentDaoImpl完成按性别统计学生人数代码的书写:
/**
* 按性别统计学生人数
*
* @return 统计结果向量
*/
@Override
public Vector findRowsBySex() {
//定义行集向量
Vector rows = new Vector();
//1.获取数据库连接
Connection conn = ConnectionManager.getConnection();
//2.定义SQL字符串
String strSQL = "select sex as '性别', count(*) as '人数'"
+ "from t_student group by sex order by sex desc ";
try {
//3.创建语句对象
Statement stmt = conn.createStatement();
//4.执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
//5.遍历结果集
while (rs.next()) {
//定义当前向量
Vector 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;
}
创建测试方法testFindRowsBySex():
运行结果如下:
在StudentDaoImpl完成按按系部删除学生记录代码的书写:
代码如下:
/**
* 按系部删除学生记录
*
* @param department
* @return
*/
@Override
public int deleteByDepartment(String department) {
//定义删除记录数
int count = 0;
//1.获取数据库连接
Connection conn = ConnectionManager.getConnection();
//2.定义SQL字符串
String strSQL = “delete from t_student where department = ?”;
try {
//3.创建预备语句
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//4.设置占位符的值
pstmt.setString(1,department);
//5.执行SQL语句,返回删除的记录数
count = pstmt.executeUpdate();
//6.关闭预备语句
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回删除记录数
return count;
}
创建测试方法testDeleteByDepartment():
代码如下:
运行结果如下:
删除成功:
在StudentDaoImpl完成更新学生记录代码的书写:
代码如下:
/**
* 更新学生记录
*
* @param student
* @return
*/
@Override
public int update(Student student) {
//定义更新记录数
int count = 0;
//1.获取数据库连接
Connection conn = ConnectionManager.getConnection();
//2.定义SQL字符串
String strSQL = "update t_student set name = ?,sex = ?, age = ?,"
+ "class = ?, department = ?, telephone = ? where id = ?";
try {
//3.创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//4.设置占位符的值
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());
//5.执行SQL语句,返回更新记录数
count = pstmt.executeUpdate();
//6.关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回更新记录数
return count;
}
创建测试方法testupdate():
代码如下:
运行结果如下:
在StudentDaoImpl完成按学号查询学生记录代码的书写:
代码如下:
/**
* 按学号查询学生记录
*
* @param id
* @return 学生实体
*/
@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;
}
创建测试方法testfindById():
代码如下:
运行结果如下:
在StudentDaoImpl完成按班级查询学生记录代码的书写:
代码如下:
/**
* 按班级查询学生记录
*
* @param clazz
* @return 学生列表
*/
@Override
public List findByClass(String clazz) {
//声明学生列表
List students = new ArrayList();
//1.获取数据库连接
Connection conn = ConnectionManager.getConnection();
//2.定义SQL字符串
String strSQL = "select * from t_student where class like ?";
try {
//3.创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//4.设置占位符的值
pstmt.setString(1,clazz + "%");
//5.执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
//6.遍历结果集
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);
}
//7.关闭结果集
rs.close();
//8.关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
return students;
}
创建测试方法testFindclass():
代码如下:
运行结果如下:
在StudentDaoImpl完成按系部查询学生记录代码的书写:
代码如下:
/**
* 按系部查询学生记录
*
* @param department
* @return
*/
@Override
public List findByDepartment(String department) {
//声明学生列表
List students = new ArrayList();
//1.获取数据库连接
Connection conn = ConnectionManager.getConnection();
//2.定义SQL字符串
String strSQL = "select * from t_student where department like ?";
try {
//3.创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//4.设置占位符的值
pstmt.setString(1,department + "%");
//5.执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
//6. 遍历结果集
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);
}
//7.关闭结果集
rs.close();
//8.关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回学生列表
return students;
}
创建测试方法testFindByDepartment():
代码如下:
运行如下:
在StudentDaoImpl完成查询全部学生记录代码的书写:
代码如下:
/**
* 查询全部学生记录
*
* @return
*/
@Override
public List findAll() {
//声明学生列表
List students = new ArrayList();
//1.获取数据库连接
Connection conn = ConnectionManager.getConnection();
//2.定义SQL字符串
String strSQL = "select * from t_student";
try {
//3.创建预备语句对象
Statement stmt = conn.createStatement();
//4.执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
//5.遍历结果集
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);
}
//6.关闭结果集
stmt.close();
//7.关闭语句对象
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回学生列表
return students;
}
创建测试方法testFindByAll():
代码如下:
运行结果如下:
在StudentDaoImpl完成按班级统计学生人数代码的书写:
代码如下:
/**
* 按班级统计学生人数
*
* @return 统计结果向量
*/
@Override
public Vector findRowsByclass() {
//定义向量
Vector rows = new Vector();
//1.获取数据库连接
Connection conn = ConnectionManager.getConnection();
//2.定义SQL字符串
String strSQL = "select class as '班级', count(*) as '人数'"
+ "from t_student group by class order by class desc ";
try {
//3.创建语句对象
Statement stmt = conn.createStatement();
//4.执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
//5.遍历结果集
while (rs.next()) {
//定义当前行向量
Vector currentRow = new Vector();
//利用当前记录字段值设置当前行向量的元素值
currentRow.addElement(rs.getString("班级"));
currentRow.addElement(rs.getInt("人数") + "");
//当前行向量添加到行集向量
rows.addElement(currentRow);
}
//7.关闭结果集
rs.close();
//8.关闭预备语句对象
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回行集向量
return rows;
}