Web project Summary--the ability to make additions and deletions to Oracle via Jsp+servlet

Source: Internet
Author: User

1. DAO pattern subcontracting: Set up sequentially

Entity package, which is the property of the object that corresponds to the table structure in the Oracle database, that is, what is the object

DAO: Adding and deleting the interface, realizing the specific method of adding and removing and checking

Service: With DAO, is also an interface, an interface implementation class, methods and so on the same as DAO

Servlet: Create a new servlet class, inherit the HttpServlet class, a method to build a servlet class, choose to use Doget (), DoPost () method, Services () with both doget and doPost, depending on different methods

New JSP page

These are the new Web projects

2, Save method, corresponding SQL statement insert INTO

The data prototype is what makes, when the user wants to deposit a new record, the user enters in the JSP page, obtains in the servlet, deposits the entity object, invokes the service layer the Save method, the service layer then return to the DAO layer

, and then insert into the database

3. Update data method is similar to add. More simple than add, directly get the ID call the Update method, will return a complete entity object, the entity object to the scope, forwarded to edit.jsp;

Different points: The JSP page needs to backfill data to the table with an El expression

Paging Query function

Everything in Java object, create a new Pagebean object in the Util package, including the pageindex page number, List results query result set, ROWCOUNT Total row count/pagesize number of rows per page =pagecount

Write the interface on the DAO layer. Also called the Find method, the pageindex page number ( in the servlet layer gives the initial value, in the JSP layer requires the user to pass in ),pagesize per page line (in the servlet layer to the provision of a few per page), Fuzzy query search field name (in table) (required by user)

Write the implementation class. Connect to the database first, execute the SQL statement that queries all the records, get the total number of pages and line numbers

If true, then a paged SQL statement is executed, taken from the result set to encapsulate the book entity class, and then encapsulate the Pagebean object

The DAO layer is responsible for finding ways to get data from the database; The servlet layer is responsible for getting data from the JSP, forwarding or redirecting to the JSP

The following is the servlet layer: first consider is doget or dopost,doget is to query data, search box, so we have to solve the Chinese garbled, first set pageIndex=1,pageSize= 2, determine whether the browser page number

A, data paging query

b, Data paging display

The review step begins with the database, SQL statements, and JDBC connects the Java program to the database, allowing the Java program to manipulate the data in the database

Start at the bottom, layer up, and last until the JSP display layer

1. SQL statements

COUNT () Aggregate function count Total rows

Row_number () over (partition by deptno ORDER BY sal DESC) partition clauses are partitioned by department number, dividing the query results into different groups, similar to group by; sort clauses, sorting each partition. Assigning line numbers to query results

ORDER BY desc Descending/ASC Ascending by default

SELECT * FROM book queries all columns in the Book table

Select Results Assign line numbers, all columns in the Book table from the book where title is like?

Select row_number () over (order by id DESC) as rn,all columns in the Book table from the book b where title like?

The name of the column alias is the as Name Table name alias is a direct name can

Assign row numbers, id descending, fuzzy queries to all column query results in a table

Select t.*
From (select Row_number () up (Order by id DESC) as rn,b.*
From book B
Where title like '%a% ') t
where T.rn between 2 and 14;

All the lines I wanted were shown in terms of the query, but I wanted to display the lines on each page, so that every page was displayed correctly.

Query steps

1. Get the total number of rows that meet the query criteria first

Select COUNT (*) as Count from book where title like '%a% ';

Make the SQL statement where like query conditional into a search bar, create a new Pagebean object to display the paging situation

To establish a connection to the database, execute the SQL statement, get the total number of rows, and the PageIndex and pagesize servlet layer will first give a default value

Set the line number per page

2. Execute the SQL statement again

Start paging.

Remove the data from the database (result set) DAO----->entity into the object Ah, in the collection AH service layer Returns DAO layer service----->servlet<-----jsp

3. servlet Layer

To invoke a paged query method on the list page

Because you need the user to pass the argument, you have to make a non-null judgment.

In the servlet layer, as long as the forwarding is written xxx.jsp

As long as the redirect is "list" Response.sendredirect ("list");

4, index.jsp

Search bar

Summary of the slash at the beginning of the URL

1. When setting the SRC attribute, href attribute in the HTML tag
The "/" at the beginning represents the server root directory (WEB-APPS) Tomcat directory structure, and the Web application publishing directory

2. When configuring a servlet or forwarding
The "/" at the beginning represents the Webroot directory of the Project

3. At the time of redirection, the "/" at the beginning represents the server root directory (WEB-APPS)

Path issue when forwarding redirection:

When forwarding the path, basically is the xxx.jsp page, because there is data to be forwarded to the page and then displayed to the user to see

There are two situations when redirecting a path: A. Log out directly to the login.jsp page, redirect to the login.jsp page after successful registration, and the path is the same level

B. After the successful login to access the background background/list;

Web project Summary--the ability to make additions and deletions to Oracle via Jsp+servlet

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.