测试Oracle数据库的连接。 代码包含两种方法: 1)加载驱动 2)查找数据源 <!-- Description: 测试Oracle数据库连接 Author: xiaolong8 Date: 2009-09-28 19:30:20 --> <%@ page language="java" import="java.util.*,java.sql.*,javax.sql.*,javax.naming.*,org.apache.log4j.*;" pageEncoding="GBK"%> <% response.setCharacterEncoding("GBK"); %> <!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" mce_href="styles.css"> --> </head> <body> <% try { //Class.forName("oracle.jdbc.driver.OracleDriver"); //Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@134.128.138.69:1521:tccs", "username", "password"); Context ctx = new InitialContext(); DataSource ds=(DataSource)ctx.lookup("java:comp/env/ECCTestDataSource"); Connection conn=ds.getConnection(); Statement stmt=(Statement) conn.createStatement(); ResultSet rs=stmt.executeQuery("select staff_name from ecc_staff_manager g where rownum<10 "); int rsCount=0; while(rs.next()){ String strTemp=(String)rs.getString("staff_name"); out.println(strTemp+"<br>"); rsCount++; } //logger.debug("总数: " + rsCount); } catch (NamingException e) { // TODO Auto-generated catch block //e.printStackTrace(); out.println("NamingException"); } catch (SQLException se) { out.println("SQLException"); // TODO Auto-generated catch block se.printStackTrace(); //} catch (ClassNotFoundException ce){ // out.println("ClassNotFoundException"); } catch (Exception e){ out.println("Exception"); } %> </body> </html>