This article will show you the following JSP connection MySQL to perform the insert Operation function, the specific example and code as follows, interested friends can understand the next
In the afternoon finally realized the JSP connection MySQL to perform the insert Operation function. Enter data on the INDEX.JSP page and submit to the mysql--insert.jsp page for the Insert database operation.
The index.jsp page code is as follows:
Copy Code code 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"/>
sex: <input type = "text" name= "Sex" value= "female"/>
Ages: <input type = "Text" name= "age" value= "/>
"
</br>
<input type = "Submit" value= "submitted"/>
</form>
</body>
</html>
The code for MYSQL--INSERT.JSP is as follows:
Copy Code code 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"); Get
from Form
String name=request.getparameter ("name"); Get
from Form
String sex=request.getparameter ("sex"); Get
from Form
String age=request.getparameter ("Age"); Get
from Form
java.util.Date date=new java.util.Date ();
String datetime=new Timestamp (Date.gettime ()). ToString ();
Try
{
/** Connection Database 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.executequery ("SET NAMES UTF8");
String insert_sql = "INSERT into PERSON_TB values (' + ID +" ', ' "+ name +" ', ' "+ Sex +" ', ' + "+" + ")";
String query_sql = "SELECT * from PERSON_TB";
try {
Stmt.execute (Insert_sql);
}catch (Exception e) {
E.printstacktrace ();
}
try {
ResultSet rs = stmt.executequery (query_sql);
while (Rs.next ()) {
%>
id:<%=rs.getstring ("ID")%> </br>
Name: <%=rs.getstring ("name")%> </br>
Sex: <%=rs.getstring ("Sex")%> </br>
Ages: <%=rs.getstring ("Age")%> </br> </br>
<%
}
}catch (Exception e) {
E.printstacktrace ();
}
//rs.close ();
Stmt.close ();
Conn.close ();
}catch (Exception e) {
E.printstacktrace ();
}
%>
</body>
</html>
To access the page after index.jsp:
Enter the test data and submit it to the following page:
The changes to the database are as follows:
About JSP connection to MySQL database garbled problem:
In the input data of the page encoding to use GB2312 or GBK, while receiving data in the encoding of the page to use UTF-8, in order to ensure that there is no garbled. As in the above example, the data is entered in the index.jsp, so it uses the <%@ page contenttype= "text/html;charset=gb2312"%> statement to indicate that the GB2312 encoding is used, and mysql_ insert.jsp receives data, so it uses the <% @page language= the "Java" pageencoding= "UTF-8"%> statement to indicate the use of UTF-8 encoding.
And for the data from MySQL and display the page, its encoding will also use GB2312, such as the following mysql_query.jsp file example to use the <%@ page contenttype= "text/html;charset= The gb2312 "%> statement indicates how the encoding is encoded.
Tomcat encodes the URL by default according to Iso-8859-1 so that it is converted accordingly.
Copy Code code as follows:
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.sql.*"%>
<body>
<%
Connection con=null;
String url= "jdbc:mysql://localhost/html_db?user=root&password=123456&useunicode=true& Characterencoding=8859_1 ";
//html_db is the database name
Class.forName ("Org.gjt.mm.mysql.Driver"). Newinstance ()//new 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 rs=stmt.executequery (SQL);
while (Rs.next ()) {%>
id:<%=rs.getstring ("ID")%> </br>
Name: <%=rs.getstring ("name")% > </br>
Sex: <%=rs.getstring ("Sex")%> </br>
Age: <%=rs.getstring ("aged")%> </br > </br>
<%}%>
<%out.print ("Successful database operation, congratulations!"); %>
<%
Rs.close ();
Stmt.close ();
Conn.close ();
%>
</body>