使用Eclipse和Tomcat错误连接到Oracle 11g

时间:2013-10-27 19:48:48

标签: java eclipse oracle jdbc

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

查找此javax.naming.NameNotFoundException:名称[myDataBaseName]未绑定在此Context中。无法找到[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>

看了一下,找不到任何帖子可以提供帮助。任何帮助都会很棒。

2 个答案:

答案 0 :(得分:0)

resource-ref定义了“逻辑”名称,但您使用了物理名称。然后你试图查找逻辑名称,但是没有在resource-ref中声明,它会告诉你。

答案 1 :(得分:0)

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