Use Ajax to get Web form data and store it in one of the databases (using JDBC)

Source: Internet
Author: User

The so-called JDBC is the use of Java and Database connection technology, from the database to obtain the existing information or to store information on the Web page to the database.

Here is a brief introduction to the company's part of a small project, because the code is more, so with the image of the form of display. Source code Please check the source library, upload later.

Figure 1-Infographic 2-user graph

As shown in the two modules, corresponding to the database of two tables. The first table is used to show the information of some posts, and the second one represents the user information table, which we use to display the user part information.

The database used for this time is Oracle, so you must first create a table in the database.

Figure 3-Build table

Create two tables in Oracle that correspond to the two graphs shown above. The first table java02_xp_table1 is the user Information table, when the information is filled in and click the Submit button, the information will be stored in the database, in the plsql immediately can be queried. The second table java02_xp_table2 is the information table, the database for this table already has data, just through the query bar information in the database displayed on the page. The information is shown in 1.

Figure 4-Project structure diagram

As shown in 4 as the structure of the Web project, under Eclipse, we first establish two entity classes, namely two JavaBean, corresponding to two tables of information. One of the user information

public class User {
Private String xp_id;
Private String Xp_name;
Private String Xp_email;
Private String Xp_subject;
Private String xp_message;

Automatically generate the Get,set method with Eclipse. This is no longer a statement.

}

Similarly, the Get,set method is also generated for the second table

public class Information {
Private String table_id;
Private String table_name;
Private String Table_price;
Private String Table_info1;
Private String Table_info2;
Private String Table_info3;
Private String Table_info4;
Private String Table_info5;

Automatically generate the Get,set method with Eclipse. There is no longer a statement

}

We built three DAO classes based on the success of JavaBean.

1.BaseDao---Immutable, encapsulates the underlying operation of the database, is the parent class of other DAO classes,

2.UserDao---Inherits Basedao, provides the operation interface from the user table in the database, implemented by USERDAOIMPL, Userdaoimpl---implements Userodao interface. This is basically the structure.

3. Informationdao the same function as Userdao. Other DAO classes are also inherited from Basedao. Implement operations on different tables.

Figure 5-basedao

As shown in 5, a basic DAO class is established, and the following two DAO are inherited with this basic DAO. Note that the driver_oracle, using Oracle to drive must be added to the corresponding jar package, this time the use of the Q-Oh ojdbc6.jar. Add the build path to the project file. Url_oracle is the connection string for the database, via localhost we know that the connected database is a local database, 1521 is the listening port of the Oracle database, MySQL is generally 3306, and then Oracle is the name of my database. The Class.forName () method loads the driver of the database Drivermanager.getconnection (url_oracle, "User2", "12345") is connected to the database, User2 is the user name of the current database, 12345 is the password.

This is to connect to the database using JDBC.

Figure 6-userdao

6 is the Userdao class. This class inherits the Basedao class, primarily defining a adduser () method, which is designed to add data to the Java02_xp_table1 table by clicking Submit. Here the use of pre-compiled methods to operate the database, do not understand, please refer to: http://blog.csdn.net/c1481118216/article/details/52838673.

The Executeupdate method is used to execute INSERT, UPDATE, or DELETE statements, as well as SQL,DDL (data definition language) statements, such as CREATE table and DROP table. The effect of an INSERT, UPDATE, or DELETE statement is to modify one or more columns in 0 or more rows in a table. The return value of Executeupdate is an integer that simply returns the number of rows affected (that is, the update count). For statements that do not manipulate rows such as CREATE table or DROP table, the return value of executeupdate is always zero.

Figure 7-informationdao

Informationdao inherits from Basedao, mainly to information this table to carry on the data operation, realizes to the database Java02_xp_table2 table's content query function. After executing the query, the data is encapsulated, encapsulated into a list, and the data is transformed by the Get,set method.

Figure 8-informationservice Figure 9-userservice

For two service, these two service is primarily to implement two methods. Informationservice is to get the number of updates, and UserService is to implement the AddUser () method.

The main thing is two servlets, all of which have the same methods, but the functions implemented in the Dopost () method are different.

Figure 10-userservlet

In Userservlet, the corresponding data is obtained from the page by means of the Request.getparameter () method. The format of the data uses JSON and the corresponding jar package is added using JSON. After the corresponding data on the page is obtained, the AddUser () method is implemented by passing the set method to user. The value of row is to confirm the amount of data passed, and it is a small bright spot to confirm whether the delivery is successful.

Figure 10-informationservlet

The main function of the Iformationservlet is to query the data and query the information that already exists in the database.

Figure 11-jquery Figure 12-ajax

Both of these methods are the techniques used in HTML. This is also a fixed form of the template. Used for the delivery of page data.

This is probably the structure of the project, so don't forget to configure it in Web. Xml.

Use Ajax to get Web form data and store it in one of the databases (using JDBC)

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.