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