package cn.fjnu.edu.daoimpl;
import cn.fjnu.edu.dao.StudentDao;
import cn.fjnu.edu.model.Student;
import cn.fjnu.edu.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class StuDaoImpl implements StudentDao {
final String strCreate = "insert into stuinfo values(?,?,?,?,?,?,?)";
final String strDelete = "delete from stuinfo where 1=1";
final String strUpdate = "update stuinfo set";
final String strFind = "select * from stuinfo where Name like ? or Dep like ? or Id like ? or Age like ? or Sex like ? or Phone like ? or Email like ?";
final String strLogin = "select Name from stuinfo where id=? and Phone=?";
@Override
public boolean Create(Student student) throws Exception {
DBUtil msh = new DBUtil();
Connection conn = msh.getConnection();
PreparedStatement pstmt = conn.prepareStatement(strCreate);
pstmt.setString(1, student.getId());
pstmt.setString(2, student.getName());
pstmt.setInt(3, student.getAge());
pstmt.setString(4, student.getDep());
pstmt.setString(5, student.getSex());
pstmt.setString(6, student.getPhone());
pstmt.setString(7, student.getEmail());
int i = pstmt.executeUpdate();
pstmt.close();
if (i > 0)
return true;
else
return false;
}
@Override
public boolean Update(Student student) throws Exception {
DBUtil msh = new DBUtil();
Connection conn = msh.getConnection();
Statement stmt = conn.createStatement();
String str = strUpdate;
if (!(student.getName().equals(null))) {
str += " Name='" + student.getName() + "' ";
}
if (!(student.getDep().equals(null)))
str += ",Dep='" + student.getDep() + "'";
if (!(student.getSex().equals(null)))
str += ",Sex='" + student.getSex() + "'";
if (!(student.getPhone().equals(null)))
str += ",Phone='" + student.getPhone() + "'";
if (!(student.getEmail().equals(null)))
str += ",Email='" + student.getEmail() + "'";
if (student.getAge() != 0) {
str += ",Age=" + student.getAge() + "";
}
str += " where Id=" + student.getId() + ";";
System.out.println(str);
int i = stmt.executeUpdate(str);
stmt.close();
msh.closeConnection(conn);
if (i > 0)
return true;
else {
System.out.println(i + " errorD");
return false;
}
}
@Override
public boolean Delete(Student student) throws Exception {
DBUtil msh = new DBUtil();
Connection conn = msh.getConnection();
Statement stmt = conn.createStatement();
String str = strDelete;
if (!("".equals(student.getId())))
str += " and Id=" + student.getId();
int i = stmt.executeUpdate(str);
stmt.close();
msh.closeConnection(conn);
if (i > 0)
return true;
else
return false;
}
@Override
public boolean findLogin(Student student) throws Exception {
DBUtil msh = new DBUtil();
Connection conn = msh.getConnection();
PreparedStatement pstmt = conn.prepareStatement(strCreate);
boolean flag = false;
try {
pstmt = conn.prepareStatement(strLogin);
pstmt.setString(1, student.getId());
pstmt.setString(2, student.getPhone());
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
student.setName(rs.getString(1));
flag = true;
}
} catch (Exception e) {
throw e;
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e) {
throw e;
}
}
}
return flag;
}
@Override
public List<Student> findAll(String keyWord) throws Exception {
List<Student> all = new ArrayList<Student>();
DBUtil msh = new DBUtil();
Connection conn = msh.getConnection();
PreparedStatement pstmt = conn.prepareStatement(strFind);
pstmt.setString(1, "%" + keyWord + "%");
pstmt.setString(2, "%" + keyWord + "%");
pstmt.setString(3, "%" + keyWord + "%");
pstmt.setString(4, "%" + keyWord + "%");
pstmt.setString(5, "%" + keyWord + "%");
pstmt.setString(6, keyWord);
pstmt.setString(7, keyWord);
ResultSet rs = pstmt.executeQuery();
Student people = null;
while (rs.next()) {
people = new Student();
people.setId(rs.getString(1));
people.setName(rs.getString(2));
people.setAge(rs.getInt(3));
people.setDep(rs.getString(4));
people.setSex(rs.getString(5));
people.setPhone(rs.getString(6));
people.setEmail(rs.getString(7));
all.add(people);
}
pstmt.close();
msh.closeConnection(conn);
return all;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页