logo资料库

基于JSP+AJAX的类百度搜索动模糊查询.docx

第1页 / 共8页
第2页 / 共8页
第3页 / 共8页
第4页 / 共8页
第5页 / 共8页
第6页 / 共8页
第7页 / 共8页
第8页 / 共8页
资料共8页,全文预览结束
.jsp 页面中: <%@ page contentType="text/html; charset=UTF-8"%> 自动提示框
id="myinput"/>
Servlet中: package dd; import java.io.IOException; import java.io.PrintWriter; import java.net.URLDecoder; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class QueryServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest request, HttpServletResponse response) { throws ServletException, IOException request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UT F-8");
PrintWriter out = response.getWriter(); String msg = request.getParameter("msg"); String decoded_msg = URLDecoder.decode(msg, System.out.println(decoded_msg); getContent(decoded_msg, out); "UTF-8"); } public static Connection getConnection() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/[DataBase]", "root", "[password]"); return conn; } catch (ClassNotFoundException cnfe) { cnfe.printStackTrace(); return null; } catch (SQLException e) { e.printStackTrace(); return null; } } public static void getContent(String msg, PrintWriter out) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null;
if (!msg.equals("")) { System.out.println(msg); String sql = "select * from [table] where msg like '" + msg + "%'"; System.out.println(sql); try { conn = getConnection(); pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { out.print(rs.getString("msg") + "
"); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } if (pstmt != null) { pstmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } }
{ msg =?"; // 删除内容 public static boolean deleteContent(String msg) Connection conn = null; PreparedStatement pstmt = null; System.out.println(msg); String sql = "delete * from hotwords where try { conn = getConnection(); pstmt = conn.prepareStatement(sql); pstmt.setString(1, msg); int n = pstmt.executeUpdate(); if (n > 0) { return true; } else { return false; } } catch (SQLException e) { e.printStackTrace(); return false; } finally { try { if (pstmt != null) { pstmt.close(); } if (conn != null) { conn.close(); }
} catch (SQLException e) { e.printStackTrace(); } } } } 然后自己配XML文件,建数据库(MySql),里面带[]的字段是自己 要填的。
分享到:
收藏