javax.naming.NamenotFoundException:名称postgre未绑定在此上下文中

时间:2013-12-11 05:58:35

标签: java postgresql tomcat struts

我是struts的新手..我一直在尝试使用tomcat 6中的数据源连接到postgresql,但是我收到以下错误..

  *javax.naming.NameNotFoundException: Name postgre is not bound in this Context
    org.apache.naming.NamingContext.lookup(NamingContext.java:770)
    org.apache.naming.NamingContext.lookup(NamingContext.java:140)
    org.apache.naming.NamingContext.lookup(NamingContext.java:781)
    org.apache.naming.NamingContext.lookup(NamingContext.java:140)
    org.apache.naming.NamingContext.lookup(NamingContext.java:781)
    org.apache.naming.NamingContext.lookup(NamingContext.java:140)
    org.apache.naming.NamingContext.lookup(NamingContext.java:781)
    org.apache.naming.NamingContext.lookup(NamingContext.java:153)
    org.apache.naming.SelectorContext.lookup(SelectorContext.java:152)
    javax.naming.InitialContext.lookup(InitialContext.java:411)
    strutsTutorial.UserRegistrationAction.execute(UserRegistrationAction.java:30)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.jav
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)*

我在tomcat配置文件夹中的context.xml文件是..

***<Context>
    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <Resource name="jdbc/strutsTutorial"
              auth="Container"
              type="javax.sql.DataSource"
              DriverClassname="org.postgresql.Driver"

              url="jdbc:postgresql://127.0.0.1:5432/strutstutorial"
              username="postgres"
              password=""
              maxActive="20"
              maxIdle="10"
              maxWait="-1"
    />

</Context>***

和我的动作类......使用struts1.1是......

 **package strutsTutorial;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.Statement;
    import javax.naming.InitialContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.sql.DataSource;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
     public class UserRegistrationAction extends Action {
    private static Log log = LogFactory.getLog(UserRegistrationAction.class);
    @SuppressWarnings("unused")
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest             
    request, HttpServletResponse response) throws Exception {

    log.trace("in execute method of UserregistrationAction.class...");
    Connection conn =null;
    InitialContext cxt = new InitialContext();  
    if(cxt == null) {
        throw new Exception("oh haaa no context");
    }
    DataSource ds = (DataSource)cxt.lookup("java:/comp/env/jdbc/strutsTutorial");
    if(ds == null) {
        throw new Exception("datasource not found");
    }
    UserRegistrationForm userForm = (UserRegistrationForm) form;

    System.out.println("entered firstname is" + userForm.getFirstName());

    if(isCancelled(request)) {
        return mapping.findForward("welcome");
    }
    else
         conn =  ds.getConnection();
        try{
            PreparedStatement stmt = conn.prepareStatement(
            "insert into USERDB " +
            "(firstName)" +
            " values (?)");
            stmt.setString(1,userForm.getFirstName());
            stmt.executeUpdate();
            }finally{
            conn.close();
            }
        return mapping.findForward("success");

    }
    }**
有人可以解释一下我哪里出错吗?

0 个答案:

没有答案