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>