1.创建接口CollegeDao
这是位置
代码
package net.lzz.student.dao;
import net.lzz.student.bean.College;
public interface CollegeDao {
College findById(int id);// 按编号查询状态记录
int update(College college);// 更新状态记录
}
2.创建类CollegeDaolmpl
创建 impl 包用来装她
package net.lzz.student.dao.impl;
import net.lzz.student.bean.College;
import net.lzz.student.dao.CollegeDao;
import net.lzz.student.dbutil.ConnectionManager;
import java.sql.*;
public class CollegeDaolmpl implements CollegeDao {
@Override
public College findById(int id) {
// 声明 学校对象
College college = null;
// 1.获取数据库连接
Connection conn = ConnectionManager.getConnection();
// 2.mysql语句
String strSQl = "select * from t_college where id = ?";
try {
// 3.创建MySQL语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQl);
// 4.设置占位符的值
pstmt.setInt(1, id);
// 5.执行MySQL
ResultSet rs = pstmt.executeQuery();
// 6.判断是否为空
if (rs.next()){
// 7.创建学校对象
college = new College();
// 8. 利用当前设置对象