Ajax小案例---在数据库中查询数据

Posted IT界新晋小白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ajax小案例---在数据库中查询数据相关的知识,希望对你有一定的参考价值。

ConnectSqlDao.java部分

package connectsqldao;
import java.sql.*;public class ConnectSqlDao { static private Connection conn = null; static private String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; static private String url = "jdbc:sqlserver://localhost:1433;DatabaseName=Xk"; static private String name = "sa"; static private String passwd = "sa"; public static Connection connectsql() { try { Class.forName(driver); } catch (ClassNotFoundException e1) { e1.printStackTrace(); } try { conn = DriverManager.getConnection(url,name,passwd); } catch (SQLException e) { e.printStackTrace(); } return conn; }}



GetInfoServlet.java部分

package getinfoservlet;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;
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 handleinfodao.InfoDao;
@WebServlet("/GetInfoServlet")public class GetInfoServlet extends HttpServlet { private static final long serialVersionUID = 1L;
public GetInfoServlet() { super(); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); String stuco = request.getParameter("stuco"); ArrayList<String> infoli = InfoDao.hadle(stuco); PrintWriter out = response.getWriter(); out.println(infoli.get(0)); out.println(infoli.get(1)); out.println(infoli.get(2)); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }
}



InfoDao.java部分

package handleinfodao;
import java.sql.*;import java.util.ArrayList;
import connectsqldao.*;public class InfoDao { static private Connection conn = null; static private PreparedStatement pst = null; static private ResultSet rst = null; static private String sql = ""; static private String name; static private String classno; static private String passwd; static private ArrayList<String> als; public static ArrayList<String> hadle(String stuno) {// 每执行一次查询就调用一次该方法,每调用一次该方法就重新创建一次对象,目的是清空列表中的值,只存储最新查找的值 als = new ArrayList<String>();// 每调用一次该方法就赋初始值,如果不是每调用一次方法就赋初始值,那么当用户先查询存在的数据,再输入为空时也可以查到数据,因为此时// 的变量已被赋值 name = "默认为空"; classno = "默认为空"; passwd = "默认为空";// 用户输入数据情况:空值(不输入),超出查询范围// 判断用户输入是否为空 if (stuno == null | stuno.length() == 0) { als.add(name); als.add(classno); als.add(passwd); }else { sql = "select * from Student where StuNo = "+stuno+""; conn = ConnectSqlDao.connectsql(); try { pst = conn.prepareStatement(sql); rst = pst.executeQuery(); if (rst.next()) { //判断用户查询数据是否超出范围 超出范围时查询结果为空 name = rst.getString("StuName"); classno = rst.getString("ClassNo"); passwd = rst.getString("Pwd"); als.add(name); als.add(classno); als.add(passwd); }else { //判断用户查询数据是否超出范围 als.add("超出范围"); als.add("超出范围"); als.add("超出范围"); } } catch (SQLException e) { e.printStackTrace(); }finally { if (rst != null) { try { rst.close(); } catch (SQLException e) { e.printStackTrace(); } } if (pst != null) { try { pst.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } return als; }}



index.jsp部分

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><body><p>学号:<input id="stuno" type="text"/><input type="submit" value="查询" onclick="ajaxdo()"><br/></p><table style="width:300px;height:200px;border:solid" border="1"> <tr><td width="40%">姓名</td><td id="td1"></td></tr> <tr><td width="40%">班级</td><td id="td2"></td></tr> <tr><td width="40%">密码</td><td id="td3"></td></tr></table>
<script type="text/javascript">function ajaxdo(){ //创建异步对象 var xhr = new XMLHttpRequest(); //绑定函数 当获取到查询结果后所要做的事 xhr.onreadystatechange = function(){ if (xhr.readyState == 4 && xhr.status == 200){ //responseText方法会获取向页面输出的数据(print里的数据) 数据不会再输出到页面 //将查询到的数据通过 split 方法转变为列表 var infoli = xhr.responseText.split("\n") //通过不同下标将查询结果输出到不同表格 document.getElementById("td1").innerText = infoli[0] document.getElementById("td2").innerText = infoli[1] document.getElementById("td3").innerText = infoli[2] } } //获取用户输入的学号 var stuno = document.getElementById("stuno").value; //将学号添加到请求地址中 xhr.open("get","GetInfoServlet?stuco=" + stuno,true) //发送请求 xhr.send();}</script></body></html>



数据库表结构




项目结构




效果演示



以上是关于Ajax小案例---在数据库中查询数据的主要内容,如果未能解决你的问题,请参考以下文章

Ajax小案例---检测玩家昵称是否重复(失去焦点)

Ajax案例:三级联动查询员工的信息(三张表进行内连接)

在案例查询中显式转换枚举

php和ajax分页小案例

mybatis-plus小课堂:多表查询案例篇(apply 拼接 in SQL,来查询从表某个范围内的数据)

JavaWeb——AJAX(附三级联动省市区案例)