2023.12.6每日总结

package shiyan;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AllMethods {
    public Connection connect;
    public AllMethods()throws Exception
    {
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/webshiyan?useUnicode=true&characterEncoding=utf8";
        String username="root";
        String password="qq2665410029";
        connect = DriverManager.getConnection(url,username,password);
    }
    public void finalize() throws Exception
    {
        connect.close();
    }
    public student[] AllStudents()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();//移回去
        student[] students=new student[rowCount];
        int i=0;
        while(rs.next())
        {
            students[i]=new student();
            students[i].id=rs.getInt(1);
            students[i].name=rs.getString(2);
            students[i].sex=rs.getString(3);
            students[i].birthday=rs.getDate(4);
            i++;
        }
        return students;
    }
    public boolean ModifyStudent(int stuId,String name,String sex,String birth)throws Exception
    {
        // 将日期字符串解析为 Date 类型
        Date birthDate = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            birthDate = format.parse(birth);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        String sql="update student set stuname=?,stusex=?,stubirth=? where id=?";
        PreparedStatement pre = connect.prepareStatement(sql);
        pre.setString(1,name);
        pre.setString(2,sex);
        pre.setDate(3,new java.sql.Date(birthDate.getTime()));// 将 Date 转换为 java.sql.Date 类型
        pre.setInt(4,stuId);
        int count=pre.executeUpdate();
        pre.close();
        if(count>=1)
            return true;
        else
            return false;
    }
    public boolean AddStudent(String name,String sex,String birth) throws Exception {

        // 将日期字符串解析为 Date 类型
        Date birthDate = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            birthDate = format.parse(birth);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        //存储到数据库中
        String sql="insert into student(stuname,stusex,stubirth) values(?,?,?)";
        PreparedStatement pre = connect.prepareStatement(sql);
        pre.setString(1,name);
        pre.setString(2,sex);
        pre.setDate(3,new java.sql.Date(birthDate.getTime()));// 将 Date 转换为 java.sql.Date 类型
        int count=pre.executeUpdate();
        pre.close();
        if(count>=1)
        return true;
        else
            return false;
    }
}

 

posted @ 2023-12-06 21:07  超爱彬宝同学  阅读(11)  评论(0)    收藏  举报