Java教程

javaweb数据库账号密码判断小案例

本文主要是介绍javaweb数据库账号密码判断小案例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

前端登录首页

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
<form action="/ABS/Servlet" method="post">
  <fieldset style="width: 250px;">
    <table>
      <tr>
        <td>用户名</td>
        <td>
          <input type="text" name="userName" id="b_c">
        </td>
      </tr>
      <tr>
        <td>密码</td>
        <td>
          <input type="text" name="passWord" id="b_b">
        </td>
      </tr>
    </table>
    <button type="submit">登录</button>
  </fieldset>
</form>
</body>
</html>

前端登录成功界面

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/11/30 0030
  Time: 15:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
        <p>登陆成功</p>
</body>
</html>

Java数据库链接类

package com.jdbc;

import java.sql.*;

public class JDBCUtils {

    private static final String URL="jdbc:mysql://127.0.0.1:3306/test";
    private static final String user="root";
    private static final String password="12345678";

    static {
        try {
            //1.加载驱动程序
            Class.forName("com.mysql.cj.jdbc.Driver");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //获取Connection对象
    public static Connection getConnection() throws Exception{
        //2.获得数据库链接
        return DriverManager.getConnection(URL, user, password);
    }

    //关闭资源
    public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        //关闭PreparedStatement对象
        if(preparedStatement!=null){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        //关闭Connection对象
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}

判断账号密码是否正确方法,也是数据库查询方法

package comservices;

import com.jdbc.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class LoginServier {
    /*
            检验账号密码登录是否正确
     */
    public boolean checkLogin(String username,String password){
        Connection connection=null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet=null;
        try {
            connection =JDBCUtils.getConnection();
            preparedStatement=connection.prepareStatement("SELECT COUNT(*) count FROM user_word WHERE user=? AND password=?");
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,password);
            resultSet = preparedStatement.executeQuery();
            resultSet.next();// 读取查询结果点下一行
          int count =  resultSet.getInt("count");
          return count==1?true:false;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(connection,preparedStatement,resultSet);
        }
        return false;
    }
}

通过Servlet进行调用

package com.servlet;

import com.jdbc.JDBCUtils;
import comservices.LoginServier;

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;

@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //验证登录
       String username = request.getParameter("userName");
       String password =request.getParameter("passWord");
        LoginServier loginServier = new LoginServier();
        boolean result = loginServier.checkLogin(username,password);
        if (result){
            // 重定向
            response.sendRedirect("Scoo.jsp");
        }else {
            response.sendRedirect("index.jsp");
        }


    }
}

这篇关于javaweb数据库账号密码判断小案例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!