Javaweb JDBC Page/listener (16)

Source: Internet
Author: User

Multi-Table association operations 1. One-to-many (customer and order form)
# one-to-many (customer with order form) 0. C3P0 Tool Class 1. to establish the corresponding table structure for the Construction domain class Customer table private int id;private String name;private list<orders> Orders = new arraylist< Orders> (); Orders table private int id;private String num;private float money;private Customer customer;2. Customerdaoimpl class Addcustomer (Customer C) Customer FindByCustomerID (int i,boolean isdelay)//isdelay whether to display the corresponding information 3. Test class Testadd () method Customer C = new Customer (), C.setid (1), C.setname ("Red Elder sister"), orders O1 = new orders (), O1.setid (1); O1.setnum (" 201401 "); O1.setmoney (+), orders O2 = new orders (); O2.setid (2); O2.setnum (" 201402 "); O2.setmoney (2000);// Establishing a Relationship testquery () method

2. Many-to-many (student and teacher table structure)
# Many-to-many (student and teacher table structure) 0. C3P0 Tool Class 1. Building domain class and corresponding table structure Student information table Studentprivate int id;private string Name;private string Grade;private list<teacher> Teachers = new arraylist<teacher> (); Teacher Information table private int id;//integerprivate String name;private float money;private L ist<student> students = new arraylist<student> (); 2. Teacherdaoimpl class void Addteacher (Teacher T1)//judge whether the student exists, why should judge, because if not to judge, when added to the second teacher, the student union again added to the table, the primary key has also bound Teacher Findteacherbyid (int i) 3. Test class Addtest () Teacher t1= new Teacher (); T1.setid (1); T1.setname ("QB"); T1.setmoney (10000); Teacher t2= New Teacher () T2.setid (2); T2.setname ("WYJ"); T2.setmoney (10000); Student S1 = new Student () S1.setid (1); S1.setname ("DDD"); S1.setgrade ("A"); Student s2 = new Student () S2.setid (2); S2.setname ("DJJ"); S2.setgrade ("A"); Testquery () 

Paging Technology
1. To establish the corresponding table structure of the domain class CREATE TABLE account (ID int primary key, name varchar (sum), money float); Insert to account V Alues (1, ' aaa ', + +); INSERT into account values (2, ' BBB ', 1300); Insert to account values (3, ' CCC ', 1100); INSERT INTO Account VALUES (4, ' DDD ', 1400), insert into account values (5, ' Eee ', 13300), insert into account values (6, ' FFF ', 1100); Insert into account values (7, ' Zhang San ', 22100), insert to account values (8, ' Xiao Chen ', 13300); INSERT into account values (9, ' Gillian ', Insert into account values (10, ' 5500 ', "+"), insert into account values (11, ' brother Li ', +); INSERT into account values (12, ' GGG ', (); INSERT into account values ("HHH", +);------------------------------------------ Or use circular statements to insert data in bulk Accountdaoimpl DAO = new Accountdaoimpl (); Account A = new account (); Random r = new Random (); String base = Unicode.getunicode (), for (int i=1; i<100; i++) {A.setid (i); A.setname (Base.charat (R.nextint ( Base.length ())) + "" "+base.charat (R.nextint (Base.length ())) +" "+base.charat (R.nextint (Base.length ())) +" "); A.setmoney (10000.00F+I*20);d Ao.updateaccount (a);} ------------------------------------------Java class accountprivate int id;private String name;private float money;2. The data access Layer Accountdaoimpl class int getcount ()//Gets the total record list<account> getaccountbypage (int startIndex, int size)//Gets each page of content, Used as a paging query 3. Establish Pagebean class private int prep;//Previous page Getprep if the current page is less than 1, the previous page equals 1;prep = 1; otherwise the previous page equals = = Current Page -1;prep = pageno-1;private int nextp;// Next GETNEXTP If the next page is greater than the total number of pages, the next page equals the total number of pages nextp = This.gettotalpage (); otherwise the next page equals the current page +1 nexp = pageno+1;private int totalpage;// Total Pages-->gettotalpage If the total number of records to be displayed per page shows the number of records equals 0 totalpage=totalrecordes/pagesize otherwise totalpage=totalrecordes/pagesize+1; private int startindex;//Index of the first record of the current page (current page-1) *pagesize--->getstartindex//in order to join the scrolling number private int endpageno;private int beginpageno;1. If the current page is less than 9, the start page is 1 and the end page is the total number of pages 2. Otherwise start Page-4, end page corresponding +4 if the start page is less than 0beginpage=1, if the end page is greater than the total number of pages endpageno=this.gettotalpage ();p rivate int pageno=1;//the current page private int pagesize=3;//number of records displayed per page private int totalrecordes;//total records private list recordes;//the current page records private String url;4. Business Layer ACCountserviceimpl class void Findaccountbypage (Pagebean pb)//Paging query sets the total number of records and the current page record 5. ControllerServlet1. Garbled problem/Generate a paging component/get new current page/2. If the current page is not empty and is not equal to NULL, set the current page to 3 in the paging component. Call the business method to implement paging query 4. Sets the URL5 in the paging component. Set the paging Component object to 6 in the request domain. Forward to page in page design 1. Index.jsp<jsp:forward> forwarded to servlet 2. List.jsp the data in the database is displayed in the table statically contains PAGE.JSP3. Page.jsp1. The current page/Total page shows the previous page 2. Scrolling numbers traverse each page begin/end3. Next page 4. Select page (Change event onchange=changepage (this)) traverse each page changepage (objopt) {Address bar Information submit page go to option value}
Listener 1. Custom Listener
# Custom Observer Mode 1. Define listener Interface Studentlistener Listening learning method void Prestudy (Studentevent event); The method of listening to meals void Preeat (Studentevent event); 2. Define the event class Studentevent define the event source object/The parameter construct/getter/setter3. The event itself student Student Class 1. Name/Listener object, with reference construction 2. void Addstudentlistener (Studentlistener listener) 3. void study () If the listener object is not empty, perform a listening learning method of 4. void Eat () If the listener object is not empty, perform a listener eating method 4. Test Student class/Add Event Source/Execute method in student
2.8 listeners in the servlet specification
Perceptual Httpsessionbindinglistenner: Whether the listener itself goes to the httpsession Httpsessionactivationlistener: Listening for self-deactivation and activation of listener events: Event Source: Event handlers: Listening for three objects creating and destroying servletcontext/httpsession/ Servletrequestservletcontextlistenerhttpsessionlistenerservletrequestlistenner monitoring data changes in the domain Servletcontextattributelistennerhtt Psessionattributelistenerservletrequestattributelistener-Aware listener Httpsessionbindinglistenner: Feel yourself when you are httpsession tied and Httpsessionactivationlistener: feel yourself when you implement step 1 with the httpsession deactivation and activation listener. Write class (implement the corresponding Listenner interface) 2. Configure Web. XML registration <listener><listener-class> full path name </listener-class></listener>
3. Show online users and be able to kick people
1. Loginservlet class 1.1 Solve garbled problem 1.2 get login parameter 1.3 Call business logic, determine whether the user login is successful ... 1.4 Create a User object, encapsulate the login parameters 1.5 set user to session field 1.6 Redirect to Main Page 2. Listen to the data in the domain using the listener, if you have added a value to the Session field, you can use the listener class to hear it, so create a new Userlistener class, implement the Httpsessionattributelistener class, Implement the method configuration web.xml2.1 first obtained by Httpsessionbindingevent to session object 2.2 Gets the user object in session 2.3 if the object is empty directly return 2.4 Create user Object 2.5 Determines whether the object in the session is the user class, if the object is assigned to user2.6 directly if the user object is not equal to null through the session object gets the ServletContext classic code implementation to set the map to the context domain (note map to synchronize ) to add the session object to the Map collection in 3. Kick out Kickservlet Class 3.1 get username parameter 3.2 will username handle garbled, encode decode 3.3 get ServletContext3.4 get map3.5 in the context domain to get map session3.6 If the session is not equal to NULL, remove the user3.7 from the session by username remove the user 3.8 Redirect to the home page design 1 from the collection. LOGIN.JSP provides a login page, submitted to the login LoginServlet2. INDEX.JSP Home page, if the user in session is not empty, display the online list of users because the user needs to be displayed globally, so there needs to be a list of users in the map, and then iterate through the collection using <c:url > tags to encapsulate the address bar information, Request parameter username, value, traverse key to add kick Man feature link to URL

Javaweb JDBC Page/listener (16)

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.