Database Operations in JSP (3): Database insertion on the JSP page

Source: Internet
Author: User

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
 

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.