JSP+JAVABEAN兩層模開發式(新聞中心模組的實現)

來源:互聯網
上載者: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();
         rs=stmt.executeQuery(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();
             result=stmt.executeUpdate(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>

<td width="100%">
<img border="0" src="images/ruanjian.jpg" width="270" height="254">
</td>
</tr>
<tr>

<td width="100%">

</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">
<img border="0" src="images/mid-rdxw2.gif" width="101" height="18"><p>
<%
  ResultSet rs,rsNest;
  String strSql=null;
  strSql="select * from news where type=1 order by time desc";
  rs=conn.executeQuery(strSql);
 
  while(rs.next()){
  %>
  <span class="s">&nbsp;
  <a href="newsContent.jsp?newsId=<%=rs.getInt("id") %>"><%=rs.getString("Title")%></a><br>
  <%
     }
   %> 
  <p>
  <img border="0" src="images/mid-hyxw2.gif" width="94" height="19"></p>
 
  <%
  strSql="select * from news where type=2 order by time desc";
  rs=conn.executeQuery(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">aaronbai 著作權</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>

<td width="100%">
<img border="0" src="images/ruanjian.jpg" width="370" height="254">
</td>
</tr>
<tr>

<td width="100%">

</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;
  rs=conn.executeQuery(strSql);
 
  if(rs.next()){
    keyword=rs.getString("keyword");

 %>
 <%=rs.getString("Title") %>
 </div>

<%
  rplContent = rs.getString("content").replaceAll("/n","<br>");//處理換行府問題
 %>

<hr align="center" width="490" noShade SIZE="1">
<p><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-size:9pt"><%=rplContent %>
<p></td></tr>
<tr>
<td width="13%"> </td>
<td width="87%">
<span style="font-size: 9pt">相關新聞:</span><p><span class="s">

<%
  strSql="select * from news where id<>"+id+"and title like '%"+keyword+"%' order by time desc";
  rs=conn.executeQuery(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">aaronbai 著作權</p>
   <p align="center"> </p>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.