您的位置:首页 > 数据库 > Oracle

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

2017-12-28 22:12 423 查看
(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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐