Beginners JSP Production Forum __jsp

Source: Internet
Author: User

A simple exercise, nothing to do with the foreground, using recursion, parameter transfer, database operations, session and character encoding conversion.

Of these, two major problems took some time:

The GetRow () method for a 1.ResultSet object is to return the line to which the pointer points, and next () to move to the next line

2. Character conversion problems can be used with new String (Str.getbytes ("iso-8859-1")), sometimes request.setcharacterencoding ("GB2312") conversion is not very effective. Or too much food, to be verified.

 --bbs.sql--Creating a database create database bbs; use bbs; --Create table--use Create table article (Id int primary key auto_increment,--parent Node ID pid  int,--root node ID rootid int, Title varchar (255), Cont text, Pdate datetime,--whether leaf nodes Isleaf int ); --0  represents leaf ,1 on behalf of non-leaf insert into article values  (null,0,1, ' Ant vs elephants ', ' Ant Wars Elephants ', now () , 1); insert into article values  (null,1,1, ' elephants are beaten down ', ' elephants are beaten down ', now (), 1); insert into article values  (null,2,1, ' ants are not too bad ', ' ants are Bad ', now (), 0); insert into article values  (null,2,1, ' nonsense ', ' nonsense ', now (), 1); insert into article values  (null,4,1, ' no nonsense ', ' no nonsense ', now (), 0); insert into article values  (null,1,1, ' How can ', ' How can ', ' Now (), 1); insert into article values  (null,6,1, ' How not possible ', ' how Impossible ', now (), 0); insert into article values  (null,6,1, ' possibilities are great ', ' possibilities are great ', now (), 0); insert into article values  (null,2,1, ' Elephant in hospital ', ' elephant into hospital ', now (), 1); insert into article values  (null,9,1, ' Nurse is ant ', ' nurse is Ant ', now (), 0);

 //showarticletree.jsp <%@ page language= "java"  contenttype= "text/html;charset=gb2312"  pageencoding= "gb2312"%> <%@ page import= "java.sql.*"  %> <% String path  = request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; Class.forName ("Com.mysql.jdbc.Driver"); string url =  "Jdbc:mysql://localhost:3306/bbs?user=root&password=root"; Connection conn = drivermanager.getconnection (URL); Statement smt = conn.createstatement (); Resultset rs = smt.executequery ("Select * from article where pid=0"); while ( rs.next ()  ) {    str =  "<tr><td>"  +  Rs.getint ("id")  +  "</td><td>"  +                "<a href=' Articledetail.jsp?id= '  + rs.getint ("id")  +  "' >"                 + rs.getstring ("title")  +  "</a>"  +   "</td><td>"  +  "<a href= ' delete.jsp?id="  +               rs.getint ("id")  +  "' >"  +  "Delete </ A> "+ " </td></tr> ";     if ( rs.getint ("IsLeaf")  != 0) {         tree (Conn,rs.getint ("id"), 1); &NBSP;&NBSP;&NBSP;&NBSP}} rs.close (); Smt.close (); Conn.close (); %> <%! String str= ""; Private void tree (connection conn,int id,int level)  {     string prestring =  "";     Statement stm = null;     ResultSet rs = null;   &Nbsp; for (int i=0;i<level;i++) {        prestring +=   "----"; &NBSP;&NBSP;&NBSP;&NBSP}     try{        stm  = conn.createstatement ();         rs = stm.executequery ("select * from  Article where pid= "+ id );         while (Rs.next ()) {             str +=  "<tr><td>"  + rs.getint ("id")  +  "</td><td>"  +                      preString +  "<a href= ' articledetail.jsp?id="   + rs.getint ("id")  +  "' >"                         + rs.getstring ("title")  +  "</a></td>" +  " <td> " + " <a href= ' delete.jsp?id= " +               rs.getint ("id")  +  "' >"  +  "delete </a>" +  "< /td></tr> ";             if ( rs.getint ("IsLeaf")  !=  0)                 tree ( Conn,rs.getint ("id"), level+1); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}     }catch (SQLException e) {         e.printstacktrace ();     }finally{        try{             if (rs != null) {                 rs.close ();                 rs =  null;             }              if (stm != null) {                 stm.close ();                 stm =  null;             }          }catch (sqlexception e) {             e.printstacktrace (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}    &nbsp}}%> <! doctype html public  "-//w3c//dtd html 4.01 transitional//en">

 //articledetail.jsp <%@ page language= "java"  contenttype= "TEXT/HTML;CHARSET=GBK"   pageencoding= "GBK"%> <%@ page import= " java.sql.* "  %> <% String path  = request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; String paraid=request.getparameter ("id"); Int id = integer.parseint (Paraid); Class.forName ("Com.mysql.jdbc.Driver"); string url =  "Jdbc:mysql://localhost:3306/bbs?user=root&password=root"; Connection conn = drivermanager.getconnection (URL); Statement smt = conn.createstatement (); Resultset rs = smt.executequery ("select * from article where id=" &NBSP;+&NBSP;ID); %> <! doctype html public  "-//w3c//dtd html 4.01 transitional//en" >

delete.jsp <%@ page language= "java" contenttype= "text/html;charset=gb2312" pageencoding= "GB2312"%>
<%@ page import= "java.sql.*"%> <%!
private void Delete (Connection conn,int ID) {
Statement stm = null;
ResultSet rs = null;
try{
STM = Conn.createstatement ();
String sql = "SELECT * from article where pid=" + ID;
rs = stm.executequery (SQL);
while (Rs.next ()) {
Delete (conn, rs.getint ("id"));
}
Stm.executeupdate ("Delete from article where id=" + ID);
}catch (SQLException e) {
E.printstacktrace ();
}finally{
try {
if (Rs!= null) {
Rs.close ();
rs = null;
}
if (STM!= null) {
Stm.close ();
STM = null;
}
}catch (SQLException e) {
E.printstacktrace ();
}
}
}%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; int id = integer.parseint (request.getparameter ("id"));
int pid = Integer.parseint (Request.getparameter ("pid")); Class.forName ("Com.mysql.jdbc.Driver");
String url = "Jdbc:mysql://localhost:3306/bbs?user=root&password=root";
Connection conn = drivermanager.getconnection (URL);
Conn.setautocommit (FALSE); Delete (Conn,id); Conn.commit ();
Conn.setautocommit (TRUE); String sql = "SELECT COUNT (*) from article where pid=" + pid;
Statement STM1 = Conn.createstatement ();
ResultSet rs = stm1.executequery (SQL);
Rs.next ();
int count = Rs.getint (1);
if (count <=0) {
Stm1.executeupdate ("Update article set isleaf=0 where id=" + pid);
}
Rs.close ();
Stm1.close ();
Conn.close ();
%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<base href= "<%=basePath%>" >

<title>my JSP ' delete.jsp ' starting page</title>

<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<!--
<link rel= "stylesheet" type= "Text/css" href= "Styles.css" >
-->
<body>

<% response.sendredirect ("showarticletree.jsp"); %>
</body>
reply.jsp <%@ page language= "java"  contenttype= "text/html; charset=gb2312"      pageencoding= "GB2312"%> <% String path = request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; Int id = integer.parseint (Request.getparameter ("id")); Int rootid = integer.parseint (Request.getparameter ("Rootid")); %> <! doctype html public  "-//w3c//dtd html 4.01 transitional//en" >

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.