0

我在使用 eclipse 和 tomcat 7 连接到 oracle 数据库 11g 时遇到问题。我得到的错误代码是:

查找此 javax.naming.NameNotFoundException:名称 [myDataBaseName] 未绑定在此上下文中。找不到 [myDataBaseName]。

上下文.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>

环顾四周,找不到任何可以帮助的帖子。任何帮助都会很棒。

4

2 回答 2

0

首先:确保context.xml位于META-INF文件夹下。
第二:资源名称是jdbc/mario,而你正在寻找myDataBaseName

于 2013-10-27T20:31:31.220 回答
0

resource-ref定义了“逻辑”名称,但您使用了物理名称。然后您尝试查找逻辑名称,但尚未在 a 中声明resource-ref,它会告诉您。

于 2013-10-27T19:59:19.860 回答