jdbc+ajax实现级联菜单

2010-03-22

 

 

1.前台代码

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ page import="java.sql.*,java.io.*"%>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>Ajax级联菜单实现</title>  
  9. <script language="JavaScript" type="text/javascript">  
  10. <!--   
  11. var cache = [];   
  12. function getLevel2(){   
  13.     if(document.forms.LevelMenu.select1.selectedIndex == 0){   
  14.         //当一级菜单未被选中时,二级菜单保持初始状态   
  15.         document.forms.LevelMenu.select2.length = 1;   
  16.         return;   
  17.     }   
  18.     //如果当前二级分类没有缓存时,则从服务器获取数据   
  19.     if(!cache[document.forms.LevelMenu.select1.selectedIndex]){   
  20.         //创建跨浏览器的XMLHttpRequest对象   
  21.     var xmlhttp;   
  22.     try{   
  23.     //IE 5.0    
  24.         xmlhttp = new ActiveXObject('Msxm12.XMLHTTP');   
  25.     }catch(e){   
  26.         try{   
  27.         //IE 5.5 及更高版本   
  28.             xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');   
  29.         }catch(e){   
  30.             try{   
  31.             //其他浏览器   
  32.                 xmlhttp = new XMLHttpRequest();   
  33.             }catch(e){}   
  34.         }   
  35.     }   
  36.     xmlhttp.open("get","ShowTwoMenu.jsp?id="+document.forms.LevelMenu.select1.value);   
  37.     xmlhttp.onreadystatechange = function(){   
  38.         if(xmlhttp.readyState == 4){   
  39.             if(xmlhttp.status == 200){   
  40.                 cache[document.forms.LevelMenu.select1.selectedIndex] = eval("("+xmlhttp.responseText+")");   
  41.                 getLevel2();   
  42.             }   
  43.         }   
  44.     }   
  45.     xmlhttp.send(null);   
  46.     return;   
  47.     }   
  48.     document.forms.LevelMenu.select2.length = 1;   
  49.     var _arr = cache[document.forms.LevelMenu.select1.selectedIndex];   
  50.     for (var i=0; i<_arr.length-1; i+=2){   
  51.         with(document.forms.LevelMenu.select2){   
  52.             options[options.length] = new Option(_arr[i], _arr[i+1]);   
  53.         }   
  54.     }   
  55. }   
  56. //-->  
  57. </script>  
  58. </head>  
  59. <body>  
  60. <%   
  61.     //驱动器名   
  62.     //数据库用户名和密码   
  63.     String userName = "root";   
  64.     String userPasswd = "1984428";   
  65.     //需要连接的数据库名   
  66.     String dbName = "studyajax";   
  67.     //表名   
  68.     String tableName = "articleType";   
  69.     //创建连接并执行查询操作   
  70.     Class.forName("com.mysql.jdbc.Driver").newInstance();   
  71.     Connection conn = DriverManager.getConnection(   
  72.             "jdbc:mysql://localhost:3306/" + dbName, userName,   
  73.             userPasswd);   
  74.     Statement statement = conn.createStatement();   
  75.     String sql = "SELECT * FROM " + tableName + " where level=0";   
  76.     ResultSet rs = statement.executeQuery(sql);   
  77. %>  
  78. <form name="LevelMenu"><select name="select1" id="select1"  
  79.     onchange="getLevel2()">  
  80.     <option value="0">请选择一级分类:</option>  
  81.     <%   
  82.         while (rs.next()) {   
  83.     %>  
  84.     <option value="<%=rs.getInt(1) %>"><%=rs.getString(2)%></option>  
  85.     <%   
  86.         }   
  87.         rs.close();   
  88.         statement.close();   
  89.         conn.close();   
  90.     %>  
  91. </select> <select name="select2" id="select2">  
  92.     <option value="0">请选择二级分类</option>  
  93. </select></form>  
  94.   
  95. </body>  
  96. </html>  

2. 后台代码实现

 

 

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ page import="java.sql.*,java.io.*"%>  
  4. <%   
  5.     //数据库用户名和密码   
  6.     int id = Integer.parseInt(request.getParameter("id").trim());   
  7.     //System.out.println("id:"+id);   
  8.     String userName = "root";   
  9.     String userPasswd = "1984428";   
  10.     //需要连接的数据库名   
  11.     String dbName = "studyajax";   
  12.     //表名   
  13.     String tableName = "articletype";   
  14.     //创建连接并执行查询操作   
  15.     Class.forName("com.mysql.jdbc.Driver").newInstance();   
  16.     Connection conn = DriverManager.getConnection(   
  17.             "jdbc:mysql://localhost:3306/" + dbName, userName,   
  18.             userPasswd);   
  19.     Statement statement = conn.createStatement();   
  20.     String sql = "SELECT id, name FROM " + tableName   
  21.             + "  where level=1 and parentId=" + id;   
  22.     //System.out.println("sql:"+sql);   
  23.     ResultSet rs = statement.executeQuery(sql);   
  24.   
  25.     //获取数据结果集   
  26.     response.setContentType("text/html; charset=UTF-8");   
  27.     response.setHeader("Cache-Control", "no-cache");   
  28.     PrintWriter pout = null;   
  29.     pout = response.getWriter();   
  30.     pout.print("[");   
  31.     while (rs.next()) {   
  32.         try {   
  33.   
  34.             pout.print("'" + (rs.getString("name")) + "',");   
  35.             pout.print(rs.getString("id"));   
  36.             pout.print(",");   
  37.         } catch (Exception e) {   
  38.             e.printStackTrace();   
  39.         }   
  40.     }   
  41.     pout.print("0]");   
  42.     rs.close();   
  43.     statement.close();   
  44.     conn.close();   
  45. %>  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值