执行数据库8步骤

 操作数据库,拼接SQL语句,执行SQL语句
        /**
         * 操作数据库步骤
         * 3.1导入操作数据库的jar
         * 3.2数据库驱动
         * 3.3创建连接对象
         * 3.4定义SQL语句
         * 3.5创建执行SQL语句的对象
         * 3.6执行SQL语句
         * 3.7处理执行SQL语句结果
         * 3.8关闭数据库,释放资源
         * 
         */

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

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

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet({ "/LoginServlet", "/login.do" })
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        /**
         * 登录功能
         * 
         * 1.获得用户名及密码。
         * 2.数据有效性验证,验证失败的话,返回登录页面告诉用户。
         * 3.操作数据库,拼接SQL语句,执行SQL语句。
         * 4.查询成功,进入登录成功页面。
         * 5.查询不成功,返回登录页面。
         * 
         */
        //设置响应的类型
        response.setContentType("text/html; charset=UTF-8");
        //设置request编码
        request.setCharacterEncoding("UTF-8");
        //1.获得用户名及密码
        //取得用户名
        String username = request.getParameter("username");
        //取得密码
        String userpass = request.getParameter("userpass");
        
        System.out.println("username:"+username+" userpass:"+userpass);
        
        //2.数据有效性验证,验证失败的话,返回登录页面告诉用户
        //错误集合
        ArrayList<String> list = new ArrayList<String>();
        //验证用户名
        
        if(username==null || "".equals(username)) {
            list.add("1");
        }
        //验证密码
        if(userpass==null || "".equals(userpass)) {
            list.add("2");
        }
        //判断验证是否通过,不通过返回登录页面
        if(list.size()>0) {
            //请求转发
            request.setAttribute("listinfo", list);
            request.getRequestDispatcher("login.jsp").forward(request, response);
            return;
        }
        
        //3.操作数据库,拼接SQL语句,执行SQL语句
        /**
         * 操作数据库步骤
         * 3.1导入操作数据库的jar
         * 3.2数据库驱动
         * 3.3创建连接对象
         * 3.4定义SQL语句
         * 3.5创建执行SQL语句的对象
         * 3.6执行SQL语句
         * 3.7处理执行SQL语句结果
         * 3.8关闭数据库,释放资源
         * 
         */
        //3.1导入操作数据库的jar
        Connection conn = null;
        PreparedStatement psts = null;
        ResultSet rs = null;
        
        try {
            //3.2数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            //3.3创建连接对象
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/expressdata", "root", "root");
            //3.4定义SQL语句
            String sql = "SELECT * FROM userinfo WHERE username=? AND userpass=?";
            //3.5创建执行SQL语句的对象
            psts = conn.prepareStatement(sql);
            psts.setString(1, username);
            psts.setString(2, userpass);
            //3.6执行SQL语句
            rs= psts.executeQuery();
            //3.7处理执行SQL语句结果
            if(rs.next()) {
                //登录成功状态保留下来
                request.getSession().setAttribute("loginuser", username);
                //跳转到登录成功页面
                response.sendRedirect("index.jsp");
            }
            else {
                //登录失败,返回到登录页面
                list.add("3");
                request.setAttribute("listinfo", list);
                request.getRequestDispatcher("login.jsp").forward(request, response);
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            //3.8关闭数据库,释放资源
                try {
                    if(rs!=null)
                        rs.close();
                    if(psts!=null)
                        psts.close();
                    if(conn!=null)
                        conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    }

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

}
 

猜你喜欢

转载自blog.csdn.net/xiaoxiaoli0_0/article/details/84978348