javabean与jsp查询数据库实现级联菜单的实例源码



jsp如何才能做出查询数据库数据,然后构成级联菜单呢?以下将为大家介绍javabean与jsp查询数据库实现级联菜单的实例源码:

<%@page contentType=”text/html; charset=gb2312″ language=”java” import=”java.sql.*” errorPage=”"%>
<jsp:useBean id=”connDB” class=”com.core.ConnDB” scope=”page”/>
<%
ResultSet rs_city = null;
ResultSet rs_Town = null;
ResultSet rs_province = null;
String sql = “”;
String sql_T = “”;
int SelectID_C = 0;
int SelectID_P=0;
String strCityID1=request.getParameter(“CityID”);
String strSelectID_P=request.getParameter(“ProvinceID”);
if (strSelectID_P!=null &&!strSelectID_P.equals(“”)) {
session.setAttribute(“SelectID_P”, strSelectID_P);
}
String sql_P = “SELECT * FROM tb_Area WHERE TypeID=1 or TypeID=4 or TypeID=5″;
try {
rs_province = connDB.executeQuery(sql_P);
if (rs_province.next()) {
if(session.getAttribute(“SelectID_P”)!=null){
SelectID_P= Integer.parseInt((String)session.getAttribute(“SelectID_P”));
}else{
SelectID_P=rs_province.getInt(“ID”);
}
%>
<html>
<head>
<title>级联菜单</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>
<link href=”style.css” rel=”stylesheet”>
<style type=”text/css”>
<!–
body {
margin-left: 0px;
margin-top: 0px;
background-image: url(Images/bg.gif);
}
.style1 {color: #FFFFFF}
.style2 {color: #a2bcc5}
–>
</style>
</head>
<script language=”javascript”>
function ChangeItem_P(){
var ProvinceID=form1.Province.value;
window.location.href=”index.jsp?ProvinceID=”+ProvinceID;
}
</script>
<script language=”javascript”>
function ChangeItem_C(){
var CityID=form1.city.value;
window.location.href=”index.jsp?CityID=”+CityID;
}
</script>
<body>
<table width=”400″ height=”242″ border=”0″ cellpadding=”-2″ cellspacing=”-2″ background=”Images/add.gif”>
<tr>
<td valign=”top”>
<table width=”400″ height=”271″ cellpadding=”-2″ cellspacing=”-2″>
<tr>
<td width=”11″ height=”85″>&nbsp;</td>
<td width=”373″>&nbsp;</td>
<td width=”14″>
<span class=”style2″></span>
</td>
</tr>
<tr>
<td height=”144″>&nbsp;</td>
<td valign=”top”>
<div align=”center”>
<form name=”form1″ method=”post” action=”">
<table width=”88%” height=”142″ border=”1″ cellpadding=”-2″ cellspacing=”-2″ bordercolor=”#669999″ bordercolordark=”#FFFFFF”>
<tr>
<td width=”32%” height=”27″ bgcolor=”#809EA4″>
<div align=”center” class=”style1″>省级名称:</div>
</td>
<td width=”68%”>
<%
sql = “select* from tb_area where TypeID=2 and father=” + SelectID_P + “”;
sql_T = “select* from tb_area where TypeID=3 and father=” + SelectID_P + “”;
rs_city = connDB.executeQuery(sql);
rs_Town = connDB.executeQuery(sql_T);
%>
<div align=”left”> &nbsp;
<select name=”Province” onChange=”ChangeItem_P()”>
<%
rs_province.first();
do {
%>
<option value=”<%=rs_province.getInt(“ID”)%>” <%if(rs_province.getInt(“ID”)==SelectID_P){out.print(“selected”);} %>><%=rs_province.getString(“Name”)%> </option>
<%
} while (rs_province.next()) ;
%>
</select>
</div>
</td>
</tr>
<tr>
<td height=”27″ bgcolor=”#809EA4″>
<div align=”center” class=”style1″>市/县名称:</div>
</td>
<td>
<div align=”left”> &nbsp;
<%if (rs_city.next()) {
if (strCityID1==null) {
SelectID_C=rs_city.getInt(“ID”);
}else{
SelectID_C = Integer.parseInt(request.getParameter(“CityID”));
}
%>
<select name=”city” onChange=”ChangeItem_C()”>
<%rs_city.first();
do {
%>
<option value=”<%=rs_city.getInt(“ID”)%>” <%if(rs_city.getInt(“ID”)==SelectID_C){%>selected<%}%>><%=rs_city.getString(“Name”)%> </option>
<%
} while (rs_city.next());
%>
</select>
</div>
<%} else { %>
<select name=”city” onChange=”ChangeItem_C()”>
<option value=”0″>—</option>
</select>
</div>
<%
}
%>
</td>
</tr>
<tr>
<td height=”27″ bgcolor=”#809EA4″>
<div align=”center” class=”style1″>区/镇/乡名称:</div>
</td>
<td>
<%
if (SelectID_C != 0) { //选择了市县级名称
sql_T = “select* from tb_area where father=” + SelectID_C + “”;
System.out.println(“sql_T!=0:”+sql_T);
rs_Town = connDB.executeQuery(sql_T);
}
%>
<%if (rs_Town.next()) {%>
<div align=”left”> &nbsp;
<select name=”town” id=”select5″>
<%rs_Town.first();
do { %>
<option value=”<%=rs_Town.getInt(“ID”)%>”><%=rs_Town.getString(“Name”)%></option>
<%
} while (rs_Town.next());
%>
</select>
</div>
<%} else { %>
<div align=”left”> &nbsp;
<select name=”town” id=”select5″>
<option value=”0″>—</option>
</select>
</div>
<%} %>
</td>
</tr>
<tr>
<td height=”33″ colspan=”2″>
<div align=”center”>
<input name=”Submit” type=”submit” class=”Style_button” value=”删除”>
<input name=”Button” type=”button” class=”Style_button” value=”关闭” onClick=”javascrip:window.close()”>
</div>
</td>
</tr>
</table>
</form>
</div>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td> </td>
<td>&nbsp;</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
<%
}
} catch (Exception e) {
System.out.println(“查询省级名称时出错:”+e.getMessage());
}
%>

ChStr.java处理出中文乱码问题的java文件:

package com.core;
public class ChStr {
/***************************************************
*功能:解决输出中文乱码问题,返回值为String
***************************************************/
public String chStr(String str){
if(str==null){
str=”";
}else{
try{
str=(new String(str.getBytes(“iso-8859-1″),”GB2312″)).trim();
}catch(Exception e){
e.printStackTrace(System.err);
}
}
return str;
}
/***************************************************
*功能:显示文本中的回车换行、空格,返回值为String
***************************************************/
public String convertStr(String str1){
if(str1==null){
str1=”";
}else{
try{

str1=str1.replaceAll(” “,”&nbsp;”);
str1=str1.replaceAll(“\r\n”,”<br>”);
}catch(Exception e){
e.printStackTrace(System.err);
}
}
return str1;
}
}


操作sqlserver数据库的javabean文件源码:

package com.core;

import java.sql.*;
public class ConnDB{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
public ConnDB(){
try{
Class.forName(“com.microsoft.jdbc.sqlserver.SQLServerDriver”);
}catch(java.lang.ClassNotFoundException e){
System.err.println(e.getMessage());
}
}
/***************************************************
*method name: executeQuery()
*功能:执行查询操作
*return value: ResultSet
****************************************************/
public ResultSet executeQuery(String sql){
try{
conn=DriverManager.getConnection(“jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_database02;user=sa;password=”);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}catch(SQLException ex){
System.err.println(ex.getMessage());
}finally{}
return rs;
}
/***************************************************
*method name: executeUpdate()
*功能:执行更新操作
*return value: int
****************************************************/

public int executeUpdate(String sql){
int result=0;
try{
conn=DriverManager.getConnection(“jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_database02;user=sa;password=”);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
result=stmt.executeUpdate(sql);
}catch(SQLException ex){
result=0;
}finally{}
return result;
}
/***************************************************
*method name: close()
*功能:关闭数据库链接
*return value: void
****************************************************/
public void close(){
try {
if (rs != null) rs.close();
}
catch (Exception e) {
e.printStackTrace(System.err);
}finally{}
try {
if (stmt != null) stmt.close();
}
catch (Exception e) {
e.printStackTrace(System.err);
}finally{}
try {
if (conn != null) {
conn.close();
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}finally{}
}
}

查找数据库里的相关数据,然后构成级联菜单是开发项目时经常要用到的,因此在这里把java源码与大家分享,希望可以给您带来帮助。现在您只需构建你的项目实例,把相关文件复制进去就行了,提醒各位,链接数据库时记得为你的项目加入相关的jar包,否则会报错的。