#判断存在即删除数据库
drop database if exists booksdb;
#创建数据库
create database booksdb;
#使用数据库
use booksdb;
#创建表
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;
package com.yang.servlet;
import com.yang.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 response) throws ServletException, IOException {
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.设置请求和响应的编码格式,以及相应的格式
request.setCharacterEncoding("utf-8");//设置请求的编码格式
response.setCharacterEncoding("utf-8");//设置响应的编码格式
response.setContentType("text/html;charset=utf-8");//以什么样的格式(文本/网页)响应
//2.获取请求的参数
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println(username);
System.out.println(password);
//3.执行业务处理
Connection conn=null;
PreparedStatement pstm=null;
ResultSet rs=null;
User login=null;
try{
//1.加载驱动包
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获得数据可连接
conn= DriverManager.getConnection("url:jdbc:mysql://127.0.0.1:3386/mydb?useSSL=false&serverTimezone=UTC ","root","123456");
//3.定义SQL语句
String sql="select * from t_user where username=? and password=?";
//4.获取预处理对象
pstm=conn.prepareStatement(sql);
//5.传参
pstm.setObject(1,username);
pstm.setObject(2,password);
//6.执行SQL
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 {
try{
if (rs!=null){
rs.close();
}
if (pstm!=null){
pstm.close();
}
if (conn!=null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
//判断业务处理结果给前端作出响应
if (login!=null){
response.sendRedirect("zhuye.jsp");
}else{
response.sendRedirect("error.jsp");
}
}
}