软件工程开学第一次测试

package bean;

public class user {
    private  int id;
    private String name;
    private String mima;
    private String phone;
    private String danwei;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMima() {
        return mima;
    }

    public void setMima(String mima) {
        this.mima = mima;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getDanwei() {
        return danwei;
    }

    public void setDanwei(String danwei) {
        this.danwei = danwei;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}
package bean;

public class xuqiu {
    private  int id;
    private String people;
    private  String name;
    private  String gaishu;
    private  String leixing;
    private  String moshi;
    private  String touzi;
    private  String data;
    private  String zhuangtai;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGaishu() {
        return gaishu;
    }

    public void setGaishu(String gaishu) {
        this.gaishu = gaishu;
    }

    public String getLeixing() {
        return leixing;
    }

    public void setLeixing(String leixing) {
        this.leixing = leixing;
    }

    public String getMoshi() {
        return moshi;
    }

    public void setMoshi(String moshi) {
        this.moshi = moshi;
    }

    public String getTouzi() {
        return touzi;
    }

    public void setTouzi(String touzi) {
        this.touzi = touzi;
    }

    public String getPeople() {
        return people;
    }

    public void setPeople(String people) {
        this.people = people;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getZhuangtai() {
        return zhuangtai;
    }

    public void setZhuangtai(String zhuangtai) {
        this.zhuangtai = zhuangtai;
    }
}
package dao;

import bean.user;
import bean.xuqiu;
import util.util;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class dao {
    public Connection sjk()
    {
        Connection connect;
        try {
            connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/xuqiuzhengji?&useSSL=false&serverTimezone=UTC","root","123456");
            return connect;
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }


    public boolean queryEmpty(String name,String phone,String danwei,String mima)
    {
        boolean flag=false;
        if(phone==null||"".equals((phone))&&name==null||"".equals(name)&&danwei==null||"".equals(danwei)&&mima==null||"".equals(mima))
        {
            flag=true;
        }
        return flag;
    }
    public Connection sj()
    {
        Connection connect;
        try {
            connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/xuqiuzhengji?&useSSL=false&serverTimezone=UTC","root","123456");
            return connect;
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }


    //查询旧密码是否正确
    public boolean judgemima(String name,String mima)
    {
        boolean flag=false;
        Connection connection=util.getConnection();
        ResultSet rs=null;
        String sql="select * from user where name="+name;
        try {
            Statement statement=connection.createStatement();
            rs=statement.executeQuery(sql);
            if(rs.next()) {
                String mima2 =(rs.getString("mima"));
                if (mima2.equals(mima))
                    flag=true;
            }
            util.close(rs,statement,connection);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return flag;
    }
    //修改顾客密码
    public void mimaxiugai(String mima)
    {
        try {
            PreparedStatement preparedStatement=null;
            Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/xuqiuzhengji?&useSSL=false&serverTimezone=UTC","root","123456");
            preparedStatement=connect.prepareStatement("Update user set mima=(mima)");
            preparedStatement.setString(2,mima);
            preparedStatement.executeUpdate();
            preparedStatement.execute();
            connect.close();
        }catch(SQLException e){
            e.printStackTrace();
        }
    }

    //录入顾客input()
    public void input(String name,String phone,String danwei,String mima)
    {
        try {
            PreparedStatement preparedStatement=null;
            Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/xuqiuzhengji?&useSSL=false&serverTimezone=UTC","root","123456");
            preparedStatement=connect.prepareStatement("insert into user (name,mima,phone,danwei) values (?,?,?,?)");
            preparedStatement.setString(1,name);
            preparedStatement.setString(2,mima);
            preparedStatement.setString(3,phone);
            preparedStatement.setString(4,danwei);

            preparedStatement.executeUpdate();
            connect.close();
            preparedStatement.close();
        }catch(SQLException e){
            e.printStackTrace();
        }
    }


    //录入需求input2()
    public void input2(String name,String gaishu,String leixing,String moshi,String touzi,String data,String zhuangtai,String people){
        try {
            PreparedStatement preparedStatement=null;
            Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/xuqiuzhengji?&useSSL=false&serverTimezone=UTC","root","123456");
            preparedStatement=connect.prepareStatement("insert into xuqiu (name,gaishu,leixing,moshi,touzi,people,data,zhuangtai) values (?,?,?,?,?,?,?,?)");
            preparedStatement.setString(1,name);
            preparedStatement.setString(2,gaishu);
            preparedStatement.setString(3,leixing);
            preparedStatement.setString(4,moshi);
            preparedStatement.setString(5,touzi);
            preparedStatement.setString(6,people);
            preparedStatement.setString(7,data);
            preparedStatement.setString(8,zhuangtai);

            preparedStatement.executeUpdate();
            connect.close();
            preparedStatement.close();
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
    //显示所有需求信息
    public static xuqiu gerenxuqiu(String name) {
        List<xuqiu> list = new ArrayList<xuqiu>();
        Connection con=util.getConnection();
        ResultSet rs=null;
        String sql="select * from xuqiu where people="+name;
        try {//连接数据库的操作
            Statement statement=con.createStatement();
            rs=statement.executeQuery(sql);
            while (rs.next()) {
                xuqiu x = new xuqiu();
                x.setId(Integer.parseInt(rs.getString("id")));
                x.setName(rs.getString("name"));  ;
                x.setData(rs.getString("data"));
                x.setZhuangtai(rs.getString("zhuangtai"));

                list.add(x);
            }
            util.close(rs, statement, con);
        }catch(Exception e){
            e.printStackTrace();
        }
        return (xuqiu) list;
    }

    //显示用户需求信息
    public static xuqiu getxuqiu(String name) {
        xuqiu x = new xuqiu();
        Connection connection=util.getConnection();
        ResultSet rs=null;
        String sql="select * from xuqiu where name="+name;
        try {
            Statement statement=connection.createStatement();
            rs=statement.executeQuery(sql);
            if(rs.next()) {
                x.setName(rs.getString("name"));  ;
                x.setGaishu(rs.getString("gaishu"));
                x.setLeixing(rs.getString("leixing"));
                x.setMoshi(rs.getString("moshi"));
                x.setTouzi(rs.getString("touzi"));
                x.setData(rs.getString("data"));
                x.setZhuangtai(rs.getString("zhuangtai"));

            }
            util.close(rs,statement,connection);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return x;
    }



    //判断顾客登录信息是否正确judgeguk()
    public int judge(String name,String mima)
    {
        int flag=0;
        try {
            Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/xuqiuzhengji?&useSSL=false&serverTimezone=UTC","root","123456");
            Statement stmt = connect.createStatement();
            ResultSet rs = stmt.executeQuery("select * from user");
            while(rs.next())
            {
                if((name.equals(rs.getString("name")))&& mima.equals(rs.getString("mima")))
                {
                    flag=1;
                }
                else if (name.equals(rs.getString("name"))&& !mima.equals(rs.getString("mima")))
                {
                    flag=2;
                }
            }
            rs.close();
            connect.close();
            stmt.close();
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return  flag;
    }

}
package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class util {

    private static String mysqlname = "xuqiuzhengji";
    private static Connection con;
    private static Statement sta;
    private static ResultSet re;
    private static String coursename = "com.mysql.cj.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/" + mysqlname + " ?serverTimezone=GMT%2B8";


    public static Connection getConnection() {
        try {
            Class.forName(coursename);
            System.out.println("连接成功");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            con = DriverManager.getConnection(url, "root", "123456");
            System.out.println("LIANJIE");
        } catch (Exception e) {
            e.printStackTrace();
            con = null;
        }
        return con;
    }

    public static void close(Statement state, Connection conn) {
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(ResultSet rs, Statement state, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

 

posted @ 2022-03-13 20:22  清梦韶华  阅读(24)  评论(0编辑  收藏  举报