最进同学要我做了个二级联动菜单,参考了网上的一些资料,再自己做了些修改,自我感觉做的还可以了啊,呵呵,乱砍了!页面代码如下:
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
商品搜索
|
|
品牌选择:
|
|
商品信息:
|
|
package org.servlet;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.bean.SortBean;
public class ListDataServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
java.sql.Connection conn = null;
Statement db = null;
ResultSet rs;
String strSql = null;
ArrayList list = new ArrayList();
int i=0;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager
.getConnection(
"jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=gbk",
"root", "123456");
} catch (SQLException ex) {
System.out.println(ex.getMessage() + "路径错误");
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage() + "驱动错误");
}
try {
strSql = "select * from sort";
db = conn.createStatement();
rs = db.executeQuery(strSql);
while (rs.next()) {
SortBean sort = new SortBean();
sort.setName(rs.getString("name"));
sort.setId(rs.getInt("id"));
list.add(i, sort);
++i;
}
System.out.println(list.size());
ArrayList list1=new ArrayList();
request.getSession().setAttribute("list1",list);
response.sendRedirect("dyList.jsp");
} catch (SQLException e) {e.printStackTrace();}
}
}
这里我没用bean来实现数据库取数据的功能,而是直接在servlet里面实现了这些功能,代码可能太混乱了,敬请原谅,但是我希望大家能够直接拿过去用了!所以有些优化的事情我没去做了,以后要努力了!
DyListServlet代码如下:
package org.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DyListServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// 设置接收信息的字符集
java.sql.Connection conn = null;
Statement db = null;
request.setCharacterEncoding("UTF-8");
// 接收浏览器端提交的信息
String sort = request.getParameter("sort");
// 设置输出信息的格式及字符集
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
// 创建输出流对象
PrintWriter out = response.getWriter();
// 依据验证结果输出不同的数据信息
out.println("
ResultSet rs;
String strSql = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager
.getConnection(
"jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=gbk",
"root", "123456");
} catch (SQLException ex) {
System.out.println(ex.getMessage() + "路径错误");
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage() + "驱动错误");
}
try {
strSql = "select product.name,product.id from sort,product where sort.id=product.sortid and sort.name='"
+ sort + "'";
db = conn.createStatement();
rs = db.executeQuery(strSql);
while (rs.next()) {
out.println("
}
} catch (SQLException e) {
e.printStackTrace();
}
out.println(" ");
out.close();
}
}
首先我们访问listdataservlet加载顶级菜单的数据了!在后面用http://localhost:8080/TestMysql/listdataservlet访问testmysql是你工程名,然后就可以了!呵呵说到最重要的事就是后台的数据库结构,本人觉的没有这个的话前面的全部都没意义了!两张表
sort表 字段 id int name varchar
product表 字段 id int name varchar sortid int
最近感受好多了,最近学设计摸式学了将进两个月,今天晚上感觉看的非常爽了,好象懂了一些,但是不知道怎么用语言表达不出来了,想写些设计摸式的感受了,但是总觉的自己不知道怎么说,怎么写了,总是觉的没什么可写的了!呵呵,真的感觉最近进步还是有的,尤其是看了设计模式,好东西了,不管他能不能做事,但是做为一个程序员,如果连这个都没学习,那他不是个合格的程序员了,呵呵,希望最近写几篇关于设计摸式的文章啊,加油啊!