mvc中tr中多个td合并_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 als;    public static ArrayListhadle(String stuno) {//    每执行一次查询就调用一次该方法,每调用一次该方法就重新创建一次对象,目的是清空列表中的值,只存储最新查找的值    als = new ArrayList();//    每调用一次该方法就赋初始值,如果不是每调用一次方法就赋初始值,那么当用户先查询存在的数据,再输入为空时也可以查到数据,因为此时//    的变量已被赋值    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"%><html><head><meta charset="UTF-8"><title>Insert title heretitle>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>

数据库表结构

c7e2c88df6d8a363ca3c475fb36ce87a.png

项目结构

f76d9fb60fa75cef894b2d7702a5f310.png

效果演示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值