JSP + JAVABEAN two-layer model development (implementation of the news center module)

Source: Internet
Author: User
1. conn. java

Package news;
Import java. SQL .*;

Public class conn {


String sDBDriver = "org. gjt. mm. mysql. Driver ";
String sConnStr = "jdbc: mysql: // localhost/test ";
Connection connect = null;
ResultSet rs = null;

Public conn ()
{

Try {Class. forName (sDBDriver );

} Catch (Exception e) {e. printStackTrace ();}

}


Public ResultSet executeQuery (String SQL ){

Try {
Connect = DriverManager. getConnection (sConnStr, "test_root", "test_root ");
Statement stmt = connect. createStatement ();
Rs1_stmt.exe cuteQuery (SQL );
} Catch (SQLException ex) {System. err. println (ex. getMessage ());}
Return rs;
}

Public int executeUpdate (String SQL ){
Int result = 0;
Try {
Connect = DriverManager. getConnection (sConnStr, "test_root", "test_root ");
Statement stmt = connect. createStatement ();
Result1_stmt.exe cuteUpdate (SQL );

} Catch (SQLException ex) {System. err. println (ex. getMessage ());}
Return result;


}

Public void close (){
If (connect! = Null ){
Try {
Connect. close ();
Connect = null;
}
Catch (SQLException ex ){
System. err. println (ex. getMessage ());}
}

}


}

2. mianNew. jsp

<% @ Page language = "java" contentType = "text/html; charset = gb2312" pageEncoding = "UTF-8" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<% @ Page import = "java. SQL. *" %>
<Jsp: useBean id = "conn" scope = "page" class = "news. conn"/>

<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> news MAIN </title>
</Head>
<Body>

<Table border = "0" cellpadding = "0" cellspacing = "0" style = "border-collapse: collapse"
Bordercolor = "#111111" width = "100%" id = "AutoNumber1">
<Tr>

& Lt; td width = "100%" & gt;

</Td>
</Tr>
<Tr>

& Lt; td width = "100%" & gt;

</Td>
</Tr>

</Table>

<Table border = "0" cellpadding = "0" cellspacing = "0" style = "border-collapse: collapse"
Bordercolor = "#111111" width = "100%" id = "AutoNumber2">
<Tr>

<Td width = "13%"> </td>
<Td width = "87">
<p>
<%
ResultSet rs, rsNest;
String strSql = null;
StrSql = "select * from news where type = 1 order by time desc ";
Rsw.conn.exe cuteQuery (strSql );

While (rs. next ()){
%>
<Span class = "s"> & nbsp;
<A href = "newsContent. jsp? NewsId = <% = rs. getInt ("id") %> "> <% = rs. getString (" Title ") %> </a> <br>
<%
}
%>
<P>
</p>

<%
StrSql = "select * from news where type = 2 order by time desc ";
Rsw.conn.exe cuteQuery (strSql );

While (rs. next ()){
%>
<Span class = "s"> & nbsp;
<A href = "newsContent. jsp? NewsId = <% = rs. getInt ("id") %> "> <% = rs. getString (" Title ") %> </a> <br>
<%
}
%>
<P>
</Td>
</Tr>
</Table>
<P align = "center"> copyright </p>
<P align = "center"> </p>
 

</Body>
</Html>

3. newsContent. jsp

<% @ Page language = "java" contentType = "text/html; charset = gb2312"
PageEncoding = "UTF-8" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<% @ Page import = "java. SQL. *" %>
<Jsp: useBean id = "conn" scope = "page" class = "news. conn"/>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> newsContent </title>
</Head>
<Body>

<Table border = "0" cellpadding = "0" cellspacing = "0" style = "border-collapse: collapse"
Bordercolor = "#111111" width = "100%" id = "autonumber1">
<Tr>

& Lt; TD width = "100%" & gt;

</TD>
</Tr>
<Tr>

& Lt; TD width = "100%" & gt;

</TD>
</Tr>

</Table>

<Table border = "0" cellpadding = "0" cellspacing = "0" style = "border-collapse: collapse"
Bordercolor = "#111111" width = "100%" id = "autonumber2">
<Tr>

<TD width = "13%"> </TD>
<TD width = "87">

<Div class = "title" align = "center" style = "width: 603; Height: 36">
<%
String id = "";
String keyword = "";
String rplContent = "";
ResultSet rs, rsNest;
String strSql = null;
Id = request. getParameter ("newsId ");
StrSql = "select * from news where id =" + id;
Rsw.conn.exe cuteQuery (strSql );

If (rs. next ()){
Keyword = rs. getString ("keyword ");

%>
<% = Rs. getString ("Title") %>
</Div>

<%
RplContent = rs. getString ("content"). replaceAll ("/n", "<br>"); // handle the line feed issue
%>

<Hr align = "center" width = "490" noShade SIZE = "1">
<P> <br>
& Nbsp; <span style = "font-size: 9pt" ><%= rplContent %>
<P> </td> </tr>
<Tr>
<Td width = "13%"> </td>
& Lt; td width = "87%" & gt;
<Span style = "font-size: 9pt"> News: </span> <p> <span class = "s">

<%
StrSql = "select * from news where id <>" + id + "and title like '%" + keyword + "% 'order by time desc ";
Rsw.conn.exe cuteQuery (strSql );

While (rs. next ()){
%>

<A href = "newsContent. jsp? NewsId = <% = rs. getInt ("id") %> "> <% = rs. getString ("Title") %> </a> <% = rs. getDate ("time") %> <br>

<%
}
}

%>
</Span>
</Td>
</Tr>
</Table>
<P align = "center"> copyright </p>
<P align = "center"> </p>
</Body>
</Html>

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.