The JDBC Connection database implements the Delete function

Source: Internet
Author: User

This time also use the part of the code previously written to change, to achieve a delete function, this function is very good to add, the form as long as the form of a parameter to Deleteservlet, so the focus of the code is still in the Deleteservlet to implement business logic.

The SQL statement that we applied to IS.

String sql = "Delete from Tb_book where id=?";

Therefore, a form form is required to pass in an ID value. We then delete the data for that ID based on the ID value.

1

First, let's take a look at this project.

Findservlet and book we have already had, can copy come over without modifying any code

Index.jsp does not have to make any changes.

2

Let's focus on a code in book_list.jsp and Deleteservlet

book_list.jsp

Introduce the relevant package






A script is also required to validate the input legitimacy.

Then add the columns in the table (how many <tr></tr> combinations there are on how many rows, and how many of the <td></td> combinations are there)

Iterates through the data in the collection with a For loop, and each column pulls the data in response to adding a hyperlink to the

<a href= "Deleteservlet?id=<%=book.getid ()%>" > Delete </a>

The GetID () value obtained from the book instance is stored in the ID and passed into the deleteservlet.

2





It is important to note that

Get book ID
int id = integer.valueof (request.getparameter ("id")); the ID in parentheses is passed from the form, and is a string type that needs to be transformed

And

String sql = "Delete from Tb_book where id=?";

And

Ps.setint (1, id);

Modify the first argument in the first statement, where the modification is actually a delete operation.

(PS: Here again to emphasize why use

Get PreparedStatement
PreparedStatement PS = conn.preparestatement (SQL);

The object of the PreparedStatement is to perform preprocessing of the SQL statement with parameters. Know this is enough, I Baidu when busy busy with a lot of information, too lazy to understand.

At last

Execute the DOS mode of MySQL to see the data before the change.

Then run the project and I clicked on the delete first one


Jump to this page again (new page after deletion) to see that the first piece of data was deleted.

The egg hurts me to turn on the DOS again and verify that it really was deleted

Summarize:

I remember the self-taught JSP is from March 24 that night brother told me what is called MVC began, then listen to the true abstraction, and now is April 5, after a poor 11 days. From the database do not know why things, connect jdbc why. To slowly fumble, write a test database connection results code.

There was a lot of egg ache and silly things,

1

For example, do not understand the installation of MySQL (tnnd, I thought that the installation of MySQL will have a similar to the Warcraft page will be displayed on the desktop, wipe, in fact, there is no);

2

Again, for example, the installation of JDBC to download the driver package, download and do not know where to lose, but also to see a lot of Baidu information;

3

Again, for example, to the back of the discovery, MySQL originally tnnd is a visual operation plug-ins and so on, do not look under that disgusting dos.

4

And then found that the previous days of self-taught database statements, in the JDBC connection is only used to select this simple sentence (of course, the future may need to use the stored procedure)

5

In the specific project, the URL of the jump page was my own, and I debug for a half-day, are not debugging out. And then I found out it was my egg. The hand changed the Web. However, I changed web. XML, but also carefully observed the servlet added when the corresponding code appears.

6

Learned HTML forms, some common HTML language, table tables and other applications. Of course, the role and specification of JavaScript in JSP page is less. and <% add Java code%>

7

The understanding of MVC is even deeper, and as JavaBean learns, it also learns that Eclipse has a shortcut to automatically generate a set get method. (I did not know before I was a little knock)

For MVC I can now explain it in my own words.

V is the UI, which is the form that can be interpreted as HTML

M is JavaBean (also become Usebean)

C Nature is the servlet.

8

Usually new Java classes or Servlets are all default to the default package, that is, I do not usually set the package name. Now find that some of the package name is better, so it is not so messy. For example, Usebean and Servelt are both suffixes. Java, which is often wrong if it is not set when the project package name is set. If the project is larger then it may not be easy to maintain.

It is clear to understand that JavaBean can be said to be a bridge between V and C. and JSP self-study process, the most core of the non-servlet mo belongs to. Especially when it comes to database operations. It can be said that JSP learning is the servlet learning.

The JDBC Connection database implements the Delete function

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.