Two ways to operate MySQL database using Dynamic Data in JSP

Source: Internet
Author: User

There are two ways to change the contents of a database using Dynamic Data:
This defines the database connection as Conn
Suppose you have a form for data entry and submit it to the processing page
One is to use the precompiled format:
The format is as follows:

String name = Request.getparameter ("name");//Gets the value of name in the previous page form string password = request.getparameter ("password");// Get the value of name password in the previous page form string sql = "INSERT into user values (null,?,?)"; /define database operation statement PreparedStatement PST = conn.preparestatement (SQL);//Create Precompiled Object Pst.setstring (1,name);//For the first one? Assignment, assigns the name value obtained by the form to the first one? Pst.setstring (2,password);//For the second one? Assignment to assign the password value obtained by the form to the second one? Pst.executeupdate ();//Perform data insert operation

  

Two types are used in normal format:
The format is as follows:

String name = Request.getparameter ("name");//Gets the value of name in the previous page form string password = request.getparameter ("password");// Gets the value of name password in the previous page form, string sql = "INSERT into user values (null, '" + name + "', '" + Password + "')";//Define Database Operation statements Statement State = Conn.createstatement ();//Create Statement Object State.executeupdate (SQL);//Perform data insert operation

The specific code is as follows:

Form page: form.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

Data processing page: preparedstatement_test.jsp

<%@ page language= "java" import= "java.util.*,java.sql.*" pageencoding= "UTF-8"%><!  DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

  

Two ways to operate MySQL database using Dynamic Data in JSP

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.