Web多级联动下拉框(从数据库中获取数据,MVC模式,使用的是tomcat)

一、文件位置
java代码的位置
Web多级联动下拉框(从数据库中获取数据,MVC模式,使用的是tomcat)_第1张图片
二、先编写一个jsp页面用来跳转到多级下拉框的jsp页面
随便编一个就行了,这里就叫index.jsp
1.index.jsp

<%@ page language="java"  pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8" import="edu.model.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>




首页



				跳转到下拉框页面


2.Xlk.jsp

<%@ page language="java"  pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8" import="edu.model.*,java.util.*,java.sql.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>







 


四级联动获取数据库数据

 
       

三、Controller.java代码
1.DdbControl.java

package edu.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import edu.model.DBOper;
import edu.model.Ddb;
import edu.model.User;

@Controller
public class DdbControl {    
	//二级联动获取数据库数据		
	//一级下拉框
	@RequestMapping("/view/ddbone")
	public String ddbone(HttpServletRequest request,HttpServletResponse response) throws IOException, ClassNotFoundException, SQLException {
	//调用javabean处理			
		Ddb d=new Ddb();
		ArrayList list=d.two();
		request.setAttribute("list", list);
		return "Xlk.jsp";
	}
	//二级下拉框
	@RequestMapping("/view/ddbtwo")
	public String ddbtwo(HttpServletRequest request,HttpServletResponse response) throws Exception{
		response.setContentType("text/json; charset=UTF-8");
		//获取部门ID
		int id = Integer.parseInt(request.getParameter("Id"));
		PrintWriter out = null;
		
		Ddb d=new Ddb();
		
		try{
			out = response.getWriter();
		}catch(Exception e){
			e.printStackTrace();
		}
		JSONArray array = new JSONArray();
		JSONObject member = null;
		try{
			//根据种类的Id 来查找种类下的所有商品
			List ClassesList = d.two2(id);
			for(Ddb classes:ClassesList){
				member = new JSONObject();
				member.put("classId", classes.getId_Pmenu());
				member.put("className", classes.getThing());
				member.put("Id_Brand", classes.getId_brand());   //三级下拉框的索引
				array.put(member);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		out.print(array.toString());
		return null;
	}

	//三级下拉框
		@RequestMapping("/view/ddbthree")
		public String ddbthree(HttpServletRequest request,HttpServletResponse response) throws Exception{
			response.setContentType("text/json; charset=UTF-8");
			//获取部门ID
			int id = Integer.parseInt(request.getParameter("Id"));
			PrintWriter out = null;
			
			Ddb d=new Ddb();
			
			try{
				out = response.getWriter();
			}catch(Exception e){
				e.printStackTrace();
			}
			JSONArray array = new JSONArray();
			JSONObject member = null;
			try{
				//根据种类的Id 来查找种类下的所有商品
				List ClassesList = d.three(id);
				for(Ddb classes:ClassesList){
					member = new JSONObject();
					member.put("classId", classes.getId_brand());
					member.put("className", classes.getBrand());
					member.put("Id_Model", classes.getId_model());   //四级下拉框的索引
					array.put(member);
				}
			}catch(Exception e){
				e.printStackTrace();
			}
			out.print(array.toString());
			return null;
		}	
		
		//四级下拉框
		@RequestMapping("/view/ddbfour")
		public String ddbfour(HttpServletRequest request,HttpServletResponse response) throws Exception{
			response.setContentType("text/json; charset=UTF-8");
			//获取部门ID
			int id = Integer.parseInt(request.getParameter("Id"));
			PrintWriter out = null;
					
			Ddb d=new Ddb();
					
			try{
				out = response.getWriter();
			}catch(Exception e){
				e.printStackTrace();
			}
			JSONArray array = new JSONArray();
			JSONObject member = null;
			try{
				//根据种类的Id 来查找种类下的所有商品
				List ClassesList = d.four(id);
				for(Ddb classes:ClassesList){
					member = new JSONObject();
					member.put("Id_Model", classes.getId_model());
					member.put("className", classes.getModel());
					member.put("id_no", classes.getId_no());
					array.put(member);
				}
			}catch(Exception e){
				e.printStackTrace();
			}
			out.print(array.toString());
			return null;
		}			
	}

四、model代码
1.Ddb.java

package edu.model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Ddb {
	//四级联动获取数据库数据	
	private String kind;
	private String thing;
	private String brand;
	private String model;
	private int id_menu;	
	private int id_Pmenu;
	private int id_brand;
	private int id_model;

	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public String getModel() {
		return model;
	}
	public void setModel(String model) {
		this.model = model;
	}

	public int getId_brand() {
				return id_brand;
		}
	public void setId_brand(int id_brand) {
				this.id_brand = id_brand;
			}
	public int getId_model() {
				return id_model;
			}
		public void setId_model(int id_model) {
				this.id_model = id_model;
			}	
		public String getKind() {
				return kind;
			}
			public void setKind(String kind) {
				this.kind = kind;
			}
			public String getThing() {
				return thing;
			}
			public void setThing(String thing) {
				this.thing = thing;
			}	
			public int getId_Pmenu() {
				return id_Pmenu;
			}


			public void setId_Pmenu(int id_Pmenu) {
				this.id_Pmenu = id_Pmenu;
			}
			public int getId_menu() {
				return id_menu;
			}
		public void setId_menu(int id_menu) {
				this.id_menu = id_menu;
			}
//查询种类(一级下拉框)
			public ArrayList two() throws SQLException, ClassNotFoundException{
				DBOper db=new DBOper();
				String sql="select * from dbo.[kind] ";
				ResultSet rs=db.exeQuery(sql);
				ArrayList list=new ArrayList();
				while(rs.next()){
					Ddb d=new Ddb();
					d.setId_menu(rs.getInt(1));		
					d.setKind(rs.getString(2));
					list.add(d);
				}
				return list;
			}	
			//查询名称(二级下拉框)
			public ArrayList two2(int id_Pmenu) throws SQLException, ClassNotFoundException{
				DBOper db=new DBOper();
		//		String sql="select * from dbo.[menu2] where id_Pmenu='"+id_Pmenu+"'";
				String sql="select * from dbo.[thing] where id_Pmenu='"+id_Pmenu+"'";
				ResultSet rs=db.exeQuery(sql);
				ArrayList list=new ArrayList();
				while(rs.next()){
					Ddb d=new Ddb();
					d.setId_Pmenu(rs.getInt(1));		
					d.setThing(rs.getString(2));	
					d.setId_brand(rs.getInt(3));        //加了一个三级下拉框
					list.add(d);
				}
				return list;
			}	
			
			//查询品牌(三级下拉框)
			public ArrayList three(int id_brand) throws SQLException, ClassNotFoundException{
				DBOper db=new DBOper();
				String sql="select * from dbo.[brand] where id_brand='"+id_brand+"'";
				ResultSet rs=db.exeQuery(sql);
				ArrayList list=new ArrayList();
				while(rs.next()){
					Ddb d=new Ddb();
					d.setId_brand(rs.getInt(1));		
					d.setBrand(rs.getString(2));
					d.setId_model(rs.getInt(3));			//加了一个四级下拉框
					list.add(d);
				}
				return list;
			}	
			
			//查询型号(四级下拉框)
			public ArrayList four(int id_model) throws SQLException, ClassNotFoundException{
				DBOper db=new DBOper();
				String sql="select * from dbo.[model] where id_model='"+id_model+"'";
				ResultSet rs=db.exeQuery(sql);
				ArrayList list=new ArrayList();
				while(rs.next()){
					Ddb d=new Ddb();
					d.setId_model(rs.getInt(1));		
					d.setModel(rs.getString(2));
					d.setId_no(rs.getInt(3));	
					list.add(d);
				}
				return list;
			}			
		}

2.连接数据库(这里用的是SQL Server,所以需要SQL Server的JDBC驱动,如果是MySql就换成MySql的驱动)
DBOper.java

package edu.model;

import java.sql.*;
public class DBOper {
	private String uri="jdbc:sqlserver://localhost:1433;DatabaseName=wjDB";//数据库名字叫wjDB,这里改成自己建的数据库名
	private String user="sa";
	private String password="sa";
	private Connection con;
	private Statement st;
	private ResultSet rs;
	//构造方法
 	public DBOper() throws ClassNotFoundException, SQLException{
 		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
 		con=DriverManager.getConnection(uri,user,password);
 	}
 	//执行查询
 	public ResultSet exeQuery(String sql) throws SQLException{
 		st=con.createStatement();
 		rs=st.executeQuery(sql);
 		return rs;
 	}
 	//执行更新
 	public void exeUpdate(String sql) throws SQLException{
 		st=con.createStatement();
 		st.executeUpdate(sql);
 	} 	
}

五、相关配置文件
1.web.xml



  
  
    index.jsp
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
  
    contextConfigLocation
    WEB-INF/applicationContext.xml
  
  
  
    springMVC
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      WEB-INF/springMVC.xml
    
    1
  
  
  
    springMVC
    *.do
  


2.springMVC.xml



         
	
	

	
	

	
	
	
	
	
		
	


3.applicationContext.xml



	
	
		
		
		
		
		
		
	
	
      
        

六、数据库中的表
1.kind表
在这里插入图片描述
Web多级联动下拉框(从数据库中获取数据,MVC模式,使用的是tomcat)_第2张图片
2.thing表
Web多级联动下拉框(从数据库中获取数据,MVC模式,使用的是tomcat)_第3张图片
Web多级联动下拉框(从数据库中获取数据,MVC模式,使用的是tomcat)_第4张图片
3.brand表
Web多级联动下拉框(从数据库中获取数据,MVC模式,使用的是tomcat)_第5张图片
Web多级联动下拉框(从数据库中获取数据,MVC模式,使用的是tomcat)_第6张图片
4.model表
Web多级联动下拉框(从数据库中获取数据,MVC模式,使用的是tomcat)_第7张图片
Web多级联动下拉框(从数据库中获取数据,MVC模式,使用的是tomcat)_第8张图片
注意:后面三张表的第一个数字要和前一张表对应

以上代码都能从文件中找到
附上文件连接链接:https://pan.baidu.com/s/1EOaBxsvpcH_nPQKccXavtA
提取码:rney
复制这段内容后打开百度网盘手机App,操作更方便哦

你可能感兴趣的:(技术)