package net.hw.student.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import net.hw.student.bean.College;
import net.hw.student.dao.CollegeDao;
import net.hw.student.dbutil.ConnectionManager;
public class CollegeDaoImpl implements CollegeDao{
@Override
public College findById(int id){
College college = null;
Connection conn = ConnectionManager.getConnection();
String strSQL = “select * from t_college where id = ?”;
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
college = new College();
college.setId(rs.getInt(“id”));
college.setName(rs.getString(“name”));
college.setPresident(rs.getString(“president”));
college.setStartTime(rs.getDate(“start_time”));
college.setTelephone(rs.getString(“telephone”));
college.setEmail(rs.getString(“email”));
college.setAddress(rs.getString(“address”));
college.setProfile(rs.getString(“profile”));
}
}catch (SQLException e) {
e.printStackTrace();
}finally {
ConnectionManager.closeConnection(conn);
}
return college;
}
@Override
public int update(College college){
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = “UPDATE t_college SET name = ?, president = ?, start_time = ?,”
+ " telephone = ?, email = ?, address = ?, profile = ? WHERE id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, college.getName());
pstmt.setString(2, college.getPresident());
pstmt.setTimestamp(3, new Timestamp(college.getStartTime().getTime()));
pstmt.setString(4, college.getTelephone());
pstmt.setString(5, college.getEmail());
pstmt.setString(6, college.getAddress());
pstmt.setString(7, college.getProfile());
pstmt.setInt(8, college.getId());
count = pstmt.executeUpdate();
pstmt.close();
}catch (SQLException e) {
e.printStackTrace();
}finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
}
package net.hw.student.dao.impl;
import net.hw.student.bean.Status;
import net.hw.student.dao.StatusDao;
import net.hw.student.dbutil.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class StatusDaoImpl implements StatusDao {
@Override
public Status findById(int id){
Status status = null;
Connection conn = ConnectionManager.getConnection();
String strSQL = “SELECT * FROM t_status WHERE id = ?”;
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
status = new Status();
status.setId(rs.getInt("id"));
status.setCollege(rs.getString("college"));
status.setVersion(rs.getString("version"));
status.setAuthor(rs.getString("author"));
status.setTelephone(rs.getString("telephone"));
status.setAddress(rs.getString("address"));
status.setEmils(rs.getString("email"));
}
pstmt.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return status;
}
@Override
public int update(Status status) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "UPDATE t_status SET college = ?, version = ?, author = ?,"
+ " telephone = ?, address = ?, email = ? WHERE id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, status.getCollege());
pstmt.setString(2, status.getVersion());
pstmt.setString(3, status.getAuthor());
pstmt.setString(4, status.getTelephone());
pstmt.setString(5, status.getAddress());
pstmt.setString(6, status.getEmils());
pstmt.setInt(7, status.getId());
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
}