数据库CRUD操作------连载中

mac2024-02-20  30

数据库CRUD操作-----登录

大家继续关注,往后会把所有的功能都一一实现分享

请大家仔细看我的代码,里面有很多的小细节需要大家观察 这里面有一个关键点就是哦按段用户名和密码的时候 是在jsp页面用Jquery写的,多思考

<%-- Created by IntelliJ IDEA. User: Lenovo Date: 2019/10/30 Time: 21:55 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录页面</title> <script src = "js/jquery-3.4.1.js"></script> </head> <body> <center> <form action="${pageContext.request.contextPath}/LoginServlet" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" name="name" id="name"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="pwd" id="pwd"></td> </tr> <tr> <td colspan="1"></td> <td><input type="submit" value="登录"></td> </tr> </table> </form> <script> $(function () { $("form").submit(function () { var name = $("#name").val(); var pwd = $("#pwd").val(); var nameFlag = false; var pwdFlag = false; if(name == "" || name == "undefined"){ alert("用户名不能为空!!"); nameFlag = false; }else{ nameFlag = true; } if(pwd == "" || pwd == "undefined"){ alert("密码不能为空!!"); pwdFlag = false; }else{ pwdFlag = true; } if(nameFlag && pwdFlag){ return true; }else{ return false; } }); }); </script> </center> </body> </html>

下面是servlet代码

package com.hnpi.servlet; import com.hnpi.util.DBUtil; 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 java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @WebServlet(value = "/LoginServlet") public class LoginServlet extends HttpServlet { 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 pwd = request.getParameter("pwd"); Connection conn = DBUtil.getConn(); PreparedStatement ps = null; ResultSet rs = null; String sql = "select * from zhuce where name = ? and pwd = ?"; try { ps = conn.prepareStatement(sql); ps.setString(1, name); ps.setString(2, pwd); rs = ps.executeQuery(); if(rs.next()){ response.sendRedirect("/index.jsp"); }else{ response.sendRedirect("/login.jsp"); } }catch (Exception e){ e.printStackTrace(); }finally { DBUtil.close(conn,ps,rs); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }

这是DBUtil代码:

package com.hnpi.util; import java.sql.*; public class DBUtil { public static Connection getConn() { String url = "jdbc:sqlserver://localhost:1433;databaseName=MyDB"; String user = "sa"; String pwd = "1"; Connection conn = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); conn = DriverManager.getConnection(url, user, pwd); }catch (Exception e){ e.printStackTrace(); } return conn; } public static void close(Connection conn, PreparedStatement ps, ResultSet rs){ if(conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if(ps != null){ try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if(rs != null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
最新回复(0)