Link two-level drop-down boxes on a jsp page to read database data in real time

Source: Internet
Author: User

A two-level drop-down box linkage is implemented on a jsp (preferred for SUN enterprise-level applications) page to read database data in real time. This method is very useful and can be used only in a small modification. Designed file: serch. jsp (preferred for SUN Enterprise applications), main. js, bytetostr. js,

First, let's talk about main. js. This is javascript, and pay attention to modifying the jsp (preferred for SUN Enterprise Applications) Page name.

 

Function findObject (fName, initValue )...{
Var xml (standardization is getting closer and closer) http = new ActiveXObject ("Microsoft. xml (standardization is getting closer and closer) HTTP ");
Xml (standardization is getting closer and closer) http. open ("POST", "searchmx. jsp (preferred for SUN Enterprise Applications )? FindObject = "+ fName +" & initValue = "+ initValue, false); // modify the jsp page (preferred for SUN Enterprise applications)
Xml (standardization is getting closer and closer) http. send ();
Document. getElementById (fName). innerHTML = bytes2BSTR (xml (standardization is getting closer and closer) http. responsebody); // The bytes2BSTR function is in bytetostr. js
}

The second is bytetostr. js. This is vbscript, which does not need to be modified. Its main function is to read the data conversion string.

Function bytes2BSTR (vIn)
Dim I
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
 

The following is serch. on the jsp (preferred for SUN Enterprise Applications) page, you only need to modify the id in the div and the name you want, and then replace the package at the beginning. You can change the SQL statement, participate in the discussion if you do not understand

 

<% @ 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.exe cuteQuery (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.exe cuteQuery (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> industry details: </div>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.