java期末复习

考试题型

30(选择题)+15(基本编程)+15(面向对象编程)+10(文件编程)+30(综合编程)

综合编程题

1.JDBCDruidutils里面的几个参数,最基本的4大参数

driverClassName=com.mysql.cj.jdbc.Driver//注册加载驱动类
url =jdbc:mysql://localhost:3306/ttt//数据库连接地址
username=root//数据库用户名
password=123456//数据库密码

其他参数

initialSize=5//初始化连接数量
maxActive=10//最大连接数
maxWait=3000//最大等待时间

2.Bean类

public class Customer {
    private int id;
    private String name;
    private String email;
    private Date birth;

    public Customer() {
    }
    public Customer(String name, String email, Date birth) {
        this.name = name;
        this.email = email;
        this.birth = birth;
    }
    public Customer(int id, String name, String email, Date birth) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.birth = birth;
    }
    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 getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", birth=" + birth +
                '}';
    }

3.控制层AddCustomerServlet(增)

@WebServlet("/addCustomerServlet")
public class AddCustomerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          this.doPost(request,response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //重点代码
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String birth1 = request.getParameter("birth");
        Date birth = JDBCUtils.changeToDate(birth1);
        Customer customer = new Customer(name,email,birth);
        CustomerDao dao = new CustomerDaoImpl();
        dao.insert(customer);
        System.out.println("add success");
        request.getRequestDispatcher("/index.jsp").forward(request,response);

控制层DeleteCustomerServlet(删)

public class DeleteCustomerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String id = request.getParameter("id");
        CustomerDao dao = new CustomerDaoImpl();
        dao.deleteById(Integer.parseInt(id));
        request.getRequestDispatcher("/listCustomerServlet").forward(request,response);
    }

控制层UpdateCustomerServlet(改)

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String birth1 = request.getParameter("birth");
        Date birth = JDBCUtils.changeToDate(birth1);

        Customer customer = new Customer(Integer.parseInt(id),name,email,birth);

        CustomerDao dao = new CustomerDaoImpl();
        dao.update(customer);
        //System.out.println("update success");
        request.getRequestDispatcher("/listCustomerServlet").forward(request,response);
    }

控制层ListCustomerServlet(查)

public class ListCustomerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        CustomerDao dao = new CustomerDaoImpl();
        List<Customer> cs = dao.getAll();
        request.setAttribute("custs",cs);
        request.getRequestDispatcher("/listcustomer.jsp").forward(request,response);
    }

4.读取jdbc配置文件

   private static Properties pro = new Properties();//读取Java的配置文件

    static {
        try {
            //读取指定文件,返回一个输入流InputStream对象
            InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
            pro.load(in);
            //重点代码
            driver =pro.getProperty("jdbc.driver");
            url =pro.getProperty("jdbc.url");
            username =pro.getProperty("jdbc.username");
            password =pro.getProperty("jdbc.pwd");//获取系统变量
            Class.forName(driver);
        } catch (Exception e) {
//            e.printStackTrace();
            throw new ExceptionInInitializerError(e);
        }
    }

5.获取数据库连接

    public static Connection getConnection(){
        Connection conn =null;
        try {
            conn = DriverManager.getConnection(url,username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
        public static void close(Connection conn, Statement st, ResultSet rs){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

6.网络实现类CustomerDaoImpl(增)

    public void add(Customer cust) {
        String sql="insert into customers(name,email,birth) values(?,?,?)";
        Connection conn =null;
        PreparedStatement ps =null;
        try {
             conn = JDBCUtils.getConnection();//执行sql语句的一个接口
             ps = conn.prepareStatement(sql);
             ps.setString(1, cust.getName());//1表示第一个?
             ps.setString(2,cust.getEmail());
             ps.setDate(3,cust.getBirth());//注意cust类名可能会换
             ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(conn,ps,null);
        }

网络实现类CustomerDaoImpl(删)

public void delete(int id) {
        String sql="delete from customers where id=?";
        Connection conn =null;
        PreparedStatement ps =null;
        try {
            conn = JDBCUtils.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(conn,ps,null);
        }

网络实现类CustomerDaoImpl(改)

    public void update(Customer cust) {
        String sql="update customers set name=?,email=?,birth=? where id=?";
        Connection conn =null;
        PreparedStatement ps =null;
        try {
            conn = JDBCUtils.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setString(1, cust.getName());
            ps.setString(2,cust.getEmail());
            ps.setDate(3,cust.getBirth());
            ps.setInt(4,cust.getId());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(conn,ps,null);
        }

网络实现类CustomerDaoImpl(查)

public Customer queryByid(int id) {//单条查询
        Connection conn =null;
        PreparedStatement ps =null;
        ResultSet rs =null;//定义出这个ResultSet的对象rs
        String sql = "select * from customers where id=?";
        try {
            conn =JDBCUtils.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();//用来执行查询语句
            Customer cust =null;
            if(rs.next()){
                cust = new Customer(rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("email"),
                        rs.getDate("birth"));
            }
            return cust;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(conn,ps,rs);
        }
        return null;
    }

小考点:http使用了TCP/IP协议

选择题

1.java 中,用(A)关键字定义常量
A.final B.#define C.float D.const

3.下列语句中,属于多分支语句的是B
A.if 语句B.switch 语句C.do while 语句D.for 语句

4.以下哪个方法用于定义线程的执行体?C
A.start() B.init() C.run() D.main()

5.Thread 类的方法中,toString()方法的作用是(B)
A.只返回线程的名称 B.返回当前线程所属的线程组的名称 C.返回当前线程对象
D.返回线程的名称

6.与Applet 生命周期有关的主要方法是(D)
A.init() B.start() C.stop() D.以上都是

第五章

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

第六章

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

第七章

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

第八章

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

第九章

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

第十章

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

第十一章

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

第十二章

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值