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