Read the specific column values in MySQL into the drop-down box in the page

Source: Internet
Author: User

1. Technology used: jsp,spring JDBC (Mapper)

2. Code

2.1 Interface

 Public Interface Imeetingroomdao {    public list<mrcap> selectcap ();       Public List<mrfloor> Selectfloor ();}

2.2 Implementation Classes

@Override PublicList<mrcap>Selectcap () {List<Mrcap> caplist =NewArraylist<mrcap>(); //The SQL statement cannot look up the distinct value of two fields in a statement, check a field, and need to establish the mapper of the field separatelyString sql = "SELECT DISTINCT capacity from Meetingroom"; Caplist= Jt.query (SQL,NewCapmapper ()); returncaplist; } @Override PublicList<mrfloor>Selectfloor () {List<Mrfloor> floorlist =NewArraylist<mrfloor>(); String SQL= "SELECT distinct floor from Meetingroom"; Floorlist= Jt.query (SQL,NewFloormapper ()); returnfloorlist; }

Note: Mrcap and Mrfloor in the paradigm are newly created bean classes, with only one private property (Cap/floor) in the class, so you need to create a new corresponding mapper when using spring jdbc.

2.3 Capmapper

 Public class Implements Rowmapper<mrcap>{    @Override    publicintthrows  SQLException {         New Mrcap ();        Mrc.setcapacity (Rs.getint ("Capacity"));         return MRC;    }}

2.4 Floormapper with Capmapper, no code show

2.5 because the implementation class exists, you must add the <bean></bean> tag to the spring configuration file

class= "Com.dao.MeetingRoomDao" >        <property name= "DataSource" ref= "DataSource"/></bean >

Where Meetingroomdao has the following code to map the datasource:

    Private  DataSource DataSource;     Private JdbcTemplate JT;          Public void Setdatasource (DataSource ds) {        this. DataSource = ds;           This New JdbcTemplate (DataSource);    }

At this point, the contents of the drop-down box to be populated are read and placed in Caplist and Floorlist, respectively, in the drop-down box, using the El expression when placing the drop-down box >>>

Capacity:     <select name= "Capacity" id= "capacity" >        <option value= "" >--Please select--</option>        <c:foreach items= "${caplist}" var= "cap" >        <option value= "${cap.capacity}" ><c:out value= "${cap.capacity}" > </c:out></option>        </c:forEach>    </select> floor:    <select name= " Floor "id=" >        <option value= ">--Please select--</option> <c:foreach        items=" ${floorlist} " Var= "Floor" >        <option value= "${floor.floor}" ><c:out value= "${floor.floor}" ></c:out>< /option>        </c:forEach>    </select>

Summarize:

The values of the capacity and floor fields in the database are duplicated, and when querying with SQL statements, it is not possible to find out the distinct values of two fields in a single statement, and because the spring JDBC framework is used to query with the Jt.query () method, You need to create a new two bean class and create a new corresponding mapper map before you can finally put the drop-down box.

Read the specific column values in MySQL into the drop-down box in the page

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.