每日总结-23.10.17

package wangzhan;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Notice {
    public Connection connect;
    public Notice()throws Exception
    {
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/pddabc?useUnicode=true&characterEncoding=utf8";
        String username="root";
        String password="2223640185";
        connect = DriverManager.getConnection(url,username,password);
    }
    public void finalize() throws Exception
    {
        connect.close();
    }
    /* notice add
     * 消息增加,将消息信息添加到数据库
     * 输入参数为:学生id,消息类型,消息所属类型的id
     * 例如,学生id为20210001,即学号;消息类型为“发布新作业”,则消息所属类型id为该作业的id
     * */
    public void add(String stu_id,String type,String type_id) throws Exception
    {
        String sql = "insert into notice(notice_student_id,notice_type,notice_type_id,notice_isview) values(?,?,?,?);";
        PreparedStatement pre = connect.prepareStatement(sql);
        pre.setString(1,stu_id);
        pre.setString(2,type);
        pre.setString(3,type_id);
        pre.setString(4,"未查看");
        int count=pre.executeUpdate();
        pre.close();
    }
    /* notice batch add
     * 消息批量增加,将消息信息批量添加到数据库
     * 输入参数为:专业,消息类型,消息所属类型的id
     * 为所有属于该专业的学生添加新消息
     * 例如,专业为major2,即专业;消息类型为“发布新作业”,则消息所属类型id为该作业的id,结果是为所有专业为major2的学生添加信息
     * 参考add函数
     * */
    public void batch_add(String major,String type,String type_id) throws Exception
    {
        Thesql thesql=new Thesql();
        Pd_stu[] pdStus = thesql.stu_DimQuery_major(major);
        for(int i=0;i<pdStus.length;i++)
        {
           add(pdStus[i].getId()+"",type,type_id);
        }
    }
    /* notice query
     * 消息信息查询,将消息信息查询并返回
     * 输入参数为:id(String)即消息id
     * 返回类型为Pd_notice,包括int id;String stu_id;String type;String type_id;String isView;
     * */
    public Pd_notice query(String id) throws Exception
    {
        String sql="select * from notice where notice_id = ?";
        PreparedStatement pre = connect.prepareStatement(sql);
        pre.setString(1,id);
        ResultSet rs = pre.executeQuery();
        Pd_notice pdd = new Pd_notice();
        while(rs.next())
        {
            pdd.setId(rs.getInt(1));
            pdd.setStu_id(rs.getString(2));
            pdd.setType(rs.getString(3));
            pdd.setType_id(rs.getString(4));
            pdd.setIsView(rs.getString(5));
        }
        pre.close();
        if(pdd.getId()!=0)
            return pdd;
        else
            return null;
    }

 

posted @ 2023-10-17 07:42  lao_bing  阅读(29)  评论(0)    收藏  举报