This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How do you connect to database(sql) from an ejb application

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you connect to an sql database from an ejb application.I wrote the normal connection code(Class.forName(....)...) in my ejb class.But it is not connecting.It gives "SQLException : driver not found" error?
Is it so the from an ejb application you can only connect to cloudscape database and not to other databases.
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is possible to connect to any database from EJB for which you have JDBC drivers.
Looks like the container is not able to find the driver in the classpath. Please check you container documentation on how to place the driver in the classpath.
It is recommended that you use connection pooling rather than call Class.forName().
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shouldn't use Class.forName(...) to register JDBC drivers when doing J2EE development. Your EJB should lookup a DataSource reference for your database connection using JNDI and configure the DataSource using your application server's administration tools.
 
Venkatesh C.R.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies.
Pradeep, What exactly do you mean by connection Pooling ?
can you elaborate this with an example.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pradeep, What exactly do you mean by connection Pooling ?
can you elaborate this with an example.


EJBs can benefit from connection pooling by using DataSource references through JNDI as I mentioned earlier. Take a look at here for an example (look for "DataSource" in the code example).
 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Is it connection pooling posssible through a DataSource only?Doest JDBC2.0 support connection pooling?And if i dont want to do a lookup of the DataSource as Naming.lookup("Example"),how to obtain a DataSource without lookup?
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess it should be possible by knmowing the ConnectionPoolDataSource implementation class. I have never tried it though. Any one who feels that I am wrong?
 
Venkatesh C.R.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I took a look on your "EJB Tutorial site".Thank you for that.But is the connection sequence same for a session bean.Then what in case of a session bean the difference in using Class.forName(..) type connection and DataSource type connection.
C.R.Venkatesh
 
Venkatesh C.R.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got a new error in my application.
It is saying "SQLException : Driver not found".this is in my ejb class.
My coding for the connection is via a datasource.
while deploying I have mentioned the JNDI name in resourse references.
I have even edited the default.properties file.
can you help...
[ August 07, 2003: Message edited by: Venkatesh C.R. ]
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I got a new error in my application.
It is saying "SQLException : Driver not found".


Is the JDBC driver's .jar file in your appserver's classpath?
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Then what in case of a session bean the difference in using Class.forName(..) type connection and DataSource type connection.


The procedure for locating a database connection is exactly the same for session beans as it is for entity beans (or message-driven beans).
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you tell us which app server you are using?
 
Venkatesh C.R.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pradeep Bhat:
Can you tell us which app server you are using?


Let me explain the whole scenario.
I have an ejb application consisting of (remote Interface, home interface,Ejb class extending from SessionBean).I complied and deployed them on the j2ee web server.I use a client application(developed in java) which uses this component using the Lookup method..
hope that you are getting the picture..
C.R.Venkatesh
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understood you problem. The reason I asked abt the app server is because you are facing "Driver not found". Is it J2EE RI from Sun?
 
Venkatesh C.R.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pradeep Bhat:
I understood you problem. The reason I asked abt the app server is because you are facing "Driver not found". Is it J2EE RI from Sun?


sorry,I did not get what you are saying "j2ee 'RI' from sun"?
any way if you are asking about the web server. It was embedded in the j2sdkee1.2.1 software kit.
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RI stands for the reference implementation. I have a question - Where have you placed your driver class jar file.
 
Venkatesh C.R.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pradeep Bhat:
RI stands for the reference implementation. I have a question - Where have you placed your driver class jar file.


well ,
till now ,I was searching for my driver jar files in my sdk's folders.But without much success.Anyway , I have run other j2ee application using database connectivity.that time it did not give any problem.the connectivity was established.But now from the ejb component it is saying driver not found.but anyway i have configured "jdbc.drivers"property in my default.properties file in my <java 2 enterprise edition kit>'s folder.
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is you driver class files are not in the classpath. The j2ee server from Sun comes with only cloudscape driver. I guess you can use the JDBC-ODBC (type 1) driver. It comes along with the JDK.
 
Venkatesh C.R.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pradeep Bhat:
The problem is you driver class files are not in the classpath. The j2ee server from Sun comes with only cloudscape driver. I guess you can use the JDBC-ODBC (type 1) driver. It comes along with the JDK.


well , how should i do it.I do have jdk kit installed.how would i make j2ee server use it or locate it.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Venkatesh, which database you're using? The Cloudscape that comes with J2EE RI? If so, then the driver should already be configured according to the documentation (<J2EE_HOME>/doc/release/ConfigGuide.html#12442).
 
Venkatesh C.R.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lasse Koskela:
Venkatesh, which database you're using? The Cloudscape that comes with J2EE RI? If so, then the driver should already be configured according to the documentation (<J2EE_HOME>/doc/release/ConfigGuide.html#12442).


Lasse , I am using Sql Server 2000 .
I hope that for an sql connection we use "Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");".anyway what does that mean.From this statement can i locate my jdbc driver.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm. If you're using the Sun JDBC-ODBC bridge, you shouldn't need any configuration either according to Sun's documentation.
Maybe you should check that you have JdbcOdbc.dll under your JRE directory?
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, if you just can't get the JDBC-ODBC bridge working, you should consider trying Microsoft's JDBC driver for SQL Server 2000.
 
Venkatesh C.R.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lasse Koskela:
Hmm. If you're using the Sun JDBC-ODBC bridge, you shouldn't need any configuration either according to Sun's documentation.
Maybe you should check that you have JdbcOdbc.dll under your JRE directory?


yes,I do have that in the jre directory.
You did say of mapping the datasource with deploytool.
well,i provided a jndi name "jdbc/sqlServer" in the resource references.
I have used the same datasource name in the code "java:comp/env/jdbc/Manifestation"
Then in the default.properties file i have mentioned "jdbc.datasources = jdbc/sqlServer|jdbc dbc:Manifestation"
then why am i getting this error : java.sql.SQLException : Driver Not Found
Waiting for your Reply.............
[ August 07, 2003: Message edited by: Venkatesh C.R. ]
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You did say of mapping the datasource with deploytool.
well,i provided a jndi name "jdbc/sqlServer" in the resource references.
I have used the same datasource name in the code "java:comp/env/jdbc/Manifestation"
Then in the default.properties file i have mentioned "jdbc.datasources = jdbc/sqlServer|jdbc dbc:Manifestation"
then why am i getting this error : java.sql.SQLException : Driver Not Found
Waiting for your Reply.............


Just to be sure, which version of the J2EE SDK/RI are you using?
I have J2EE SDK 1.3.1 and its JDBC drivers configured in resource.properties like this:

To get the SQL Server JDBC driver configured, I'd add something like this:

...and ofcourse add mssqlserver.jar to userconfig.bat...
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Venkatesh,
A humble suggestion.
Pls. get yourself acquinted with the concept/theory of what you are doing and you will be able to solve most of these problems yourself.
That will save your time too.
For starters you can take these courses:
JDBC Tutorial
J2EE
Happy Learning
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic