首页>代码>JSP+Servlet+MySQL数据库增删改查>/SingleJSP/src/com/test/dao/StudentDao.java
package com.test.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.test.model.Student;

public class StudentDao {
	Connection conn = null;
	Statement st = null;
	ResultSet rs = null;
	
	public List<Student> getStudentList(){
		List<Student> list = new ArrayList<Student>();
		String sql = "select id,sno,name,sex,age from student order by id asc";
		conn = JDBCUtilSingle.getInstance().getConnection();
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			Student stu = null;
			while(rs.next()){
				stu = new Student();
				stu.setId(rs.getInt("id"));
				stu.setSno(rs.getString("sno"));
				stu.setName(rs.getString("name"));
				stu.setSex(rs.getString("sex"));
				stu.setAge(rs.getInt("age"));
				list.add(stu);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		JDBCUtilSingle.getInstance().closeConnection(rs, st, conn);
		return list;
	}
	
	public void saveStudent(Student student){
		String sql = "insert into student(sno,name,sex,age) values(?,?,?,?)";
		conn = JDBCUtilSingle.getInstance().getConnection();
		try {
			PreparedStatement  st = conn.prepareStatement(sql);
			st.setString(1, student.getSno());
			st.setString(2, student.getName());
			st.setString(3, student.getSex());
			st.setInt(4, student.getAge());
			st.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		JDBCUtilSingle.getInstance().closeConnection(rs, st, conn);
	}
	
	public Student getStudent(int id){
		Student stu = new Student();
		String sql = "select id,sno,name,sex,age from student where id=?";
		conn = JDBCUtilSingle.getInstance().getConnection();
		try {
			PreparedStatement  st = conn.prepareStatement(sql);
			st.setInt(1, id);
			rs = st.executeQuery();
			while(rs.next()){
				stu.setId(rs.getInt("id"));
				stu.setSno(rs.getString("sno"));
				stu.setName(rs.getString("name"));
				stu.setSex(rs.getString("sex"));
				stu.setAge(rs.getInt("age"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		JDBCUtilSingle.getInstance().closeConnection(rs, st, conn);
		return stu;
	}
	
	public void updateStudent(Student student){
		String sql = "update student set sno=?,name=?,sex=?,age=? where id=?";
		conn = JDBCUtilSingle.getInstance().getConnection();
		try {
			PreparedStatement  st = conn.prepareStatement(sql);
			st.setString(1, student.getSno());
			st.setString(2, student.getName());
			st.setString(3, student.getSex());
			st.setInt(4, student.getAge());
			st.setInt(5, student.getId());
			st.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		JDBCUtilSingle.getInstance().closeConnection(rs, st, conn);
	}
	
	public void deleteStudent(int id){
		String sql = "delete from  student where id=?";
		conn = JDBCUtilSingle.getInstance().getConnection();
		try {
			PreparedStatement  st = conn.prepareStatement(sql);
			st.setInt(1, id);
			st.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		JDBCUtilSingle.getInstance().closeConnection(rs, st, conn);
	}
}
最近下载更多
哪里的完整版  LV8 2024年9月3日
微信网友_7032639073292288  LV1 2024年6月11日
asddwh  LV13 2023年12月29日
去码头整点薯条  LV3 2023年11月15日
ds9009  LV8 2023年8月7日
CH小老虎  LV1 2023年7月4日
sfy_1802661689  LV2 2023年7月3日
qwqwqw12345  LV3 2023年6月20日
ericxu1116  LV24 2023年6月14日
泓鼎168  LV20 2023年6月13日
最近浏览更多
随便取个名字_哈哈  LV27 6月24日
kingwjay  LV2 5月21日
krispeng  LV15 2024年12月31日
124523 2024年12月11日
暂无贡献等级
哪里的完整版  LV8 2024年9月3日
暂无贡献等级
ma406805131  LV19 2024年6月18日
微信网友_7032639073292288  LV1 2024年6月11日
一起加油  LV5 2024年4月19日
 LV7 2024年3月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友