首页>代码>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);
	}
}
最近下载更多
asddwh  LV12 2023年12月29日
去码头整点薯条  LV3 2023年11月15日
ds9009  LV7 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  LV19 2023年6月13日
微信网友_6470947892727808  LV1 2023年5月11日
kxjh星辰  LV6 2022年12月28日
最近浏览更多
一起加油  LV4 4月19日
 LV7 3月29日
tkggddm  LV3 1月27日
xiongwei11231  LV8 1月15日
Kaiaahh  LV1 1月5日
2131234536546  LV7 1月4日
susitian  LV2 2023年12月28日
asddwh  LV12 2023年12月25日
ddzfgh  LV1 2023年12月25日
颜菜菜  LV2 2023年12月19日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友