java二级联动sql_第十八篇——JDBC操作数据库之二级菜单联动

  • Post author:
  • Post category:java


package com.model;

import com.dbutil.Dbconn;

import com.entity.City;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.util.List;

/**

* Created by Ray on 2018/3/18 0018.

**/

public class CityDao {

Dbconn dbconn = new Dbconn();

Connection conn = null;

PreparedStatement ps;

ResultSet rs;

public List listAlias(String alias){

List citylist = new ArrayList();

City city = null;

try{

conn = dbconn.getConnection();

String sql = “select * from city where alias = ?”;

ps = conn.prepareStatement(sql);

ps.setString(1,alias);

rs = ps.executeQuery();

while (rs.next()){

city = new City();

city.setId(rs.getInt(“id”));

city.setName(rs.getString(“name”));

city.setAlias(rs.getString(“alias”));

citylist.add(city);

}

}catch (Exception e){

e.printStackTrace();

}

return citylist;

}

}

五、secondMenu.jsp

String path = request.getContextPath();

String basePath = request.getScheme() + “://” + request.getServerName() + “:” + request.getServerPort() + path + “/”;

%>

二级菜单联动

$(function () {

//给下拉列表添加事件

$(“#city1”).change(function () {

//测试

// alert($(this).val() + ” ” + $(“city1 option:selected”).text());

//post传输,固定四个参数

//第一个参数是url:发送请求地址

//第二个参数data:待发送的值key/value

//第三个参数callback:发送返回时回调函数

//第四个参数type:返回内容格式,常用text,json

$.post(“SecondMenu”, {city1: $(this).val()},

function (result) {

//获取第二级城市的id

var city2 = $(“#city2”);

//清空第二级里面的城市

city2.empty();

for (var i = 0; i < result.length; i++) {

city2.append(“” + result[i].name + “”);

}

}, “json”);

});

});

城市

请选择

广东

湖南

请选择

六、SecondMenu.javapackage com.control;

import com.entity.City;

import com.model.CityDao;

import net.sf.json.JSONArray;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

/**

* Created by Ray on 2018/3/18 0018.

**/

public class SecondMenu extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request,response);

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//获取页面传输的数据

String alias = request.getParameter(“city1”);

System.out.println(“测试数据alias: ” + alias);

//调用模型

CityDao cityDao = new CityDao();

//将页面数据进行匹配,如果匹配成功显示下面创建的城市

List list = cityDao.listAlias(alias);

//将list转换为json

JSONArray jsonArray = JSONArray.fromObject(list);

//测试

System.out.println(jsonArray);

//响应页面

response.setCharacterEncoding(“utf-8”);

response.setContentType(“text/json”);

PrintWriter out = response.getWriter();

//打印json,为了页面获取

out.println(jsonArray);

out.flush();

out.close();

}

}

七、web.xml



版权声明:本文为weixin_28681719原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。