package com.examples.ajax.servlet;
import com.alibaba.fastjson.JSON;
import com.examples.ajax.beans.KeyWords;
import com.examples.ajax.utils.DBUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@WebServlet(“/ajaxAutoComplete”)
public class AjaxRequest13 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取前端传来的关键字
String keyWords=request.getParameter(“keyWords”);
//连接数据库,进行模糊查询
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
//封装关键字对象
List<KeyWords> keyWordsList=new ArrayList<>();
try {
conn=DBUtils.getConnection();
String sql=”select content from tb_search where content like ?”;
ps=conn.prepareStatement(sql);
ps.setString(1, keyWords + “%”);
rs=ps.executeQuery();
while(rs.next()){
String content=rs.getString(“content”);
//封装成关键字对象
KeyWords keyWordsObj=new KeyWords(content);
//将关键字对象封装
keyWordsList.add(keyWordsObj);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
DBUtils.close(conn, ps, rs);
}
//后端数据json化
String jsonKeyWordsArray=JSON.toJSONString(keyWordsList);
//返回后端数据
response.getWriter().write(jsonKeyWordsArray);
}
}
import com.alibaba.fastjson.JSON;
import com.examples.ajax.beans.KeyWords;
import com.examples.ajax.utils.DBUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@WebServlet(“/ajaxAutoComplete”)
public class AjaxRequest13 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取前端传来的关键字
String keyWords=request.getParameter(“keyWords”);
//连接数据库,进行模糊查询
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
//封装关键字对象
List<KeyWords> keyWordsList=new ArrayList<>();
try {
conn=DBUtils.getConnection();
String sql=”select content from tb_search where content like ?”;
ps=conn.prepareStatement(sql);
ps.setString(1, keyWords + “%”);
rs=ps.executeQuery();
while(rs.next()){
String content=rs.getString(“content”);
//封装成关键字对象
KeyWords keyWordsObj=new KeyWords(content);
//将关键字对象封装
keyWordsList.add(keyWordsObj);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
DBUtils.close(conn, ps, rs);
}
//后端数据json化
String jsonKeyWordsArray=JSON.toJSONString(keyWordsList);
//返回后端数据
response.getWriter().write(jsonKeyWordsArray);
}
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。