当前位置:网站首页>Servlet connects to database to realize user login function
Servlet connects to database to realize user login function
2022-06-12 15:14:00 【xiaoweiwei99】
Make a small case and review it by the way jdbc Knowledge
One 、 demand :
The user enters the user name and password in the browser , If there is data in the database , Remind the user that the login is successful , If there is no data , Remind the user to log in again
Two 、 Just to review JDBC, I forgot some details when I used it today , Knowledge still needs to be reviewed many times , It doesn't mean that learning is your own .
1. summary :
use java A technique by which a program operates a database , yes java A set of standards for connecting programs to databases , It's essentially a bunch of API.
2. Development steps :
2.1. guide jar package :
about java project , Direct will jar The package is copied to the project and then parsed jar(add as library) You can use it ; about web project , Need to put jar Put the bag in tomcat Of lib Objective c Record , stay web In the project , When Class.forName(“com.mysql.jdbc.Driver”); when idea It doesn't look for strings , I won't look for drivers . So just put mysql-connector-java-5.1.7-bin.jar copy to tomcat Next lib The catalogue will do . There is a problem here today , Find this solution .
2.2. step : The specific steps are in the code
Registration drive —>
Class.forName(“com.mysql.jdbc.Driver”);
Get the connection —>
String url = “ agreement ://IP Address : Port number / Database name /”;
String url = “jdbc:mysql://localhost:3306/person”;
Connection c = DriverManager.getConnection(url, “root”, “root”);
Write sql sentence —>
String sql = "select * from user where name = and pwd = ";
Get the transmitter —>
PreparedStatement preparedStatement = connection.prepareStatement(sql);
Set the value —>
preparedStatement.setObject(1,username);
preparedStatement.setObject(2,pwd);
Get the result set —>
ResultSet resultSet = preparedStatement.executeQuery();
Parse the result set —>
resultSet.next()
3、 ... and 、 Code implementation :
1. Login interface code :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> User login interface </title>
</head>
<body>
<h3 style="text-align: center"> The user login </h3>
<form action="userLogin" method="post" style="text-align: center" >
user name :<input type="text" name="username"><br/>
<br/>
The secret code :<input type="password" name="pwd"><br/>
<br>
<input type="submit" value=" Submit ">
</form>
</body>
</html>
2. Login success screen :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Successful login interface </title>
</head>
<body>
<h1> Congratulations on login success !!!</h1>
</body>
</html>
3.servlet Code :
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.*;
// Configure access path
@WebServlet(urlPatterns = "/userLogin")
public class ServletLogin extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. Solve the Chinese garbled code
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset = utf-8");
//2. Get the name and password entered by the user
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//3. Connect to database
try {
//3.1 Registration drive
Class.forName("com.mysql.jdbc.Driver");
//3.2 Get the connection
String url = "jdbc:mysql://localhost:3306/person";
connection = DriverManager.getConnection(url,"root","root");
//3.3 Write sql
String sql = "select * from user where name = ? and pwd = ?";
//3.4 Get the transmitter
preparedStatement = connection.prepareStatement(sql);
//3.5 Set the value
preparedStatement.setObject(1,username);
preparedStatement.setObject(2,pwd);
//3.6 Return result set
resultSet = preparedStatement.executeQuery();
if (resultSet.next()){
// relocation , If the result returns true," Jump " To success.html
response.sendRedirect("success.html");
}else{
String urls = "login.html";
response.getWriter().write(" The user doesn't exist "+""+"<a href = '"+urls+"'> Click login again </a>");
}
} catch (Exception e) {
e.printStackTrace();
//3.6 close resource
}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. Database data

5. Login success page :

6. Login failure interface :

7. Login screen :

边栏推荐
- Element positioning of selenium
- How to set public IP access on the H3C gr5200 router
- Pointer related concepts
- Phpstudy indicates that the hosts file may not exist or be blocked from being opened. How to resolve the failure of synchronizing hosts
- [spark][core] what is an external shuffle service?
- New features of ES6
- 宝塔面板新建数据库提示数据库名不能大于16位的解决方法
- PHPstudy建站提示hosts文件可能不存在或被阻止打开,同步hosts失败怎么解决
- Industrial end: a new battlefield of 618
- USART (rs232422485), I2C, SPI, can, USB bus
猜你喜欢

Change according to the situation, the road to promotion in the second half of 2022

3D reconstruction system | L3 dual view motion recovery structure (SFM binocular SFM)

SQL cross database injection

C 操作符

Deepin20.6 RTX3080 安装显卡驱动510.60.02、CUDA11.6、PyTorch1.11

Swap numbers, XOR, operator correlation

Installation and use of mat

TF learning notes in ROS

Structure example

TC menu split
随机推荐
频繁项集产生强关联规则的过程
Tensorrt based in-depth learning model deployment practice tutorial!
Deepin20.6 rtx3080 installing graphics card drivers 510.60.02, cuda11.6, pytorch1.11
First set and follow set in vernacular
产业端:618的新战场
Apprendre est une chose contre la nature humaine
Pta: self TEST-1 print Hourglass (20 points)
IMU learning records
左对齐,右对齐,随机数,goto,比较输出bool
Solution to the new database prompt on the pagoda panel that the database name cannot be greater than 16 bits
Jenkins' RPC test project
关于互联网大厂裁员
Ali suggests that all POJO attributes use wrapper classes, but have you noticed these pits?
ROS 中 boost::bind( ) 的使用
ES6新特性
Differences between microservice architecture and SOA Architecture
[SPARK][CORE] 面试问题之什么是 external shuffle service?
3D reconstruction system | L3 incremental motion recovery structure (incremental SFM)
Loadbalancer load balancer
USART(RS232422485)、I2C、SPI、CAN、USB总线