1.编写mysql数据库
#判断存在即删除数据库
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.zeng.servlet;
public class User {
private Integer uid;
private String username;
private String password;
private String phone;
private String address;
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"uid=" + uid +
", username='" + username + '\'' +
", password='" + password + '\'' +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
'}';
}
}
3.使用JDBC去响应执行
package com.zeng.servlet;
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.*;
public class Login extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Login...doGet");
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// //req用来处理请求 resp用来处理响应
//1.设置请求和响应的编码格式,以及响应的格式
req.setCharacterEncoding("utf-8"); //设置请求的编码格式为中文
resp.setCharacterEncoding("utf-8"); //设置响应的编码格式
resp.setContentType("text/html;charset=UTF-8"); //以什么样的格式(文本/网页)响应
//2.获取请求的参数
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println(username);
System.out.println(password);
//3.执行业务处理
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","root","root");
//3.定义登录的sql语句
String sql="select * from t_user where username=? and password=?";
//4.获取预处理对象
pstm = connection.prepareStatement(sql);
//5.传参
pstm.setObject(1,username);
pstm.setObject(2,password);
//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("password"));
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){
//登录成功,跳转到主页
resp.sendRedirect("zhuye.jsp"); //指定跳转的页面
}else{
//登录失败,跳转到错误页面
resp.sendRedirect("error.jsp");
}
}
}
4.运行成功跳转页面