How to put the elephant in the refrigerator this time! Everyone knows this!
We have two main problems to solve:
1. How to obtain the input content
2. How to insert time.
Directly Add code
[Html]
<% @ Page language = "java" import = "java. util. *, java. SQL. *" pageEncoding = "gb2312" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
<% Request. setCharacterEncoding ("UTF-8"); %>
<% Response. setCharacterEncoding ("UTF-8"); %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Style type = "text/css">
Table {width: 800px; margin: auto; padding: 5px; font-size: 12px; border: 0px; background: #00 CCFF ;}
Tr {background: # fff ;}
Td {padding: 5px ;}
# Title {text-align: center ;}
</Style>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> untitled document 11 </title>
</Head>
<Body>
<Form method = post>
Title: <input type = "text" name = "title"/> <br/>
Content: <input type = "text" name = "content"/> <br/>
<Input type = "submit" value = "submit"/>
</Form>
<%
// Connect to the MySQL database
Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
String url = "jdbc: mysql: // localhost: 3306/news ";
String user = "root ";
String password = "1234 ";
Connection conn = DriverManager. getConnection (url, user, password );
Statement st = conn. createStatement ();
%>
<Table>
<Tr>
<Td width = "174" id = "title"> title </td>
<Td width = "449" id = "title"> content </td>
<Td width = "161" id = "title"> time </td>
</Tr>
<%
ResultSet rs = st.exe cuteQuery ("SELECT * FROM data order by id desc limit 10 ");
While (rs. next () {%>
<Tr>
<Td width = "174"> <% = rs. getString ("title") %> </td>
<Td width = "449"> <% = rs. getString ("content") %> </td>
<Td width = "161"> <% = rs. getString ("time") %> </td>
</Tr>
<% }%>
</Table>
<%
// Obtain the input content using the name in the input. The line of time is to get the time and define the time format.
String getTitle = request. getParameter ("title ");
String getContent = request. getParameter ("content ");
Java. text. SimpleDateFormat time = new java. text. SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
String insertSQL = "insert into data (title, content, time) Values ('" + getTitle + "', '" + getContent + "', '" + time. format (new java. util. date () + "')";
St.exe cuteUpdate (insertSQL );
%>
<%
Rs. close ();
St. close ();
Conn. close ();
%>
</Body>
</Html>
By: shirenfeigui