这段代码是一个关于教室管理的数据库操作类,主要包含以下几个方法:

  1. get(Integer id) 方法:根据id查询教室信息,并返回一个 Classroom 对象。该方法首先通过 DB.openCn() 方法打开数据库连接,然后使用预编译的SQL语句查询指定id的教室信息。如果查询成功,则将结果集中的数据封装到一个 Classroom 对象中,并返回该对象;否则,返回null。最后使用 DB.closeCn() 方法关闭数据库连接。

  2. add(Classroom classroom) 方法:向数据库中添加一个教室信息,并返回受影响的行数。该方法首先通过 DB.openCn() 方法打开数据库连接,然后使用预编译的SQL语句将教室的名称插入到数据库中。如果插入成功,则返回受影响的行数;否则,返回0。最后使用 DB.closeCn() 方法关闭数据库连接。

  3. edit(Classroom classroom) 方法:修改数据库中的教室信息,并返回受影响的行数。该方法首先通过 DB.openCn() 方法打开数据库连接,然后使用预编译的SQL语句根据教室的id修改教室的名称。如果修改成功,则返回受影响的行数;否则,返回0。最后使用 DB.closeCn() 方法关闭数据库连接。

  4. del(Integer id) 方法:根据id删除数据库中的教室信息,并返回受影响的行数。该方法首先通过 DB.openCn() 方法打开数据库连接,然后使用预编译的SQL语句根据教室的id删除教室信息。如果删除成功,则返回受影响的行数;否则,返回0。最后使用 DB.closeCn() 方法关闭数据库连接。

其中,DB.openCn()DB.closeCn() 是打开和关闭数据库连接的方法,具体实现不在代码中给出。另外,该代码中使用了 PreparedStatement 对象来执行SQL语句,可以有效防止SQL注入攻击,提高了代码的安全性。

@Override
public Classroom get(Integer id) {

    Classroom classroom = null;
    Connection cn = DB.openCn();//开启数据库
    String sql = 'select id,name from classroom where id=?';
    PreparedStatement ps = null;//当预编译对象值为null时
    try {
        ps = cn.prepareStatement(sql);
        ps.setInt(1, id);
    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println('查询所有SQL语句错误');
        DB.closeCn();
        return classroom;
    }
    ResultSet rs = null;
    try {
        rs = ps.executeQuery();
    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println('执行查询所有SQL语句错误');
        DB.closeCn();
        return classroom;
    }

    try {
        while (rs.next()) {
            int idTemp = rs.getInt('id');
            String name = rs.getString('name');
            classroom = new Classroom(idTemp, name);

        }
    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println('结果集遍历错误');
        DB.closeCn();
        return classroom;
    }
    DB.closeCn();
    return classroom;
}


@Override
public Integer add(Classroom classroom) {
    int rows = 0;
    Connection cn = DB.openCn();
    String sql = 'insert into classroom (name) values(?)';
    PreparedStatement ps = null;
    try {
        ps = cn.prepareStatement(sql);
        ps.setString(1, classroom.getName());
    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println('添加SQL语句错误');
        DB.closeCn();
        return rows;
    }

    try {
        rows = ps.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println('执行添加SQL语句错误');
        DB.closeCn();
        return rows;
    }

    DB.closeCn();
    return rows;
}

@Override
public Integer edit(Classroom classroom) {
    int rows = 0;
    Connection cn = DB.openCn();
    String sql = 'update classroom set name=? where id=?';
    PreparedStatement ps = null;
    try {
        ps = cn.prepareStatement(sql);
        ps.setString(1, classroom.getName());
        ps.setInt(2, classroom.getId());
    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println('修改SQL语句错误');
        DB.closeCn();
        return rows;
    }

    try {
        rows = ps.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println('执行修改SQL语句错误');
        DB.closeCn();
        return rows;
    }
    DB.closeCn();
    return rows;
}

@Override
public Integer del(Integer id) {
    int rows = 0;
    Connection cn = DB.openCn();
    String sql = 'delete from classroom where id=?';
    PreparedStatement ps = null;
    try {
        ps = cn.prepareStatement(sql);
        ps.setInt(1, id);
    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println('删除SQL语句错误');
        DB.closeCn();
        return rows;
    }

    try {
        rows = ps.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println('执行删除SQL语句错误');
        DB.closeCn();
        return rows;
    }
    DB.closeCn();
    return rows;
}
Java 教室管理数据库操作类代码示例

原文地址: https://www.cveoy.top/t/topic/qFkw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录