
package dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import dao.BaseDao;
import dao.IStudentInfoDao;
import entity.StudentInfo;
public class StudentInfoDaoImpl extends BaseDao implements IStudentInfoDao {
//添加
public int insert(StudentInfo stu) {
String sql="insert into dbo.StudentInfo values(?,?,?,?,?)";
int i=0;
try {
ps=getCon().prepareStatement(sql);
ps.setString(1,stu.getSname());
ps.setString(2,stu.getSgender());
ps.setInt(3,stu.getSage());
ps.setString(4,stu.getSaddress());
ps.setString(5,stu.getSemail());
i=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
this.closeAll(con, ps, rs);
}
return i;
}
//删除
public int delete(StudentInfo stu){
String sql="delete from StudentInfo where sid=?";
int i=0;
try {
ps = getCon().prepareStatement(sql);
ps.setInt(1, stu.getSid());
i=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return i;
}
//修改:
public int update(StudentInfo stu) {
String sql="update StudentInfo set sname=?,sgender=?,sage=? where sid=?";
int i=0;
try {
ps=getCon().prepareStatement(sql);
ps.setString(1, stu.getSname());
ps.setString(2, stu.getSgender());
ps.setInt(3, stu.getSage());
ps.setInt(4, stu.getSid());
i=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeAll(con, ps, rs);
}
return i;
}
//查询
public StudentInfo delectByid(StudentInfo stu) {
String sql="select * from dbo.StudentInfo where sid=?";
try {
ps=getCon().prepareStatement(sql);
ps.setInt(1,stu.getSid());
rs=ps.executeQuery();
if(rs.next()){
StudentInfo stuinfo = null;
stuinfo.setSid(rs.getInt(1));
stuinfo.setSname(rs.getString(2));
stuinfo.setSgender(rs.getString(3));
stuinfo.setSage(rs.getInt(4));
stuinfo.setSaddress(rs.getString(5));
stuinfo.setSemail(rs.getString(6));
}
} catch (SQLException e) {
e.printStackTrace();
}
return (StudentInfo) rs;
}
//查询:根据ID查单个
public ArrayList<StudentInfo> selectAll() {
ArrayList<StudentInfo> list=new ArrayList<StudentInfo>();
try {
String sql="select * from StudentInfo";
ps=getCon().prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
StudentInfo stu=new StudentInfo();
stu.setSid(rs.getInt(1));
stu.setSname(rs.getString(2));
stu.setSgender(rs.getString(3));
stu.setSage(rs.getInt(4));
stu.setSaddress(rs.getString(5));
stu.setSemail(rs.getString(6));
list.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import dao.BaseDao;
import dao.IStudentInfoDao;
import entity.StudentInfo;
public class StudentInfoDaoImpl extends BaseDao implements IStudentInfoDao {
//添加
public int insert(StudentInfo stu) {
String sql="insert into dbo.StudentInfo values(?,?,?,?,?)";
int i=0;
try {
ps=getCon().prepareStatement(sql);
ps.setString(1,stu.getSname());
ps.setString(2,stu.getSgender());
ps.setInt(3,stu.getSage());
ps.setString(4,stu.getSaddress());
ps.setString(5,stu.getSemail());
i=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
this.closeAll(con, ps, rs);
}
return i;
}
//删除
public int delete(StudentInfo stu){
String sql="delete from StudentInfo where sid=?";
int i=0;
try {
ps = getCon().prepareStatement(sql);
ps.setInt(1, stu.getSid());
i=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return i;
}
//修改:
public int update(StudentInfo stu) {
String sql="update StudentInfo set sname=?,sgender=?,sage=? where sid=?";
int i=0;
try {
ps=getCon().prepareStatement(sql);
ps.setString(1, stu.getSname());
ps.setString(2, stu.getSgender());
ps.setInt(3, stu.getSage());
ps.setInt(4, stu.getSid());
i=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeAll(con, ps, rs);
}
return i;
}
//查询
public StudentInfo delectByid(StudentInfo stu) {
String sql="select * from dbo.StudentInfo where sid=?";
try {
ps=getCon().prepareStatement(sql);
ps.setInt(1,stu.getSid());
rs=ps.executeQuery();
if(rs.next()){
StudentInfo stuinfo = null;
stuinfo.setSid(rs.getInt(1));
stuinfo.setSname(rs.getString(2));
stuinfo.setSgender(rs.getString(3));
stuinfo.setSage(rs.getInt(4));
stuinfo.setSaddress(rs.getString(5));
stuinfo.setSemail(rs.getString(6));
}
} catch (SQLException e) {
e.printStackTrace();
}
return (StudentInfo) rs;
}
//查询:根据ID查单个
public ArrayList<StudentInfo> selectAll() {
ArrayList<StudentInfo> list=new ArrayList<StudentInfo>();
try {
String sql="select * from StudentInfo";
ps=getCon().prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
StudentInfo stu=new StudentInfo();
stu.setSid(rs.getInt(1));
stu.setSname(rs.getString(2));
stu.setSgender(rs.getString(3));
stu.setSage(rs.getInt(4));
stu.setSaddress(rs.getString(5));
stu.setSemail(rs.getString(6));
list.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
