• JAVA EE JSP_JNDI


    dsfdsa

    http://lindows.iteye.com/admin/blogs/213348

    public class DBAccess {
    
    	private static DataSource ds = null;
    
    	private static Connection connection = null;
    
    	public static Connection getCon() {
    		try {
    			Context ctx = new InitialContext();
    			String lookUpString = "java:comp/env/jdbc/myoracle";
    			ds = (DataSource) ctx.lookup(lookUpString);			
    			connection = ds.getConnection();
    		} catch (NamingException e) {
    			e.printStackTrace();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		return connection;
    	}
    
    	public static void closeCon(Connection con){		
    		if (null != con){
    			try {
    				con.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    		
    	}
    
     

    Tomcat 6 数据源与连接池配置

    http://www.blogjava.net/ec2008/archive/2008/07/19/216063.html

    project:test

    Driver path:

    D:\workspace\test\WebRoot\WEB-INF\lib\ojdbc14.10g.jar

    Datasource config

    D:\tomcat6.0\conf\server.xml

    <GlobalNamingResources>
    <!-- 其中将数据源参数配置在tomcat全局连接池中-->
      <Resource
        name="jdbc/oracle "
        type="javax.sql.DataSource"
        maxActive="4"
        maxIdle="2"
        username="scott"
        maxWait="5000"
        driverClassName="oracle.jdbc.driver.OracleDriver"
        validationQuery="select 1 from dual"
        password="tiger"
        url="jdbc:oracle:thin:@localhost:1521:orcl"/>
    </GlobalNamingResources>
     

    JNDI config

    D:\workspace\test\WebRoot\WEB-INF\web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee "
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">
     <!-- 其中加入应用JNDI配置 -->
     <resource-ref>
      <description>DB Connection </description>
       <!-- JNDI 命名-->
      <res-ref-name>jdbc/oracle </res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Application </res-auth>
     </resource-ref>
    或
     <resource-ref>
      <description>DB Connection </description>
      <res-ref-name>jdbc/oracle </res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container </res-auth>
     </resource-ref>
    </web-app>
     

    Tomcat 6 数据源与连接池配置 方法二

    project:test

    Driver path:

    D:\workspace\test\WebRoot\WEB-INF\lib\ojdbc14.10g.jar

    Datasource config

    D:\tomcat6.0\conf\Catalina\localhost\test.xml

    D:\tomcat6.0\conf \context.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <Context path="/test " docBase="D:\workspace\test\WebRoot " reloadable="true">
    <!-- 其中将数据源参数配置在tomcat局部连接池中-->
      <Resource name="jdbc/oracle"
          type="javax.sql.DataSource"
          username="scott"
          password="tiger"
          driverClassName="oracle.jdbc.OracleDriver"
          validationQuery="select 1 from dual"
          maxIdle="2"
          maxWait="5000"
          url="jdbc:oracle:thin:@localhost:1521:orcl"
          maxActive="4"/> 
    </Context>
     

    page test ok

    D:\workspace\test\WebRoot\index.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
    <%  
        String path = request.getContextPath();  
        String basePath = request.getScheme() + "://"  
                + request.getServerName() + ":" + request.getServerPort()  
                + path + "/";  
    %>  
    <%@ page import="java.sql.*"%>  
    <%@ page import="javax.naming.*"%>  
    <%@ page import="javax.sql.*"%>  
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    <html>  
        <head>  
            <base href="<%=basePath%>">  
            <title>tomcat datasource test</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>  
            <%  
                Context initContext = new InitialContext();  
                //java:/comp/env 或  java:/comp/env/ 固定写法  
                Context envContext = (Context) initContext  
                        .lookup("java:/comp/env/");  
                DataSource ds = (DataSource) envContext.lookup("jdbc/oracle");  
                //方法二  
                //DataSource ds = (DataSource) initContext.lookup("java:/comp/env/jdbc/oracle");  
                Connection conn = ds.getConnection();  
                Statement stmt = conn.createStatement();  
                ResultSet rs = stmt.executeQuery("select * from dept");  
                out.println("结果集是否为空:" + (rs == null) + "<p>");  
                while (rs.next()) {  
                    String deptno = rs.getString(1);  
                    String dname = rs.getString(2);  
                    String loc = rs.getString(3);  
                    out.println("\t部门编号:" + deptno + "\t部门名称:" + dname + "    地点:"  
                            + "\t" + loc + "<p>");  
                }  
                out.println("finally");  
                try {  
                    if (rs != null) {  
                        rs.close();  
                    }  
                    if (stmt != null) {  
                        stmt.close();   
                    }  
                    if (conn != null) {  
                        conn.close();  
                    }  
                } catch (SQLException e2) {  
                    e2.printStackTrace();  
                }  
            %>  
        </body>  
    </html>  
     

    end

  • 相关阅读:
    hive基本操作与应用
    理解MapReduce计算构架
    熟悉HBase基本操作
    熟悉常用的HDFS操作
    爬虫大作业
    数据结构化与保存
    使用正则表达式,取得点击次数,函数抽离
    爬取校园新闻首页的新闻
    网络爬虫基础练习
    Hadoop综合大作业
  • 原文地址:https://www.cnblogs.com/lindows/p/14390496.html
Copyright © 2020-2023  润新知