我在这个阶段将后台的代码进行了一下晚上因为需要一个模糊搜索,所以需要连接数据库进行前后端的交互
Servlet.java
package servlet;
import java.io.IOException;
import java.util.List;
import java.io.PrintWriter;
import javax.security.auth.message.callback.PrivateKeyCallback.Request;
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 dao.*;
import bean.*;
@WebServlet(“/r2”)
public class Servlet extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
return;
}
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding(“utf-8”);// 设置字符集,避免乱码
// 获取jsp界面需要进行的操作,
String method = req.getParameter(“method”);
//System.out.println(“============”);
if (method.equals(“zhuce”))
{
adduser(req,resp);
}
if (method.equals(“denglu”))
{
denglu(req,resp);
}
if (method.equals(“sousuo”))
{
sousuo(req,resp);
}
if (method.equals(“dianzan”))
{
dianzan(req,resp);
}
}
private void adduser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding(“utf-8”);
resp.setContentType(“text/html;charset=utf-8”);
String username=req.getParameter(“username”);
String password = req.getParameter(“password”);
String number=req.getParameter(“number”);
String email=req.getParameter(“email”);
String sex=req.getParameter(“sex”);
String age=req.getParameter(“age”);
String weight=req.getParameter(“weight”);
System.out.println(username);
dao userdao = new dao();
System.out.println(“============”);
userdao.adduser(number,password,number,email,sex,age,weight);
System.out.println(“============”);
req.getRequestDispatcher(“/login.jsp”).forward(req,resp);
}
private void denglu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding(“utf-8”);
resp.setContentType(“text/html;charset=utf-8”);
String username=req.getParameter(“username”);
String password = req.getParameter(“password”);
System.out.println(username);
dao userdao = new dao();
String w= userdao.login(username,password);
if(w.equals(“正确”))
{
req.getRequestDispatcher(“daohang.jsp”).forward(req,resp);
}
else {
req.setAttribute(“message”, “账号或密码错误,请重新输入。”);
req.getRequestDispatcher(“login.jsp”).forward(req,resp);
}
}
private void sousuo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding(“utf-8”);
String name=req.getParameter(“name”);
// System.out.println(req.getParameter(“luxian”)+”=========”);
List<Food2> kc=dao.sousuo(name);
req.setAttribute(“kc”, kc);
//req.setAttribute(“inf1”, inf1);
req.getRequestDispatcher(“sousuo.jsp”).forward(req, resp);
}
private void dianzan(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding(“utf-8”);
resp.setContentType(“text/html;charset=utf-8”);
String name=req.getParameter(“name”);
dao userdao = new dao();
// System.out.println(“============”);
userdao.dianzan(name);
// System.out.println(“============”);
req.setAttribute(“message”, “点赞成功”);
req.getRequestDispatcher(“/daohang.jsp”).forward(req,resp);
}
}
Dao.java
package dao;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.sql.ResultSet;
import java.sql.Statement;
import dbutil.DBUtil;
import bean.*;
import java.util.Collections;
public class dao {
public int adduser(String username,String password,String number,String email,String sex,String age,String weight)
{
Connection conn = DBUtil.getConn();//这里就是从DBUtil类里面得到连接
Statement state =null;
ResultSet rs = null;
int flag=0;
try
{
String sql = “insert into user(username,password,number,email,sex,age,weight)values(‘”+username+”‘,'”+password+”‘,'”+number+”‘,'”+email+”‘,'”+sex+”‘,'”+age+”‘,'”+weight+”‘)”;
state = conn.createStatement();
int count = state.executeUpdate(sql);
if(count>0) {
System.out.println(“数据插入成功”);
}else {
System.out.println(“数据插入失败了QAQ”);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(rs, state, conn);
}
return flag;
}
public String login(String name,String paw)
{
Connection conn = DBUtil.getConn();//这里就是从DBUtil类里面得到连接
Statement state =null;
ResultSet rs = null;
try
{
String sql = “select * from user”;
state = conn.createStatement();
rs=state.executeQuery(sql);
while(rs.next())
{
String number=rs.getString(“username”);
String password=rs.getString(“password”);
if(number.equals(name)&&password.equals(paw))
{
return “正确”;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(rs, state, conn);
}
return “错误”;
}
public static List<Food2> sousuo(String name)
{
String sql = “select * from food join windows on food.position=windows.WPosition where foodname like ‘%”+name+”%’ or WName like ‘%”+name+”%'”;
Connection conn = DBUtil.getConn();//这里就是从DBUtil类里面得到连接
Statement state =null;
List<Food2> list = new ArrayList<>();
ResultSet rs = null;
int flag=0;
try
{
state = conn.createStatement();
rs=state.executeQuery(sql);
Food2 kc=null;
while(rs.next())
{
int WId=rs.getInt(“WId”);
String WPosition=rs.getString(“WPosition”);
String WName=rs.getString(“WName”);
int id=rs.getInt(“id”);
String foodname=rs.getString(“foodname”);
double price=rs.getDouble(“price”);
String kind=rs.getString(“kind”);
//String img=rs.getString(“img”);
int score=rs.getInt(“score”);
kc =new Food2(WId,WPosition,price,WName,id,kind,foodname,/*img,*/score);
System.out.println(kc);
list.add(kc);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(rs, state, conn);
}
return list;
}
public int dianzan(String name)
{
Connection conn = DBUtil.getConn();//这里就是从DBUtil类里面得到连接
Statement state =null;
ResultSet rs = null;
int flag=0;
//System.out.println(name);
try
{
String sql = “update windows set score=score+1 where WName='”+name+”‘”;
state = conn.createStatement();
int count = state.executeUpdate(sql);
if(count>0) {
System.out.println(“数据插入成功”);
}else {
System.out.println(“数据插入失败了QAQ”);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(rs, state, conn);
}
return flag;
}
}
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/267736.html