package dao.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import bean.Student2;
import dao.Student2Dao;
public class Student2DaoImpl implements Student2Dao {
@Override
public int deleteStudent(int stuno) {
Connection con=null;
PreparedStatement pps=null;
int k=0;
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://192.168.1.40:3306/a1","root","654321");
//得到预状态通道
String sql="delete from student2 where stuno=?";
pps=con.prepareStatement(sql);
pps.setInt(1, stuno);
k=pps.executeUpdate();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(pps!=null){
pps.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return k;
}
@Override
public List findall() {
Connection con=null;
PreparedStatement pps=null;
ResultSet rs=null;
List list=new ArrayList();
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/yhp","root","123456");
//得到预状态通道
String sql="select * from student2";
pps=con.prepareStatement(sql);
rs=pps.executeQuery();
while(rs.next()){
Student2 stu=new Student2();
stu.setStuname(rs.getString("stuname"));
stu.setAddress(rs.getString("address"));
list.add(stu);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(rs!=null){
rs.close();
}
if(pps!=null){
pps.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
@Override
public int insertStudent(Student2 stu) {
Connection con=null;
PreparedStatement pps=null;
int k=0;
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/yhp","root","123456");
//得到预状态通道
String sql="insert into student2 values(null,?,?,?,?,?)";
pps=con.prepareStatement(sql);
pps.setString(1,stu.getStuname());
pps.setInt(2,stu.getAge());
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
pps.setString(3,sf.format(stu.getBirthday()));
pps.setString(4,stu.getAddress());
pps.setString(5,stu.getPassword());
k=pps.executeUpdate();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(pps!=null){
pps.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return k;
}
@Override
public int updateStudent(Student2 stu) {
Connection con=null;
PreparedStatement pps=null;
int k=0;
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/yhp","root","123456");
//得到预状态通道
String sql="update student2 set stuname=?,age=?,birthday=?,address=?,password=? " +
" where stuno=?";
pps=con.prepareStatement(sql);
pps.setString(1,stu.getStuname());
pps.setInt(2,stu.getAge());
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
pps.setString(3,sf.format(stu.getBirthday()));
pps.setString(4,stu.getAddress());
pps.setString(5,stu.getPassword());
pps.setInt(6, stu.getStuno());
k=pps.executeUpdate();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(pps!=null){
pps.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return k;
}
}
|