两个JSP页面中都使用到访问数据库的代码, 我们将其封装。将数据库连接的代码专门放在一个声名当中。
1.创建一个JSP页面,将其命名为 a.inc 代码如下
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'a.inc' 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 is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%!
public Connection getConnection() throws Exception{
String DRIVER = "oracle.jdbc.driver.OracleDriver";
String URL = "jdbc:oracle:thin:@localhost:1521:MYFIRSTORACL";
String USER = "user1";
String PASSWORD = "user1";
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL,USER,PASSWORD);
return conn;
}
%>
</body>
</html>
显示页面index.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
// 将a.inc导入页面
<%@ include file = "a.inc" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.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 is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<table align = "center">
<caption>欢迎给老师投票</caption>
<tr bgcolor ="yellow">
<td>编号</td>
<td>姓名</td>
<td>得票数</td>
<td>投票</td>
</tr>
<%
//通过getcoonection()函数获取我们封装在a.inc的COON对象
Connection conn = getConnection();
Statement stat = conn.createStatement();
String sql = "select * from tvote";
ResultSet rs = stat.executeQuery(sql);
while(rs.next()){
String no= rs.getString("no");
String name = rs.getString("name");
int vate = rs.getInt("vate");
%>
<tr bgcolor="pink">
<td><%=no %> </td>
<td><%=name %> </td>
<td><img src ="images/a.jpg" width="<%=vate%>" height="2"><%=vate %></td>
<td><a href="vote.jsp?no=<%=no%>">投票</a> </td>
</tr>
<%
}
stat.close();
conn.close();
%>
</table>
<body>
</body>
</html>
显示的index.jsp 与投票的vote.jsp实现了不同的功能,所以卸载不同的页面。降低了耦合性 便于开发和维护
vote.jsp
//省略了基础代码
//将a.inc导入页面
<%@ include file = "a.inc" %>
//getcooection获取到连接对象
<body>
<%
String no = request.getParameter("no");
Connection conn = getConnection();
String sql = "update tvote set vate=vate+1 where no=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, no);
ps.executeUpdate();
ps.close();
conn.close();
%>
<jsp:forward page="index.jsp"></jsp:forward>
</body>
数据库中的创建表的代码
create table tvote (no varchar2(20),name varchar2(20), vate int);
总结,:不同的页面实现不同的功能,降低了耦合。 使用.inc文件创建一个函数,对重复使用的代码进行封装,可以减少重复代码,并且便于维护。