File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes problem in connecting struts to ejb Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "problem in connecting struts to ejb" Watch "problem in connecting struts to ejb" New topic
Author

problem in connecting struts to ejb

kolkata halder
Greenhorn

Joined: Jun 06, 2006
Posts: 8
hi
I am using netbeans 5.0 as IDE and sun application server as server.
I am using struts 1.1 framework.plz help me giving some source code to connect oracle database from action class.i am stucked in action class from there how to connect with database and what are the things i need to do.
SSSSjava stud
Greenhorn

Joined: Jun 06, 2006
Posts: 3
public static Connection getConnection()
{
Connection con = null;
try
{
sUrl = "jdbc racle:thin:@192.168.1.2:1521:HELLO";
sUser = "sys";
sPassword = "sys";
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
con = DriverManager.getConnection(sUrl,sUser,sPassword);
}
catch(Exception e)
{
System.out.println("EXCEPTION IN GETTING CONNECTION :"+e);
}
return con;
}
kolkata halder
Greenhorn

Joined: Jun 06, 2006
Posts: 8
hi ssss,

thx for yr reply.this code i used when i was using model 1 arch. i placed this code in servlet . that's fine . but i m following mvc with struts now. in this where to put connection code.

in view part i used jsp ---> then actionform-- to send data to action class.my doubt is whether to put connection code in action class or from action class i have to call some bean class which will be having connection code.

plz send some codes or links where can i get info.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The Action class is a controller object, which means that it should only contain code to connect the view with the model. Generally, code that accesses a database would be considered "model" code. Therefore, it's not a good practice to put any sort of database access code in your Action class.

My suggestion would be to follow the Data Access Object (DAO) pattern in creating your data access classes.

Also, it's much better to use your application server's connection pooling for database connections, rather than creating them yourself.


Merrill
Consultant, Sima Solutions
kolkata halder
Greenhorn

Joined: Jun 06, 2006
Posts: 8
then controler part ie. action class will not contain any business logic only the code to connect view and model. so plz tell me or give me some code for what should be in action class and then for model part to use connection pool.

infact to understand whole cycle starting from getting username from view part then through action class goes to database and checks it correct or not, then coming back to action class then again through view it is show user successful or failure.

please give some code to understand full cycle.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Here's a possible design scenario:

Class: User

Represents a user of the system. Has userId and possibly many other attributes

Class: DataAccessObject

This is an abstract class. It's main feature is a getConnection() method which does a JNDI lookup on the data source and then uses it to get a JDBC Connection object.

Class: UserDAO extends DataAccessObject

Has a getValidUser(String userId, String password) method which throws InvalidUser and InvalidPassword exceptions and returns a User object. This method uses the superclass's getConnection method to get a JDBC connection. It then uses the connection to query the database to see if there is a user with the given userId and password. If there is, it returns the User object. If there isn't, it throws an exception.

Class: LoginForm extends ActionForm

The ActionForm used by the Login action. Has a userId and a password property that are populated by Struts

Class: LoginAction extends Action

In the execute() method, retrieve the userid and password from the LoginForm object passed in as a parameter. Instantiate a UserDAO object and call its getValidUser() method, passing the UserId and password as parameters. Do the above in a try/catch block and if exceptions are thrown, add new messages to the ActionMessages object and forward to the Login page. If no exceptions are thrown, put the User object in the HttpSession and forward to the welcome page.

In more complex interactions, you may want another layer between the Action class and the DAO known as a Business Delegate. For example, suppose your business has rules for how complex a password has to be. When a user wants to change a password, you would have a Business Delegate object such as RegistrationDelegate with a changePassword method. You would put the logic to check password complexity in this method, and then call the changePassword method on the UserDAO to persist the new password to the database.

If you don't know how to set up or use a connection pool, I'd suggest looking in the documentation for the Sun Application Server that you're using. It will tell you how to do it for that specific App Server. This link may get you pointed in the right direction.
[ June 08, 2006: Message edited by: Merrill Higginson ]
kolkata halder
Greenhorn

Joined: Jun 06, 2006
Posts: 8
thx.
scenario is quite clear , how it works.
if you can plz send me some code for the whole cycle you told.

I am using netbeans 5.0, j2ee sdk ,sun app server.
(I can only use free softwares)
if i can run atleast a login operation then i shall be confident.
and further I shall try to do something good.so please help me giving some code.
kolkata halder
Greenhorn

Joined: Jun 06, 2006
Posts: 8
I am still searching for some code for simple login application with struts+DAO or struts+ejb or struts+jdbc.
please help me by giving some link or code to do it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

"chi@java" and "SSSSjava stud",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
This link has a number of Struts sample projects that you can download. I think you will find beer4all useful because it uses the DAO pattern.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in connecting struts to ejb
 
Similar Threads
Please help!
Struts 2 and JDBC
jdbc not bound
Applet
how to connect struts1.1 with database ?