业务流程

    习惯 2019/12/9 19:24:57
package com.serve;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.Connect;
import com.dao.register;


@WebServlet("rvlet")
public class servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       

    public servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
        request.setCharacterEncoding("utf-8");
        String name=request.getParameter("uname");
        String pwd=request.getParameter("upwd");
        String zhiwei=request.getParameter("uzhiwei");
        if (register.add(name,pwd,zhiwei)>0) {
            System.out.println("信息添加成功");
            response.sendRedirect("dl.jsp");
        }else {
            System.out.println("信息添加失败");
            response.sendRedirect("del.jsp");
        }
        
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

    习惯 2019/12/9 19:25:08
ublic class register {
public static int add(String name,String pwd,String zhiwei) {
    Connection conn=Connect.getConn();
    Statement st=null;
    int row=0;
    try {
        st=conn.createStatement();
        String sql="insert into admin(name,admin,zhiwei) values('"+name+"','"+pwd+"','"+zhiwei+"')";
        row=st.executeUpdate(sql);
    } catch (Exception e) {
        // TODO: handle exception
    }finally {
        Connect.close(conn, st, null);
    }
    return row;
}

public static ResultSet login(String name,String pwd,String zhiwei) {
    Connection conn=Connect.getConn();
    Statement st=null;
    ResultSet rs=null;
    int row=0;
    try {
        st=conn.createStatement();
        String sql="select * from admin where zhiwei like '%" + zhiwei + "%'";
        row=st.executeUpdate(sql);
        if (row>0) {
            rs=st.executeQuery(sql);
        }else {
            rs=null;
        }
        
    } catch (Exception e) {
        // TODO: handle exception
    }finally {
        Connect.close(conn, st, rs);
    }
    return rs;
    
}

}

    习惯 2019/12/9 19:25:31
public class Connect {
    public static Connection getConn()
    {
        //第一步:加载驱动类,如果有异常则抛出异常或者使用try..catch..语句处理
        try {
            Class.forName("com.mysql.jdbc.Driver");
        }catch(ClassNotFoundException e){
            e.printStackTrace();
            System.out.println("驱动类加载失败");
            //这里可能报错:ClassNotFoundException 
            //错误原因:可能是驱动类的名称拼写错误,也可能是jar包没导入
        }
        
        //第二步:获取连接对象
        String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
        String username = "root";
        String password = "ly0825";
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url,username,password);
        }catch(SQLException e)
        {
            e.printStackTrace();
            System.out.println("连接对象获取失败");
        }
        //返回一个连接对象
        return conn;
    }
    public  static void close(Connection conn, Statement st,ResultSet rs) {
        if (rs!=null) {
            try {
                rs.close();
                rs=null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (st!=null) {
            try {
                st.close();
                st=null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (conn!=null) {
            try {
                conn.close();
                conn=null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

posted @ 2019-12-09 19:43  程序员亮亮丫!!  阅读(194)  评论(0编辑  收藏  举报