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 the 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 .....

<% 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 (); // the last 10 query statements are displayed and resultset rs = st.exe cutequery ("select * from data order by id desc limit 10") is arranged in reverse order "); // 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, enclose the column name with quotation marks.} Out. print ("</table> <HR>"); // 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

 

<% @ 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> 3. Put the first two codes together

Pay attention to the location of the Code.

<% @ 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> 

 

 


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.