当前位置:网站首页>Servlet连接数据库实现用户登录功能
Servlet连接数据库实现用户登录功能
2022-06-12 15:12:00 【xiaoweiwei99】
做一个小案例顺便复习一下jdbc的知识
一、需求:
用户在浏览器输入用户名和密码,如果数据库中有数据,提醒用户登录成功,如果没有数据,提醒用户重新登录
二、先复习一下JDBC,今天用的时候有些细节忘了,知识还是需要多次复习啊,并不是说学过就是自己的。
1.概述:
用java程序操作数据库的一个技术,是java程序连接数据库的一套标准,本质上就是一堆API。
2.开发步骤:
2.1.导jar包:
对于java项目,直接将jar包复制到项目然后解析jar(add as library)就可以用了;对于web项目,需要将jar包放到tomcat的lib目c录下,在web项目中,当Class.forName(“com.mysql.jdbc.Driver”);时idea是不会去查找字符串,不会去查找驱动的。所以只需要把mysql-connector-java-5.1.7-bin.jar拷贝到tomcat下lib目录就可以了。今天这里出了问题,找到这种解决方案。
2.2.步骤:具体步骤都在代码里
注册驱动—>
Class.forName(“com.mysql.jdbc.Driver”);
获取连接—>
String url = “协议://IP地址:端口号/数据库的名字/”;
String url = “jdbc:mysql://localhost:3306/person”;
Connection c = DriverManager.getConnection(url, “root”, “root”);
写sql语句—>
String sql = "select * from user where name = and pwd = ";
获取传输器—>
PreparedStatement preparedStatement = connection.prepareStatement(sql);
设置值—>
preparedStatement.setObject(1,username);
preparedStatement.setObject(2,pwd);
获取结果集—>
ResultSet resultSet = preparedStatement.executeQuery();
解析结果集—>
resultSet.next()
三、代码实现:
1.登录界面代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登录界面</title>
</head>
<body>
<h3 style="text-align: center">用户登录</h3>
<form action="userLogin" method="post" style="text-align: center" >
用户名:<input type="text" name="username"><br/>
<br/>
密 码:<input type="password" name="pwd"><br/>
<br>
<input type="submit" value="提交">
</form>
</body>
</html>
2.登录成功界面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>成功登录界面</title>
</head>
<body>
<h1> 恭喜登录成功!!!</h1>
</body>
</html>
3.servlet代码:
package cn.tedu;
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.*;
//配置访问路径
@WebServlet(urlPatterns = "/userLogin")
public class ServletLogin extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.解决中文乱码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset = utf-8");
//2.获取用户输入的名字和密码
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//3.连接数据库
try {
//3.1注册驱动
Class.forName("com.mysql.jdbc.Driver");
//3.2获取连接
String url = "jdbc:mysql://localhost:3306/person";
connection = DriverManager.getConnection(url,"root","root");
//3.3写sql
String sql = "select * from user where name = ? and pwd = ?";
//3.4获取传输器
preparedStatement = connection.prepareStatement(sql);
//3.5设置值
preparedStatement.setObject(1,username);
preparedStatement.setObject(2,pwd);
//3.6返回结果集
resultSet = preparedStatement.executeQuery();
if (resultSet.next()){
//重定位,如果结果返回true,"跳转"到success.html
response.sendRedirect("success.html");
}else{
String urls = "login.html";
response.getWriter().write("用户不存在"+""+"<a href = '"+urls+"'>点击重新登录</a>");
}
} catch (Exception e) {
e.printStackTrace();
//3.6关闭资源
}finally {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
4.数据库数据

5.登录成功页面:

6.登录失败界面:

7.登录界面:

边栏推荐
猜你喜欢

C main函数

New关键字、引用&与指针的学习记录

Writing method of JUnit multithreading
![[LDA] basic knowledge notes - mainly AE and VAE](/img/1c/ccc073cac79b139becd5de0b9a91b1.png)
[LDA] basic knowledge notes - mainly AE and VAE

粒子滤波学习记录

TF learning notes in ROS

Pointer related concepts

Qiming cloud sharing | demonstrate the switch through an example of the matter protocol to control the light on and off through the matter protocol

Jetpack architecture component learning (3) -- activity results API usage

三维重建系统 | L3双视角运动恢复结构(SFM双目SFM)
随机推荐
Error 1105: message:\“raft entry is too large
Prompt that the program cannot access key files / directories when installing PS software. Error code: 41
机器人前行、旋转的service编写
Process abstraction of program construction and interpretation
ROS beginners write the server that the little turtle rotates a certain angle at a certain speed
数组相关内容
产业端:618的新战场
h3c GR5200路由器上如何设置公网ip可以访问
[spark][core] interview questions talk about push based shuffle
结构体示例
如何写年终总结
#include使用“文件名“和<文件名>引入头文件的区别及简述
阿裏、騰訊、拼多多垂範,產業互聯網的新邏輯漸顯
野指针理解
【LDA】LDA主题模型笔记—主要是狄利克雷
C main function
Solve log4j2 vulnerability and be attacked by mining and zombie process viruses
Yiwei lithium energy plans to raise 9billion yuan: liujincheng and luojinhong jointly subscribe for 6billion yuan of layout Optical Valley
[LDA] rough version notes of EM variational reasoning [to be improved
Kinect2.0+ORBSLAM2_with_pointcloud_map