JSP基础语法之二:page指令,JSP连接Oracle数据库




一:编码方式和MIME

<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>




二:错误页面

<%@ page errorPage="myErrorPage.jsp"%><!-- 自定义的错误页面中要用page指令声明 isErrotPage="true" -->
自定义页面中

<%@ page isErrorPage="true"%>
<%
	response.setStatus(200);
%>





三:修改客户端下载jsp页面的名称

<%
	response.setHeader("Content-Disposition","attchement;filename=XXX.jsp");/*修改被下载jsp文件的名称*/
%>




四:错误的集中处理

<!-- 集中处理错误:虚拟目录\WEB-INF\web.xml 中加入error-apge标签 -->

	<welcome-file-list>
		<welcome-file>main.htm</welcome-file>
	</welcome-file-list>
	
	<error-page>
		<error-code>404</error-code>
		<location>/myErrorPage.jsp</location>
	</error-page>
	
	<error-page>
		<exception-type>java.lang.NullPointerException</exception-type>
		<location>/myErrorPage.jsp</location>
	</error-page>
	
	
</web-app>




五:JDBC连接Oracle11g数据库

Java连接Oracle数据库的范例详见:http://blog.csdn.net/ruantao1989/article/details/8012005

能够用import导入sql包之后,操作是大同小异的

<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.Date"%>


<html>
	<head>
		<title> JSP_JDBC_Demo </title>
	</head>

	<body>
		
	<%!
		private static final String DBDriver = "oracle.jdbc.driver.OracleDriver";//驱动  
		private static final String DBURL = "jdbc:oracle:thin:@localhost:1521:ORCL";//URL命名规则:jdbc:oracle:thin:@IP地址:端口号:数据库实例名
	
		private static final String DBUser = "scott";
		private static final String DBPassWord = "890307";
	%>	
	
	<%	
		Connection con = null;
		Statement st = null;
		ResultSet res = null;

		try {
		//连接
		Class.forName(DBDriver);//加载数据库驱动  
		con = DriverManager.getConnection(DBURL, DBUser, DBPassWord);//连接  
		st = con.createStatement();	
	%>
			<h2>已连接 <%=con%></h2>
	<%	
			
		res = st.executeQuery("SELECT pid,name,age,birthday,sex FROM person ");
		while(res.next())
		{
			int p = res.getInt("pid");
			String n = res.getString("name");
			int a = res.getInt("age");
			Date b = res.getDate("birthday");
			String s = res.getString("sex");
		
	%>		
				<h3><%=p%> <%=n%> <%=a%> <%=b%> <%=s%></h3>
	<%
		}
	} catch (Exception e) {
		System.out.println(e);
	}finally{
		//四:关闭连接		
		res.close();//依次关闭
		st.close();
		con.close();
	}
	%>
	
	
	</body>
	
</html>






















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值