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">
<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">
<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>
<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>