oracle以datasource数据源方式配置jdbc遇到的问题

(1)Name jdbc is not bound in this Context错误https://wenku.baidu.com/view/bd59cc47e518964bcf847cc3.html

https://stackoverflow.com/questions/12928030/javax-naming-namenotfoundexception-name-jdbc-eswastha-is-not-bound-in-this-co
(2)The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

最终配置是
web.xml中添加:

<resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/jdbc-test</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>

在META-INF文件夹新建context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/jdbc-test" auth="Container" type="javax.sql.DataSource"
        driverClassName="oracle.jdbc.driver.OracleDriver"
        url="jdbc:oracle:thin:@localhost:1521:orcl"
        username="c##zyx"
        password="root"
        maxActive="2"
        maxIdle="1"
        removeAbandoned="true"
        maxWait="300"
         />
</Context>

(3)tomcat的conf/context.xml

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

    <Resource name="jdbc/jdbc-test" auth="Container" type="javax.sql.DataSource"
        driverClassName="oracle.jdbc.driver.OracleDriver"
        url="jdbc:oracle:thin:@localhost:1521:orcl"
        username="c##zyx"
        password="root"
        maxActive="2"
        maxIdle="1"
        removeAbandoned="true"
        maxWait="300" />
        <!--autoReconnect="true"
        failOverReadOnly="false"
        maxReconnects="10"-->
</Context>

(4)tomcat的lib文件夹添加ojdbc8.jar
(5)最后写一个jsp文件测试:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.sql.*, javax.sql.*, javax.naming.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    test
    <%
    DataSource ds = null;
    Connection conn = null;
    ResultSet result = null;
    Statement stmt = null;
    ResultSetMetaData rsmd = null;
    try {
        Context context = new InitialContext();
        /*Context envCtx = (Context) context.lookup("java:comp/env");
        ds =  (DataSource)envCtx.lookup("jdbc/jdbc-test");*/
        ds = (DataSource) context.lookup("java:comp/env/jdbc/jdbc-test");
        if (ds != null) {
            conn = ds.getConnection();
            stmt = conn.createStatement();
            result = stmt.executeQuery("SELECT * FROM E8_USER");
        }
        while(result.next())
        {
            String username = result.getString("USERNAME");
            String password = result.getString("PASSWORD");
            out.println(username+" "+password);
        }
    } catch (SQLException e) {
        System.out.println("Error occurred " + e);
    }
    int columns = 0;
    try {
        rsmd = result.getMetaData();
        columns = rsmd.getColumnCount();
    } catch (SQLException e) {
        System.out.println("Error occurred " + e);
    }
%>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值