• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in connecting struts to ejb

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
kolkata halder
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic