Java Web简单登陆功能的实现

本文详细介绍如何使用IDEA创建数据库访问类,实现通过Java连接MySQL数据库,进行数据查询操作,并展示具体的代码实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      首先创建一个简单的数据库:请看表结构:id就当作用户名,stu_id当作密码吧。数据库名叫db_jxgl,里面只有一张student表,具体请看下图:

     

     使用过IDEA之后好像从农村来到了城市,太TM强大了!!! 先创建数据库访问类:

package com.jmdx.bingo.sql;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

public class MysqlDBConn {
    private PreparedStatement ps;
    private Connection conn;
    ResultSet res;
    public static final String url = "jdbc:mysql://localhost:3306/db_jxgl?useSSL=false";
    public static final String name = "com.mysql.jdbc.Driver";
    public static final String user = "root";
    public static final String password = "123456";
    public MysqlDBConn() {
        ps = null;
        res = null;
        try {
            Class.forName(name);
            conn = DriverManager.getConnection(url,user,password);

        }catch (Exception e) {
            e.printStackTrace();
        }

    }
    public ResultSet Query(String sql,Map<Integer,Integer> map) {
        try {
             ps = conn.prepareStatement(sql);
            for (Map.Entry<Integer,Integer> entry : map.entrySet()) {
                ps.setInt(entry.getKey(),entry.getValue());
            }
            res = ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }

    public String getResult(ResultSet result) {
        String uName = "";
        try {
            while (result.next()) {
                uName = result.getString("name");
//                System.out.println("name:"+userName);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return uName;
    }

    public static void main(String[] args) {
        String sql = "select name from student where id = ? and stu_id = ?";
        MysqlDBConn conn = new MysqlDBConn();
        Map<Integer,Integer> map = new HashMap<>();
        map.put(1,1);
        map.put(2,901);
        ResultSet res = conn.Query(sql,map);
//        System.out.println(res.toString());
         String str = conn.getResult(res);
        System.out.println(str);
    }
}

然后是Servlet:

package com.jmdx.bingo.servlet;

import com.jmdx.bingo.sql.MysqlDBConn;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

public class Login extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        response.setHeader("Content-type","text/html;charset=UTF-8");
        String name = request.getParameter("username");
        String pwd = request.getParameter("password");
        String user = login(name,pwd);
        response.setCharacterEncoding("UTF-8");
        PrintWriter writer = response.getWriter();
        writer.write("<h1>你好, "+user+"</h1>");

    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doPost(request,response);
    }
    public String login (String id,String passwd) {
        Integer uId = Integer.valueOf(id);
        Integer uPasswd = Integer.valueOf(passwd);
        String sql = "select name from student where id = ? and stu_id = ?";
        Map<Integer,Integer> map = new HashMap<>();
        map.put(1,uId);
        map.put(2,uPasswd);
        MysqlDBConn conn = new MysqlDBConn();
        ResultSet res = conn.Query(sql,map);
        String uName = conn.getResult(res);
        return uName;
    }
}

首页:(没有写样式,简单的用空格排版了。)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>login</title>
</head>
<body>
    <h1>个人门户</h1>
    <form method="post" action="/hello">
        用户id:<input type="text" name="username"> <br><br>
        密&nbsp&nbsp&nbsp码:<input type="password" name="password"><br><br>&nbsp&nbsp&nbsp&nbsp
        <input type="submit" value="登陆">&nbsp&nbsp&nbsp&nbsp
        <input type="reset" value="取消">

    </form>
</body>
</html>

效果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值