aspose file tools*
The moose likes Struts and the fly likes Database Connection with Struts2.0.9 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Database Connection with Struts2.0.9" Watch "Database Connection with Struts2.0.9" New topic
Author

Database Connection with Struts2.0.9

rahuk Soni
Greenhorn

Joined: Oct 11, 2007
Posts: 10
Hi,
All

I am new to struts and I have to generate the table dynamically from the database. but i do not know how to do that for the struts 2.0.9. I know the database connection and all other stuffs related to the database. but how to do that for the struts that i do not know so please help me for that.

Tools I am using :
1 --> Struts 2.0.9
2 --> Database: Sql server 2000
3 --> Tomcat 5.5.23

Regards,
Rahul
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Since data access is part of the model in a model/view/controller pattern, just code all your data access objects with no dependency on Struts. Then in your Struts Action classes you can simply instantiate and call methods on these data access objects (DAOs) to either retrieve or save data to a databasea.
[ October 11, 2007: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
rahuk Soni
Greenhorn

Joined: Oct 11, 2007
Posts: 10
Hi, Merrill Higginson
First of All thanks for your reply.

I don't know how to do that with struts so can you please if you have the code how to do that for dynamically generate the HTML output from the database table than share with me.

I really don't know how to iterate through the rs.next() loop and display one by one data on the result jsp page.

Regards,
Rahul.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You will want to avoid using a ResultSet object in a JSP. The way to do it is to translate the data from the Database into JavaBeans and Collections, and then in your Struts JSP, simply use the JavaBeans. The Struts custom tags were designed to operate on JavaBeans, rather that ResultSet objects. Here's an example of a model class that retrieves a list of order items:

You would then put code in your Struts Action to instantiate the OrderItemDAO object and call its getOrderItemsByOrderNumber method. You would then place the resulting list of OrderItems in a property of the Action class. You could then use the Struts custom tags and EL expressions to display the list of OrderItems in a JSP. If you don't know how to do this, I'd suggest spending some time working through some Struts 2 tutorials before trying to write your own application.
rahuk Soni
Greenhorn

Joined: Oct 11, 2007
Posts: 10
HI, Merrill Higginson

Can you give me the link where can I find this example of OrderItemDAO for the database operation using struts-2.0.9 or How to develop the SimpleBean Application for the struts.
what the examples till U have found are of the examples of the Previous versions of the struts but I can not able to found and database example for the struts 2.0.9 so if you have any of that please suggest me the link for that.

Regards,
Rahul
rahuk Soni
Greenhorn

Joined: Oct 11, 2007
Posts: 10
Hi

I have also question about the struts2.0.9 and the database connection. I have created the class that get the data from the database means BeanClass of bean that contains the all the variables and getter and setter methods described below:
public class Person {
private String name;
private String address;
private String phone;
// getters and setters for the above properties
}

and then DBClass that query in the database that is as follow:

List listOfPeople = new ArrayList();

// get the database connection, create an SQL statement, and execute it to get a ResultSet rs

while (rs.next()) {
Person person = new Person();
person.setName(rs.getString("NAME"));
person.setAddress(rs.getString("ADDRESS"));
person.setPhone(rs.getString("PHONE"));
listOfPeople.add(person);
}


and then I am creating the object of the database class that returns the list and now I have to display the data from that list my ActionClass is as follow:

public class DBDisplay extends ActionSupport{
private String name ;
List list = null;

public String execute() throws Exception
{
list = DBClass.getInstance().list();
Iterator it = list.iterator();
while(it.hasNext())
{
name = (String) it.next();
}
setName(name);
}
public String getname()
{
return name;
}
public void setName(String name)
{
this.name = name;
}

}


I dont know there is changes from the struts 1.x to 2.x
I have read the article and according to that make the changes
the article is:

http://www.javaworld.com/javaworld/jw-10-2007/jw-10-adoptingstruts2.html?page=5

and then I have the jsp page that display the contain:

<s roperty value="name"/>

but it does not display the contents i dont know how to do that for the struts 2.0.9. I know that we have <html:bean > and <logic:iterate> class for the doing same thing in the struts1.x but how can i do that for the struts 2.0.9. Please Help me to found the solution. If I have done something wrong then also point out that and how can I do that to get the solution. I got the error is : classcastException of that object but i have to display the string something wrong with my code but i dont know what is wrong about that.


Regards,
Rahul
rahuk Soni
Greenhorn

Joined: Oct 11, 2007
Posts: 10
Hi,Merrill Higginson

Sorry for that i think after posting the issue that previous thread is not the proper place so I have again post the same question in my thread. sorry for that.

Regards,
Rahul
Y Jadhav
Ranch Hand

Joined: Oct 02, 2008
Posts: 37
rahuk Soni wrote:Hi,
All

I am new to struts and I have to generate the table dynamically from the database. but i do not know how to do that for the struts 2.0.9. I know the database connection and all other stuffs related to the database. but how to do that for the struts that i do not know so please help me for that.

Tools I am using :
1 --> Struts 2.0.9
2 --> Database: Sql server 2000
3 --> Tomcat 5.5.23

Regards,
Rahul



Hello Rahul, I am also in same Boat. I am new to S2. and facing same problem of dynamically retrieving data and display it in jsp with tabular format.
I think you found solution for it . Please help me to get out of this problem.
Thanks!.


Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Please don't wake up old threads. This thread is a year and a half old and I don't think Rahuk is going to be coming back to it.
As Merrill mentioned in the second post, connecting to a database is the responsibility of the model, and therefore doesn't fall under the responsibility of Struts. The typical pattern follows Merrill's suggestion of using a DAO (Data Access Object).
Try searching this forum and you'll find hundreds of examples.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

pamela dsouza,
Your post was moved to a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database Connection with Struts2.0.9