Implement the linkage of the secondary drop-down box in a jsp page and read the database data in real time. This method is very practical and only needs to be modified in a small way to use it. The designed files, serch.jsp, main.js, bytetostr.js,
let’s talk about main.js first, which is javascript, and pay attention to modifying the jsp page name.
function findObject(fName,initValue)...{
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("POST","searchmx.jsp?findObject="+fName+"&initValue="+initValue,false);//Pay attention to modifying the jsp page
xmlhttp.send();
document.getElementById(fName).innerHTML=bytes2BSTR(xmlhttp.responsebody); //bytes2BSTR function is in bytetostr.js
}
The second is bytetostr.js, which is vbscript. This does not need to modify any content. Its main function is to read data and convert strings.
Function bytes2BSTR(vIn)
dimi
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
Next is the serch.jsp page. You only need to modify the id in the div to the name you want, and then change the initial package. Just change the sql statement. If you don’t understand, you can participate in the discussion.
<%@ page contentType="text/html; charset=gb2312" language="java" import="com.sjth.zdsygl.vo.*,com.sjth.zdsygl.biz.*,java.util.*,com .sjth.zdsygl.jdbc.*,java.sql.ResultSet"%>//Import the corresponding package
<%
//This part of the code is used to query the database and return a string
if (request.getParameter("findObject")!=null)...{
if (request.getParameter("findObject").equals("hy_dm"))...{
DBConnect conn = null;
ResultSet rs = null;
try...{
out.print("<select name='hy_dm' onchange="javascript:findObject('hymx_dm',this.value)">");
String sql = "select * from dm_hy group by left(hy_dm,7)";
conn = new DBConnect();
conn.setPstmt(sql);
rs = conn.executeQuery(sql);
while(rs.next())...{
out.print("<option value='"+rs.getString("hy_dm")+"'>"+rs.getString("hy_mc")+"</option>");
}
out.print("</select>");
}
catch (Exception e)...{
}
finally...{
try...{
if (rs != null)
rs.close();
if (conn != null)
conn.close();
}
catch (Exception e)...{
e.printStackTrace();
}
}
}
if (request.getParameter("findObject").equals("hymx_dm"))...{
DBConnect conn = null;
ResultSet rs = null;
try...{
out.print("<select name='hymx_dm' >");
String sql = null;
if (request.getParameter("initValue").equals(""))...{
sql = "select * from dm_hy";
}
else...{
sql = "select * from dm_hy where hy_dm like '"+request.getParameter("initValue")+"%'";
}
conn = new DBConnect();
conn.setPstmt(sql);
rs = conn.executeQuery(sql);
while(rs.next())...{
out.print("<option value='"+rs.getString("hy_dm")+"'>"+rs.getString("hy_mc")+"</option>");
}
out.print("</select>");
}
catch (Exception e)...{
}
finally...{
try...{
if (rs != null)
rs.close();
if (conn != null)
conn.close();
}
catch (Exception e)...{
e.printStackTrace();
}
}
}
return;
}
%>
<html>
<head>
<link href="css/table.css" type="text/css" rel="stylesheet">
<script language=vbscript src="css/bytetostr.js"></script>
<script language=javascript src="css/main.js"></script>//Import two js files
</head>
<body>
<div>Industry:</div>
<div id="hy_dm"></div> //Used to display the returned string
<div>Detailed industry:</div>
<div id="hymx_dm"></div>//Used to display the returned string
</body>
</html>
<script language="javascript">
findObject("hy_dm","");
findObject("hymx_dm","");//These two items are page execution calls
</script>
Everyone is welcome to discuss