当前位置导航:炫浪网>>网络学院>>网页制作>>JSP教程

Oracle & JSP 开发的小型信息管理系统 (三) 源代码2


//articleconn.jsp 数据库连接文件,几乎被每个页面包含,有的页面去掉获得Session,修改一下这个代码,很简单

<%@ page contentType="text/html;charset=big5"%><%@ page import="java.sql.*"%>

  <%       request.setCharacterEncoding("big5");  Connection conn=null;  Statement stmt=null;  try{  Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();  String url="jdbc:oracle:thin:@192.168.56.199:1521:asdb";  String user="system";  String password="oracle01";  conn=DriverManager.getConnection(url, user, password);  }  catch(Exception e1){  e1.printStackTrace();}    %>

//************************************manage.jsp管理页面,提供分页,修改,删除,添加的链接

<%/*Cookie cookies[]=request.getCookies();if(cookies==null){ response.sendRedirect("login.jsp"); }String userName=(String)session.getAttribute("admin");out.println("Welcome, "+userName);if(userName==null){response.sendRedirect("login.jsp");}*/%><%@ include file="articleconn.jsp"%><html>

<head><meta http-equiv="Content-Type" content="text/html; charset=big5"><title>管理文件</title><meta name="GENERATOR" content="Microsoft FrontPage 4.0"><link rel="stylesheet" href="css/style.css"></head>

<%request.setCharacterEncoding("big5");String query=" Select * "+             " from learning order by article_id ";String title=null;            String queryCount=" select count(*)from learning  ";String strPage=request.getParameter("strPage");int intPage;if(strPage==null){intPage=1;}else{intPage=Integer.parseInt(strPage);}

int pageSizes=10;//每頁顯示信息數目int counts=1;//信息總條數int pageCounts=1;//總頁數

try{PreparedStatement ps=conn.prepareStatement(queryCount);ResultSet rs=ps.executeQuery();if(rs.next()){counts=Integer.parseInt(rs.getString(1));}



%><body bgcolor="#FFFFFF">          <br>  <p align="left">   <a href="add.jsp">添加信息</a>           <a href="index.jsp">返回首頁</a>        </p>         <p>&nbsp;</p>  <table width="700" border="1" cellspacing="0" cellpadding="0" align="center" bordercolorlight="#000000" bordercolordark="#FFFFFF">    <tr bgcolor="#99CCFF">      <td height="10" bgcolor="#B5D85E">        <div align="center">          <b>管 理 界 面</b>        </div>      </td>    </tr>    <tr>      <td height="49">        <%-- 分頁 --%>          <table border="1" cellspacing="0" width="90%" bgcolor="#F0F8FF" bordercolorlight="#000000" bordercolordark="#FFFFFF" align="center">          <tr>            <td width="12%" align="center">              <strong>ID 號</strong>            </td>            <td width="13%" align="center">              <b>類 型</b>            </td>            <td width="47%" align="center">              <strong>信 息 名 稱</strong>            </td>            <td width="14%" align="center">              <strong>修 改</strong>            </td>            <td width="14%" align="center">              <strong>刪 除</strong>            </td>          </tr>          <%

//計算總頁數if(counts%pageSizes==0){pageCounts=counts/pageSizes;}else{pageCounts=counts/pageSizes+1;}

if(intPage>pageCounts){intPage=pageCounts;}int i=(intPage-1)*pageSizes;

ps=conn.prepareStatement(query);ResultSet rs1=ps.executeQuery();/*while(rs1.next()){String temp=rs1.getString("title");out.println(temp);}*/if(counts!=1){for( int  j=0; j<i ; j++)rs1.next();}//out.println(rs1+"<BR><BR><BR> ");

 

  i=0;while(rs1.next()&&i<pageSizes){ if(counts==0) {out.println("<font size=4 color=red>沒有您想看的數據!</font>");}%>

          <tr>            <td width="12%" height="7">              <p align="center">                <%= rs1.getString("article_id")%>              </p>            </td>            <td width="13%" height="7">              <div align="center">                <%= rs1.getString("types")%>              </div>            </td>            <td width="47%" height="7">              <%title= rs1.getString("title");              out.println(title);              %>            </td>            <td width="14%" align="center" height="7">              <a href="edit.jsp?id=<%=rs1.getInt("article_id")%>">修 改</a>            </td>            <td width="14%" align="center" height="7">              <a href="delete.jsp?id=<%=rs1.getInt("article_id")%>&title=<%=title%>">刪 除</a>            </td>          </tr>          <% i++;}%>        </table>        <%

}catch(SQLException se1){se1.printStackTrace();}finally{}

conn.close(); %>      </td>    </tr>

  </table>    <form action="manage.jsp" method="post" >第<%=intPage%>頁,共<%=pageCounts%>頁

<%if(intPage<pageCounts){%><a href="manage.jsp?strPage=<%=intPage+1%>">下一頁</a><%}if(intPage>1){%>  <a href="manage.jsp?strPage=<%=intPage-1%>">上一頁</a><%}%><br>到第<input type="text" maxlength="5" name="strPage" value="1"/>頁進行查看<input type="submit" value="確認" class="buttonface"/>

</form>

         <br>  <p align="left">   <a href="add.jsp">添加信息</a>           <a href="index.jsp">返回首頁</a>        </p>  <P>&nbsp;</P></body>

</html>

//******************************************FilterSpecial .java过滤用户输入

package moonsoft.test.j2ee.news;



public class FilterSpecial {  public FilterSpecial()    {  }  public static String filter(String input)  {    if(!hasSpecialChars(input))    {     return input;    }    StringBuffer filtered =new StringBuffer(input.length());    char c;    for(int i=0; i<=input.length()-1; i++)    {    c=input.charAt(i);    switch(c)    {      case '<':filtered.append("&lt;"); break;      case '>':filtered.append("&gt;"); break;      case '"':filtered.append("&uot;"); break;      case '&':filtered.append("&amp;"); break;      default: filtered.append(c);    }          }    return (filtered.toString());  }  public static boolean hasSpecialChars(String input)  {    boolean flag=false;    if((input!=null)&&(input.length()>0))    {      char c;      for(int i=0; i<=input.length()-1; i++)      {        c=input.charAt(i);              switch(c)        {          case '>': flag=true; break;          case '<': flag=true; break;          case '"': flag=true; break;          case '&': flag=true; break;

        }      }    }    return flag;  }}
相关内容
赞助商链接