• 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

struts with jdbc

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 = "jdbc:oracle: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..
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
ashwin bala
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ashwin bala
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic