Java学生信息管理系统——3 *从数据库中获取,更新数据*

这篇博客讲述了如何在Java中实现学生信息管理系统的数据操作,包括通过接口CollegeDao和其实现类CollegeDaolmpl进行查询和更新数据。在测试过程中遇到了MySQL语法错误,经过排查发现是SQL语句中的拼写错误,修复后成功运行。
摘要由CSDN通过智能技术生成

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. 利用当前设置对象
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值