JavaWeb实验五 综合训练至尊版
立即下载
资源介绍:
【没错,这个就是你要找的学习通的实验!】
实验五 综合训练
一、实验目的
1.了解MVC思想。
2.熟悉Java Web的各种开发技术。
二、实验学时
4H
三、实验性质
综合性实验
四、实验内容
1.创建表t_student,其中包含学号stuno,姓名stuname,性别stusex三个字段,在表中插入一些测试数据。(5分)
2.实现下述功能:
package dao.Impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import bean.Student;
import dao.StudentDao;
import util.DatabaseBean;
public class StudentDaoImpl implements StudentDao {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
@Override
public List findAll() {
List students = new ArrayList();
try {
conn = DatabaseBean.getConnection();
String sql = "select stuno,stuname,stusex from t_student";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
Student stu = new Student();
stu.setStuno(rs.getString("stuno"));
stu.setStuname(rs.getString("stuname"));
stu.setStusex(rs.getString("stusex"));
students.add(stu);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DatabaseBean.close(rs, pstmt, conn);
}
return students;
}
@Override
public List selectStudentByStuname(String stuname) {
// 上转型对象
List students = new ArrayList();
try {
conn = DatabaseBean.getConnection();
String sql = "select stuno,stuname,stusex from t_student where stuname like ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%" + stuname + "%");
rs = pstmt.executeQuery();
while (rs.next()) {
Student stu = new Student();
stu.setStuno(rs.getString("stuno"));
stu.setStuname(rs.getString("stuname"));
stu.setStusex(rs.getString("stusex"));
students.add(stu);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DatabaseBean.close(rs, pstmt, conn);
}
return students;
}
@Override // 精确查找
public Student selectStudentByStuno(String stuno) {
Student stu = new Student();
try {
conn = DatabaseBean.getConnection();
// String sql = "SELECT * FROM students WHERE stuno = ?";
// 就是因为这条语句是从实验二粘贴过来的,所以导致了后面的错误,终于找出错误了!
String sql = "SELECT * FROM t_student WHERE stuno = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, stuno);
rs = pstmt.executeQuery();
while (rs.next()) {
stu.setStuno(rs.getString("stuno"));
stu.setStuname(rs.getString("stuname"));
stu.setStusex(rs.getString("stusex"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DatabaseBean.close(rs, pstmt, conn);
}
return stu;
}
@Override
public boolean deleteStudent(String stuno) {
try {
conn = DatabaseBean.getConnection();
String sql = "delete from t_student where stuno=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, stuno);
int result = pstmt.executeUpdate();
// pstmt.close();
// conn.close();
if (result > 0) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DatabaseBean.close(rs, pstmt, conn);
}
return false;
}
@Override // 修改学生信息
public boolean saveStudent(Student stu) {
try {
conn = DatabaseBean.getConnection();
String sql = "update t_student set stuname=?,stusex=? where stuno=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, stu.getStuname());
pstmt.setString(2, stu.getStusex());
pstmt.setString(3, stu.getStuno());
int result = pstmt.executeUpdate();
// pstmt.close();
// conn.close();
if (result > 0) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DatabaseBean.close(rs, pstmt, conn);
}
return false;
}
@Override // 添加学生信息
public boolean addStudent(Student stu) {
try {
conn = DatabaseBean.getConnection();
String sql = "insert into t_student(stuno,stuname,stusex) values(?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, stu.getStuno());
pstmt.setString(2, stu.getStuname());
pstmt.setString(3, stu.getStusex());
int result = pstmt.executeUpdate();
if (result > 0) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DatabaseBean.close(rs, pstmt, conn);
}
return false;
}
}