In the afternoon, I finally implemented the jsp connection to MySQL to perform the insert operation. Enter data on the index. jsp page and submit it to the mysql -- insert. jsp page for database insertion.
The index. jsp page code is as follows:
Copy codeThe Code is as follows:
<% @ Page language = "java" pageEncoding = "UTF-8" %>
<% @ Page contentType = "text/html; charset = UTF-8" %>
<%
Request. setCharacterEncoding ("UTF-8 ");
Response. setCharacterEncoding ("UTF-8 ");
Response. setContentType ("text/html; charset = UTF-8 ");
%>
<Html>
<Head>
</Head>
<Body>
<Form action = "mysql_insert.jsp" method = "post">
ID: <input type = "text" name = "id" value = "0"/>
Name: <input type = "text" name = "name" value = "aaa"/>
Gender: <input type = "text" name = "sex" value = "female"/>
Age: <input type = "text" name = "age" value = "20"/>
</Br>
<Input type = "submit" value = "submit"/>
</Form>
</Body>
</Html>
The mysql -- insert. jsp code is as follows:
Copy codeThe Code is as follows:
<% @ Page language = "java" import = "java. util. *, java. SQL. *" pageEncoding = "UTF-8" %>
<% @ Page contentType = "text/html; charset = UTF-8" %>
<%
Request. setCharacterEncoding ("UTF-8 ");
Response. setCharacterEncoding ("UTF-8 ");
Response. setContentType ("text/html; charset = UTF-8 ");
%>
<Html>
<Head>
<Title> add message into table </TITLE>
</Head>
<Body>
<%
String id = request. getParameter ("id"); // obtain from the form
String name = request. getParameter ("name"); // obtain from the form
String sex = request. getParameter ("sex"); // obtain from the form
String age = request. getParameter ("age"); // obtain from the form
Java. util. Date date = new java. util. Date ();
String datetime = new Timestamp (date. getTime (). toString ();
Try
{
/** Database connection parameters **/
String driverName = "com. mysql. jdbc. Driver"; // Driver name
String DBUser = "root"; // mysql user name
String DBPasswd = "123456"; // mysql password
String DBName = "html_db"; // Database Name
String connUrl = "jdbc: mysql: // localhost/" + DBName + "? User = "+ DBUser +" & password = "+ DBPasswd;
Class. forName (driverName). newInstance ();
Connection conn = DriverManager. getConnection (connUrl );
Statement stmt = conn. createStatement ();
Stmt.exe cuteQuery ("set names UTF8 ");
String insert_ SQL = "insert into person_tb values ('" + id + "', '" + name + "', '" + sex + "', '"+ age + "')";
String query_ SQL = "select * from person_tb ";
Try {
Stmt.exe cute (insert_ SQL );
} Catch (Exception e ){
E. printStackTrace ();
}
Try {
ResultSet rs = stmt.exe cuteQuery (query_ SQL );
While (rs. next ()){
%>
ID: <% = rs. getString ("id") %> </br>
Name: <% = rs. getString ("name") %> </br>
Gender: <% = rs. getString ("sex") %> </br>
Age: <% = rs. getString ("age") %> </br>
<%
}
} Catch (Exception e ){
E. printStackTrace ();
}
// Rs. close ();
Stmt. close ();
Conn. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
%>
</Body>
</Html>
Access the page after index. jsp:
Enter the test data and submit the data to the following page:
Database changes are as follows:
Garbled Characters During jsp connection to the MySQL database:
The encoding method on the page that inputs data must use GB2312 or GBK, And the encoding method on the page that receives the data must use a UTF-8 to ensure that no garbled code occurs at all. In the preceding example. jsp inputs data, so it uses the <% @ page contentType = "text/html; charset = gb2312" %> statement to indicate that GB2312 encoding is used, while mysql_insert.jsp receives data, so it uses the <% @ page language = "java" pageEncoding = "UTF-8" %> statement to indicate that it uses UTF-8 encoding.
The encoding method for pages that extract data from MySQL and display data must also be GB2312. The following mysql_query.jsp file example uses <% @ page contentType = "text/html; the charset = gb2312 "%> statement specifies the encoding method.
Tomcat encodes the url according to the iso-8859-1 by default, so the corresponding conversion is required.
Copy codeThe Code is as follows:
<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page import = "java. SQL. *" %>
<Html>
<Body>
<%
Connection con = null;
String url = "jdbc: mysql: // localhost/html_db? User = root & password = 123456 & useUnicode = true & characterEncoding = 8859_1 ";
// Html_db indicates the Database Name
Class. forName ("org. gjt. mm. mysql. Driver"). newInstance (); // create an instance
Connection conn = DriverManager. getConnection (url); // establish a Connection
Statement stmt = conn. createStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
String SQL = "select * from person_tb ";
ResultSet rs1_stmt.exe cuteQuery (SQL );
While (rs. next () {%>
ID: <% = rs. getString ("id") %> </br>
Name: <% = rs. getString ("name") %> </br>
Gender: <% = rs. getString ("sex") %> </br>
Age: <% = rs. getString ("age") %> </br>
<% }%>
<% Out. print ("the database operation is successful. Congratulations! "); %>
<%
Rs. close ();
Stmt. close ();
Conn. close ();
%>
</Body>
</Html>