用AJAX实现级联菜单功能
BaseDao.java
package com.LJ.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao{
/**
* 产生一个servletID
*/
private static final long serialVersionUID = -3005086949118705373L;
//定义各种数据库的链接常量
private static final String DRIVER_NAME="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String DRIVER_URL="jdbc:sqlserver://localhost:1433;DatabaseName=computer";
private static final String DB_USER_NAME="sa";
private static final String DB_USER_PWD="sa";
//获取数据库链接对象
public static Connection getConn() throws ClassNotFoundException, SQLException{
Class.forName(DRIVER_NAME);
Connection conn = DriverManager.getConnection(DRIVER_URL, DB_USER_NAME,
DB_USER_PWD);
return conn;
}
// public static Connection getConn() throws NamingException, SQLException{
// //初始化应用上下文
// InitialContext ic = new InitialContext();
// //java:comp/env为固定的前缀,jdbc/empdatabase1则为配置的数据源的名称
// DataSource conn = (DataSource) ic.lookup("java:comp/env/jdbc/news");
// return conn.getConnection();
// }
//释放数据库资源
public static void CloseAll(Connection conn, PreparedStatement ps,
ResultSet rs) {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (ps != null)
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
ComputereDao.java
package com.LJ.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.LJ.entity.COMPUTER;
public class ComputerDao extends BaseDao {
private static Connection conn = null; // 保存数据库连接
private static PreparedStatement ps = null; // 用于执行SQL语句
private static ResultSet rs = null; // 用于保存查询结果集
// 增加一个方法向数据库查询出数据,并以集合的方式返回
public List<COMPUTER> getAllPoll(String locus){
List<COMPUTER> list = new ArrayList<COMPUTER>();
try {
conn = BaseDao.getConn();
String sql = " select brand from computero where locus = ? ";
ps = conn.prepareStatement(sql);
ps.setString(1, locus);
rs = ps.executeQuery();
while(rs.next()){
COMPUTER computer = new COMPUTER();
computer.setBrand(rs.getString("brand"));
list.add(computer);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
BaseDao.CloseAll(conn, ps, rs);
}
return list;
}
}
package com.LJ.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.LJ.entity.COMPUTER;
public class ComputerDao extends BaseDao {
private static Connection conn = null; // 保存数据库连接
private static PreparedStatement ps = null; // 用于执行SQL语句
private static ResultSet rs = null; // 用于保存查询结果集
// 增加一个方法向数据库查询出数据,并以集合的方式返回
public List<COMPUTER> getAllPoll(String locus){
List<COMPUTER> list = new ArrayList<COMPUTER>();
try {
conn = BaseDao.getConn();
String sql = " select brand from computero where locus = ? ";
ps = conn.prepareStatement(sql);
ps.setString(1, locus);
rs = ps.executeQuery();
while(rs.next()){
COMPUTER computer = new COMPUTER();
computer.setBrand(rs.getString("brand"));
list.add(computer);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
BaseDao.CloseAll(conn, ps, rs);
}
return list;
}
}
COMPUTER.java
package com.LJ.entity;
public class COMPUTER {
private int id;
private String brand;
private String locus;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getLocus() {
return locus;
}
public void setLocus(String locus) {
this.locus = locus;
}
public COMPUTER(int id, String brand, String locus) {
super();
this.id = id;
this.brand = brand;
this.locus = locus;
}
public COMPUTER() {
super();
}
}
package com.LJ.entity;
public class COMPUTER {
private int id;
private String brand;
private String locus;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getLocus() {
return locus;
}
public void setLocus(String locus) {
this.locus = locus;
}
public COMPUTER(int id, String brand, String locus) {
super();
this.id = id;
this.brand = brand;
this.locus = locus;
}
public COMPUTER() {
super();
}
}
MessageServlet.java
package com.LJ.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.LJ.dao.ComputerDao;
import com.LJ.entity.COMPUTER;
public class MessageServlet extends HttpServlet {
/**
* 增加对servlet的ID处理
*/
private static final long serialVersionUID = 2315859662727740974L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置接收信息的字符集
request.setCharacterEncoding("UTF-8");
//接收浏览器端提交的信息
String locus = request.getParameter("locus");
//设置输出信息的格式及字符
response.setContentType("text/xml;charset=UTF-8");
response.setHeader("Cache-Control","no-cache");
//创建输出流
PrintWriter out = response.getWriter();
//依据结果输出不同的数据信息
out.print("<response>");
//查找该品牌具体型号
ComputerDao computerDao = new ComputerDao();
List<COMPUTER> list = computerDao.getAllPoll(locus);
for (int i = 0; i < list.size(); i++) {
COMPUTER computer = list.get(i);
out.println("<res>"+ computer.getBrand() +"</res>");
}
out.println("</response>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//如果请求为dopost请求,则调用doget方法
this.doGet(request, response);
}
}
ComputerDaoTest.java
joinMenu.js
web.xml
index.jsp