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 struts with jdbc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts with jdbc" Watch "struts with jdbc" New topic
Author

struts with jdbc

ashwin bala
Ranch Hand

Joined: Dec 07, 2009
Posts: 38

package package11;

import java.sql.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


public class registration11 {
String name="fail";
String result=null;
public ActionForward execute(HttpServletRequest req,HttpServletResponse res,ActionMapping mapping,ActionForm form)
{
name=getresult();
return mapping.findForward(name);
}
protected String getresult()
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");

String url = "jdbcracle:thin:@localhost:1521:XP";
String username = "system";
String password = "sys";
Connection con = DriverManager.getConnection(url, username, password);
Statement s=con.createStatement();
ResultSet rs=s.executeQuery("select name from employee");
while(rs.next())
{
result=rs.getString("name");

}
HttpSession ses=request.getSession();
ses.setAttribute("customername",result);
name="success";
}
catch(Exception e)
{}
return name;
}

}

in the above code i use getAttribute(("customername") in my jsp to get the value.. i ll be getting the result only once from the database.. but i ve more than 10 data in my "name" column ... what is the code to iterate and get all the 10 data in my jsp.. ?? please help me out... thanks in advance..
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Ashwin please Use Code Tags when you post a source code. That way your code looks formatted. Unformatted code is hard to read. You can add code tags by wrapping your code in [code] [/code] tags. You can edit your message using button and then add code tags to it.

I don't see this question has anything to do with struts (it uses struts but the question isn't about struts). You need to create an array of Strings instead of a single string result i.e. instead of String result=null;, you need String[] result=null; or better use List<String> result = new ArrayList<String>();. Assuming that you'll use ArrayList, you'll need to change your while loop to something like this

That's it, now you just have to iterate over the ArrayList in your JSP and display the elements. You can do it using JSTL forEach tag...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
ashwin bala
Ranch Hand

Joined: Dec 07, 2009
Posts: 38
ok ankit thanks a ton.. this is the first time i am posting the thread..anyways i ve used arraylist..


I am getting the Arraylist in another JSP file by using getAttribute like




i need to print the arraylist elements in this page.. please help me a code....
Aditya Keyal
Ranch Hand

Joined: Dec 01, 2008
Posts: 71
First of all using of scriptlets in a jsp is a CRIME when using Struts. Always use struts-taglib.
If you want more reference about tag-libs go here.

Second. To print the elements use the following code:



The details of how this works are available in the struts documentation mentioned above.


- Aditya Webservices Blog
ashwin bala
Ranch Hand

Joined: Dec 07, 2009
Posts: 38
Thanks a lot Mr.Aditya ... but y should not i use scriplets inside jsp when using struts..? i had to use getAttribute().. what is the problem in using it?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

ashwin bala wrote:Thanks a lot Mr.Aditya ... but y should not i use scriplets inside jsp when using struts..? i had to use getAttribute().. what is the problem in using it?

Ashwin, please UseRealWords on javaranch. Don't use abbreviations like "y" or "r" etc. Using scriptlet in JSP pages is considered very bad practice. It makes the pages mostly unmanageable and hard to maintain. Aditya showed you how you can achieve the same behavior without using scriptlet. Since you are using struts, so all your logic would be outside of your JSP, so you should not face any condition where you need to use scriptlet in your JSP...
ashwin bala
Ranch Hand

Joined: Dec 07, 2009
Posts: 38
Thanks again... Mr.ankit .Iam in learning curve of struts .Please help me with this issue as well. In this code i have retrieved only one column that is the company name



but i need to add other column that is "companyaddress"...How should i add that to arraylist..?

and how can i retrieve that in my jsp? is that same procedure?
 
 
subject: struts with jdbc
 
Similar Threads
Problem in returning the username
Eclipselink problem with MySQL Stored Procedures
problem persisting an object with collections and manytomany relationship
Using cookies in JAAS to extend a Single Sign On
Help with servlets