Where to bind datasource (initialContextLookup ) to database

Hey!

I'm a newbie to javaee programming, but I'v done a bit in PHP/MYSQL. I've got a question.

When you want to connect to a database using the object initialContextLookup you have to specify a path like "java:comp/env/jdbc/test". Where is the folder test located or and how do you bind it to a mysql database?

Thanks.

[347 byte] By [vatora] at [2007-11-26 12:38:27]
# 1

Hi,

i came accross this topic whilst browsing. To connect database, hava a look at the following:

http://forum.java.sun.com/thread.jspa?threadID=759040&tstart=330

Also this:

http://www.javacoffeebreak.com/articles/jdbc/

Two ways to consider when connecting databases. you can connect as Jdbc/odbc, in which you set the odbc connection through your control panel within your Administrator , Datasource. OR u set the connection up within connection pool.

It is not easy with the connection pool but it is better as you don't have to set connections up everytime you install your application onto another PC. THe connection pool settings is done within the Admin console. Which means you need JDBC drivers for the type of DB you are using. Find out how to settign this up within your admin console of your J2EE or Jee server. Remember, some settings con cause problems for the server and you have to also know a bit of xml. the xml within your DOmain/domain1 : domain.xml has details of the server settings. Edit it at your own rist.

Also, when you install the JDBC drivers you need the server to point to the location of this. By default the Jee server ignores the environmental classpath settings. yOu may have to set it up within the application, JVM Paths, Paths of your application server, in the admin console. Sounds like a lot to do. but it really fun, after all this because, you get to know how to deal with issues of your application server. Anytime, something, goes wrong within, you can resolve it within the domain.xml.. But be warned, xml is very sensitive.

eve

evepokuaa at 2007-7-7 16:07:18 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Hey!

Thanks for your answer! I did as you explained. I made a connection pool and a resource using the admin and I think it works. I tried to ping it and there was no errormessages but I still can't fetch any info from the database. When I use DriverManager it works.

Works:

Class.forName( "com.mysql.jdbc.Driver" );

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=GNAAfk5#");

PreparedStatement addRecord, getRecords;

getRecords =

connection.prepareStatement("SELECT firstName, lastName FROM guests");

Does NOT work:

Context initialContext = new InitialContext();

DataSource dataSource = ( DataSource )

initialContext.lookup( "java:comp/env/jdbc/test" );

Connection conn = dataSource.getConnection();

Statement statement = conn.createStatement();

ResultSet rs = statement.executeQuery("SELECT firstName, lastName FROM guests");

I tried to find out where the log was, but it wasn't easy to find out. Do you know an easy way to track down the error?

Message was edited by:

vator

vatora at 2007-7-7 16:07:18 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Hey!

In catalina.out I found an errormessage:

javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

at org.apache.naming.NamingContext.lookup(NamingContext.java:769)

at org.apache.naming.NamingContext.lookup(NamingContext.java:139)

at org.apache.naming.NamingContext.lookup(NamingContext.java:780)

at org.apache.naming.NamingContext.lookup(NamingContext.java:139)

at org.apache.naming.NamingContext.lookup(NamingContext.java:780)

at org.apache.naming.NamingContext.lookup(NamingContext.java:152)

at org.apache.naming.SelectorContext.lookup(SelectorContext.java:136)

at javax.naming.InitialContext.lookup(InitialContext.java:351)

at coreservlets.RegisterServlet.initDB2(RegisterServlet.java:46)

at coreservlets.RegisterServlet.doGet(RegisterServlet.java:25)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)

at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)

at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)

at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)

at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)

at java.lang.Thread.run(Thread.java:595)

vatora at 2007-7-7 16:07:18 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...