JBuilder2005 JSP logon page (2)

Source: Internet
Author: User
Tags stringbuffer
Change User list to dynamic
The currently created login. jsp has a defect. If other users are added to the T_USER table in the database background, the new user will not appear on the page because the user name drop-down box is static code, therefore, it is necessary to change the user name list to dynamic code, and use the T_USER record in the database table to dynamically generate drop-down box data.
We use the UserList. ava class to complete this job. Create UserList. java in the project. The code is as follows:
Code List 5 UserList. java
1. package bookstore;
2. import java. SQL .*;
3.
4. public class UserList
5 .{
6. // obtain the user list code in the HTML drop-down box.
7. public static String getUserListHTML (){
8. Connection conn = null;
9. StringBuffer sBuf = new StringBuffer ();
10. try {
11. conn = DBConnection. getConnection ();
12. PreparedStatement pStat = conn. prepareStatement (
13. "select USER_ID, USER_NAME from T_USER ");
14. ResultSet rs = pStat.exe cuteQuery ();
15. while (rs. next ()){
16. sBuf. append ("<option value = '" + rs. getString ("USER_ID") + "'>" +
17. rs. getString ("USER_NAME") + "</option> ");
18 .}
19. return sBuf. toString ();
20.} catch (SQLException ex ){
21. ex. printStackTrace ();
22. return "";
23.} finally {
24. try {
25. if (conn! = Null ){
26. conn. close ();
27. conn = null;
28 .}
29.} catch (SQLException ex1 ){
30 .}
31 .}
32 .}
33 .}
UserList. java only provides a static getUserListHTML () method. This method obtains user records from the T_USER table of the background database and generates the option code of the HTML select component, such as 16th ~ 17 rows.
After creating this class, we reference this class to adjust login. jsp code: first, reference the UserList class through the import attribute in the page command label, and then adjust the content in the select option to the value returned by the getUserListHTML () method, as shown in the following figure:
Figure 7 code writing through CodeInsight
Replace the original static HTML code by using expression tags. It is worth mentioning that CodeInsight can also be used in JSP to input code like in Java classes. The adjusted login. jsp code is shown in bold as follows:
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.