每日总结-23.9.24

package san;

import java.sql.*;

public class Thesql {
    public Connection connect;

    public Thesql() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/web?useUnicode=true&characterEncoding=utf8";
        String username = "root";
        String password = "2223640185";
        connect = DriverManager.getConnection(url, username, password);
    }

    public void finalize() throws Exception {
        connect.close();
    }
    public Pd_stu[] show_stu() throws Exception
    {
        String sql="select * from student";
        PreparedStatement pre = connect.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
        ResultSet rs = pre.executeQuery();
        rs.last();
        int rowCount=rs.getRow();
        rs.beforeFirst();
        Pd_stu[] pdStus = new Pd_stu[rowCount];
        int i=0;
        while(rs.next())
        {
            pdStus[i].id=rs.getInt(1);
            pdStus[i].name=rs.getString(2);
            pdStus[i].phone=rs.getString(3);
            i++;
        }
        pre.close();
        return pdStus;
    }
    public void stu_add(int id,String name,String phone) throws Exception
    {
        String sql = "insert into student(stu_id,stu_name,stu_phone) values(?,?,?);";
        PreparedStatement pre = connect.prepareStatement(sql);
        pre.setInt(1,id);
        pre.setString(2,name);
        pre.setString(3,phone);
        int count=pre.executeUpdate();
        pre.close();
    }
    public boolean stu_modify(int id,String name,String phone) throws Exception
    {
        String sql = "update student set stu_name=?,stu_phone=? where stu_id=?";
        PreparedStatement pre = connect.prepareStatement(sql);
        pre.setString(1, name);
        pre.setString(2, phone);
        pre.setString(3, id+"");
        int count = pre.executeUpdate();
        pre.close();
        return true;
    }
    public Pd_stu find(int id)throws Exception
    {
        Pd_stu pdStu = new Pd_stu();
        String sql="select * from student where stu_id=?";
        PreparedStatement pre = connect.prepareStatement(sql);
        pre.setString(1,id+"");
        ResultSet rs = pre.executeQuery();
        while(rs.next())
        {
            pdStu.id=rs.getInt(1);
            pdStu.name=rs.getString(2);
            pdStu.phone=rs.getString(3);
        }
        pre.close();
        return pdStu;
    }
    public void del(int id)throws Exception
    {
        String sql="delete from student where stu_id=?";
        PreparedStatement pre = connect.prepareStatement(sql);
        pre.setString(1,id+"");
        pre.close();
    }











}

 

posted @ 2023-09-24 21:28  lao_bing  阅读(19)  评论(0)    收藏  举报