Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

initialcontext lookup retun null for datasource

 
Edward Fernando
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right now im trying to connect to db from jsp, and i am using datasource...
first thing i make the Resource in context.xml like this


then i make the jsp like this...just to test the connection....

not getting any name exception then i'm assuming that binding is success....CMIIW

next i'm trying to make a Data source..with code like this....


and here comes the problem....i dont know what is wrong but the datasource alway null....where did it go wrong? please help me....
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you mean for the JNDI string "java:comp/env/jdbc/testDB" to match the resource named "jdbc/windoDB"?

I don't see any other possibility, there's only one resource there. You might want to choose a better JNDI string.
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And here's another thing:



Here you have an assignment as your if-expression. That assignment always assigns "true" to the "lookupStat" variable. And then when it's used to control the if-clause, well, it's always true so the code in that block will always be executed. I assume your statement that you weren't getting a name exception is based on the if-clause being executed, rather than actually debugging the code to see whether any exception was thrown?

If you want to compare two values (and you do want that) then use the == operator. The = operator is used for assignments.

And if you want to test a boolean expression to see if it's true, don't use either of those operators. (If you do, it's easy to choose the wrong one accidentally and put a bug into your code.) Here's what your code should be:

 
Edward Fernando
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Did you mean for the JNDI string "java:comp/env/jdbc/testDB" to match the resource named "jdbc/windoDB"?

I don't see any other possibility, there's only one resource there. You might want to choose a better JNDI string.


hmm ok CMIIW....if i not get any "javax.naming.NameNotFoundException"...then there is no problem from the lookup right ? again CMIIW....

 
Edward Fernando
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now i recreate the context the web.xml and the jsp like this

context.xml


and jsp page


javax.naming.NamingException: Cannot create resource instance........what is wrong now?.....
 
Maxim Vasilyuk
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try something, like this (not shure about full lookup path, but works for me):

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic