1.完成数据库的书写
#判断存在即删除数据库
drop database if exists mydb;
#创建数据库
create database mydb;
#使用数据库
use mydb;
#创建表
create table t_user
(
uid int primary key auto_increment,
username varchar(20),
password varchar(20),
phone varchar(11),
address varchar(50)
);
insert into t_user(username,password,phone,address) values('张三','666','18965423548','南阳');
insert into t_user(username,password,phone,address) values('李四','333','18754263548','许昌');
insert into t_user(username,password,phone,address) values('小美','123','18565234759','信阳');
select * from t_user where username=? and password=?
select * from t_user;
create table t_goods
(
gid int primary key auto_increment,
gname varchar(20),
price double,
mark varchar(100)
);
insert into t_goods(gname,price,mark) values('泡面',4.5,'够香够辣就是这个味!');
insert into t_goods(gname,price,mark) values('火腿',8.5,'肉质细腻Q弹!');
insert into t_goods(gname,price,mark) values('雪碧',3.5,'清爽冰凉随心爽!');
select * from t_goods;
2.连接数据库
package com.gao.servlet;
import com.gao.bean.User;
import javax.servlet.ServletException;
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.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class login extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse respponse) throws ServletException, IOException {
doPost(request, respponse);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse respponse) throws ServletException, IOException {
//1.设置请求和响应的编码格式。以及响应的格式
request.setCharacterEncoding("utf-8");//设置请求的编码格式为中文
respponse.setCharacterEncoding("utf-8");//设置响应的编码格式
respponse.setContentType("text/html;charset=UTF-8");//以什么样的格式(文本/网页)响应
//2.获取请求的参数
String usename = request.getParameter("usename");
String passsword = request.getParameter("passsword");
System.out.println(usename);
System.out.println(passsword);
//执行业务处理
Connection connection=null;//数据库连接对象
PreparedStatement pstm=null;//预处理对象
ResultSet rs=null;//结果集对象
User login=null;
try {
//1.加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取数据库连接
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb?useSSL=false&serverTimeZone=UTC","root","root");
//3.定义登录的sql语句
String sql="select * from t_user where username=? and password=?";
//4.获取预处理对象
pstm = connection.prepareStatement(sql);
//5.传参
pstm.setObject(1,usename);
pstm.setObject(2,passsword);
//6.执行查询
rs=pstm.executeQuery();
//7.解析结果集
if (rs.next()){
login=new User();
//从结果集中获取数据,封装到实体类对象中
int uid=rs.getInt("uid");
login.setUid(uid);
login.setUsername(rs.getString("username"));
login.setPassword(rs.getString("pasasword"));
login.setPhone(rs.getString("phone"));
login.setAddress(rs.getString("address"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
//8.关闭资源
try{
if(rs!=null){
rs.close();
}
if(pstm!=null){
pstm.close();
}
if (connection!=null){
connection.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
//判断登陆的用户信息是否为空
if (login!=null){
//登陆成功,跳转到主页
respponse.sendRedirect("zhuye.jsp");//指定跳转的页面
}else{
//登陆失败,跳转到错误页
respponse.sendRedirect("error.jsp");
}
}
}
3.完成映射关系
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置servlet类-->
<servlet>
<!--起别名-->
<servlet-name>login</servlet-name>
<!--servlet类所在的位置:类的全类名就是包名.类名-->
<servlet-class>com.gao.servlet.login</servlet-class>
</servlet>
<!--servletr类的映射:servlet用来处理哪个请求-->
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
4.页面跳转
4.1.成功
<%--
Created by IntelliJ IDEA.
User: gt
Date: 2023/2/17
Time: 20:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>正确页</title>
</head>
<body>
<h2>还瞅啥,填对了</h2>
</body>
</html>
4.2.失败
<%--
Created by IntelliJ IDEA.
User: gt
Date: 2023/2/17
Time: 20:28
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>错误页面</title>
</head>
<body>
<h2>看什么看,出错了</h2>
<a href="login.jsp">去主页</a>
</body>
</html>