Database Operations in JSP (2): Query databases on the JSP page

Source: Internet
Author: User

To query a database on the JSP page, you can connect to and query the java code section of the database and the html page code section. This page is implemented in three steps

1. Write the database connection code.

2. Write the code used to display the page

3. Insert the database connection code to the proper position of the page code.


1. database connection code
1.1 import an SQL package
<% Import = "java. SQL. *" %>

1.2 connect, query, and close the database
How can I check whether there are elephants in the refrigerator? 1. open the refrigerator door. 2. Take a look. 3. Close the refrigerator door. This process .....

[Html]
<%
Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
String url = "jdbc: mysql: // localhost: 3306/news ";
String user = "root ";
String password = "1234 ";
// Configure and connect to the database
Connection conn = DriverManager. getConnection (url, user, password );
Statement st = conn. createStatement ();
// Query statement. The last 10 results are displayed in reverse order.
ResultSet rs = st.exe cuteQuery ("SELECT * FROM data order by id desc limit 10 ");
// Output Header
Out. println ("<tr> <td> title </td> <td> content </td> <td> time </td> </tr> ");
// Output each query result in sequence
While (rs. next ()){
Out. print ("<tr> <td>" + rs. getString ("title") + "</td> <td>" + rs. getString ("content") + "</td> <td>" + rs. getString ("date") + "</td> </tr> <br> ");
// If the column name is used, quotation marks are required.
}
Out. print ("</table> // Disconnect the database
Conn. close ();
%>

2. HTML code of the page
To make the page more beautiful, do some beautification ~ Create a table to store data

 

[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"); %>
<Span> <span class = "tag"> </span> <! 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> query databases on the JSP page </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>
<Table>
<Tr>
<Td width = "174" id = "title"> title </td>
<Td width = "449" id = "title"> content </td>
<Td width = "161" id = "title"> time </td>
</Tr>


<Tr>
<Td width = "174"> </td>
<Td width = "449"> </td>
<Td width = "161"> </td>
</Tr>
 
</Table>
</Body>
</Html>
3. Put the first two codes together
Pay attention to the location of the 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>
<%
// 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>

<%
// Place the second row of the table in the while loop, and you can draw the table based on the query results. The parameter is placed in the corresponding position in <td>.
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>
 
<%}
// Note the location of "}" %>
</Table>
 
<%
Rs. close ();
Conn. close ();
%>
</Body>
</Html>
By: shirenfeigui

Related Article

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.