aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes How do you connect to database(sql) from an ejb application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How do you connect to database(sql) from an ejb application" Watch "How do you connect to database(sql) from an ejb application" New topic
Author

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

Venkatesh C.R.
Greenhorn

Joined: Jul 28, 2003
Posts: 14
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.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

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().


Groovy
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Venkatesh C.R.
Greenhorn

Joined: Jul 28, 2003
Posts: 14
Thanks for your replies.
Pradeep, What exactly do you mean by connection Pooling ?
can you elaborate this with an example.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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).
Vedhas Pitkar
Ranch Hand

Joined: Jan 27, 2001
Posts: 445
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

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Jul 28, 2003
Posts: 14
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

Joined: Jul 28, 2003
Posts: 14
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Feb 27, 2002
Posts: 8919

Can you tell us which app server you are using?
Venkatesh C.R.
Greenhorn

Joined: Jul 28, 2003
Posts: 14
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

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Jul 28, 2003
Posts: 14
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

Joined: Feb 27, 2002
Posts: 8919

RI stands for the reference implementation. I have a question - Where have you placed your driver class jar file.
Venkatesh C.R.
Greenhorn

Joined: Jul 28, 2003
Posts: 14
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

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Jul 28, 2003
Posts: 14
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Jul 28, 2003
Posts: 14
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Jul 28, 2003
Posts: 14
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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...
Rangarajan Suresh
Ranch Hand

Joined: Jan 08, 2002
Posts: 50
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do you connect to database(sql) from an ejb application