package com.student.dao;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.xml.ws.Response;
import com.student.util.DbConnect;
import com.student.vo.Student;
public class StudentDao implements IstudentDao{
protected static final String FIELDS_INSERT = "id,name,sex,age,weight,hight";
// protected static final String FIELDS_RETURN ="id, " + FIELDS_INSERT;
protected static String INSERT_SQL = "insert into stu_info (" + FIELDS_INSERT + ")" + "values (?,?,?,?,?,?)";
protected static String SELECT_SQL = "select " + FIELDS_INSERT + " from stu_info where id=?";
protected static String UPDATE_SQL = "update stu_info set id=?,name=?,sex=?,age=?,weight=?,hight=? where id=?";
protected static String DELETE_SQL = "delete from stu_info where id=?";
@Override
public int add(Student stu) {
Connection con = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
int n = 0;
try {
con = DbConnect.getDBconnection();
prepStmt = con.prepareStatement(INSERT_SQL);
prepStmt.setInt(1,stu.getId());
prepStmt.setString(2, stu.getName());
prepStmt.setString(3, stu.getSex());
prepStmt.setInt(4, stu.getAge());
prepStmt.setFloat(5, stu.getWeight());
prepStmt.setFloat(6, stu.getHight());
n = prepStmt.executeUpdate();
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
DbConnect.closeDB(con, prepStmt, rs);
}
return n;
}
@Override
public List<Student> findall() {
Connection con = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
List<Student> list = new ArrayList<Student>();
con = DbConnect.getDBconnection();
try {
prepStmt = con.prepareStatement("select * from stu_info");
rs = prepStmt.executeQuery();
while (rs.next()) {
Student stu2 = new Student();
stu2.setId(rs.getInt(1));
stu2.setName(rs.getString(2));
stu2.setSex(rs.getString(3));
stu2.setAge(rs.getInt(4));
stu2.setWeight(rs.getFloat(5));
stu2.setHight(rs.getFloat(6));
list.add(stu2);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DbConnect.closeDB(con, prepStmt, rs);
return list;
}
public int deleted(int id)
{
Connection con = null;
Statement prepStmt = null;
int rs=0 ;
con = DbConnect.getDBconnection();
try {
prepStmt = con.createStatement();
rs=prepStmt.executeUpdate("delete from stu_info where id="+id);
} catch (Exception e) {
// TODO: handle exception
}
return rs;
}
public List<Student> find(int id) {
Connection con = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
List<Student> list = new ArrayList<Student>();
con = DbConnect.getDBconnection();
try {
prepStmt = con.prepareStatement(SELECT_SQL);
prepStmt.setInt(1, id);
rs = prepStmt.executeQuery();
while (rs.next()) {
Student stu2 = new Student();
stu2.setId(rs.getInt(1));
stu2.setName(rs.getString(2));
stu2.setSex(rs.getString(3));
stu2.setAge(rs.getInt(4));
stu2.setWeight(rs.getFloat(5));
stu2.setHight(rs.getFloat(6));
list.add(stu2);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DbConnect.closeDB(con, prepStmt, rs);
return list;
}
public int update(Student stu) {
Connection con = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
int n = 0;
try {
con = DbConnect.getDBconnection();
prepStmt = con.prepareStatement(UPDATE_SQL);
prepStmt.setInt(1,stu.getId());
prepStmt.setString(2, stu.getName());
prepStmt.setString(3, stu.getSex());
prepStmt.setInt(4, stu.getAge());
prepStmt.setFloat(5, stu.getWeight());
prepStmt.setFloat(6, stu.getHight());
prepStmt.setInt(7,stu.getId());
n = prepStmt.executeUpdate();
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
DbConnect.closeDB(con, prepStmt, rs);
}
return n;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源说明】 1、该资源内项目代码都是经过测试运行成功,功能正常的情况下才上传的,请放心下载使用。 2、适用人群:主要针对计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、数学、电子信息等)的同学或企业员工下载使用,具有较高的学习借鉴价值。 3、不仅适合小白学习实战练习,也可作为大作业、课程设计、毕设项目、初期项目立项演示等,欢迎下载,互相学习,共同进步!
资源推荐
资源详情
资源评论
收起资源包目录
学生体质健康管理系统源码+项目说明.zip (32个子文件)
code_30312
.classpath 825B
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.jdt.core.prefs 357B
.jsdtscope 555B
org.eclipse.wst.common.component 453B
org.eclipse.wst.common.project.facet.core.xml 335B
org.eclipse.wst.jsdt.ui.superType.container 49B
src
com
student
servlet
AddServlet.java 2KB
update.java 2KB
find.java 2KB
FindAllServlet.java 2KB
delet.java 2KB
dao
IstudentDao.java 280B
StudentDao.java 4KB
util
DbConnect.java 1KB
vo
Student.java 787B
build
classes
com
student
servlet
AddServlet.class 2KB
FindAllServlet.class 2KB
delet.class 2KB
update.class 2KB
find.class 2KB
dao
IstudentDao.class 404B
StudentDao.class 5KB
util
DbConnect.class 2KB
vo
Student.class 1KB
WebContent
WEB-INF
lib
mysql-connector-java-5.1.13-bin.jar 750KB
web.xml 638B
image
3.JPG 2KB
6.jpg 45KB
1.jpg 70KB
META-INF
MANIFEST.MF 36B
.project 872B
共 32 条
- 1
资源评论
龙年行大运
- 粉丝: 1006
- 资源: 3850
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功