[英]Connecting to Oracle 11g with Eclipse and Tomcat Error
我在使用eclipse和tomcat 7连接到Oracle数据库11g时遇到问题。我得到的erroro代码是:
查找以下javax.naming.NameNotFoundException:此上下文中未绑定名称[myDataBaseName]。 找不到[myDataBaseName]。
context.xml
<?xml version="1.0" encoding="UTF-8"?> <Context>
<Resource name="jdbc/mario"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521/XE"
username="mario"
password="*135181mi"
maxActive="20"
maxIdle="30"
maxWait="-1"/>
web.xml
> <?xml version="1.0" encoding="UTF-8"?> <web-app
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns="http://java.sun.com/xml/ns/javaee"
> xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"
> version="2.5"<display-name>werbproject</display-name>
> <welcome-file-list>
> <welcome-file>index.html</welcome-file>
> <welcome-file>index.htm</welcome-file>
> <welcome-file>index.jsp</welcome-file>
> <welcome-file>default.html</welcome-file>
> <welcome-file>default.htm</welcome-file>
> <welcome-file>default.jsp</welcome-file> </welcome-file-list>
> <resource-ref> <description>Oracle Datasource</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>
测试连接方式
<html>
`enter code here`<head>
<%@ page errorPage="errorpg.jsp"
import="java.sql.*,
javax.sql.*,
java.io.*,
javax.naming.InitialContext,
javax.naming.Context" %>
</head>
<body>
<h1>JDBC JNDI JSP Resource Test</h1>
<%
System.out.println("Console--testLine L14********************");
out.println("<BR>Browser--testLine L15");
String dsString = "java:/comp/env/myDataBaseName";
InitialContext initCtx = new InitialContext();
if ( initCtx == null ) {
throw new Exception("Uh oh -- no context!");
}
DataSource ds = (DataSource) initCtx.lookup(dsString);
if ( ds == null ) {
throw new Exception("Data source not found!");
}
System.out.println("Console--testLine L26");
out.println("<BR>Browser--testLine L27");
Connection conn = null;
try {
conn = ds.getConnection();
if(conn == null) throw new Exception("No DB Connection");
System.out.println("Console--testLine L34");
out.println("<BR>Browser--testLine L35");
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("select * from discipline");
System.out.println("Console--testLine L39********************");
out.println("<BR>Browser--testLine L40");
out.println("<BR><BR><BR>");
%>
<table width=600 border=1>
<tr>
<th align=left>Name</th>
<th align=left>Discipline</th>
<th align=left>School ID</th>
</tr>
<%
while (rset.next()) {
%>
<tr><td> <%= rset.getString(1) %></td>
<td> <%= rset.getString(2) %></td>
<td> <%= rset.getInt(3) %></td>
</tr>
<% }
rset.close();
stmt.close();
} catch(SQLException e)
{
// Do exception catch such as if connection is not made or
// query is not set up properly
out.println("SQLException: " + e.getMessage() + "<BR>");
while((e = e.getNextException()) != null)
out.println(e.getMessage() + "<BR>");
} catch(ClassNotFoundException e)
{
out.println("ClassNotFoundException: " + e.getMessage() + "<BR>");
}
finally
{
//Clean up resources, close the connection.
if(conn != null)
{
try
{
conn.close();
initCtx.close();
}
catch (Exception ignored) {}
}
}
%>
</table>
</body>
</html>
环顾四周,找不到任何可提供帮助的帖子。 任何帮助都会很棒。
resource-ref
定义了“逻辑”名称,但是您已经使用了物理名称。 然后,您尝试查找逻辑名,但是尚未在resource-ref
声明该逻辑名,它告诉您。
首先:确保context.xml
位于META-INF
文件夹下。
第二个:在查找myDataBaseName
,资源名称为jdbc/mario
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.