You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by log2akshat <ak...@iiitmk.ac.in> on 2011/07/08 23:25:52 UTC

How to display data from database to JSP Page in Struts2

I am new to Struts and want to display the Registration ID which is
generating automatically by the mysql to my success page when user submits
the form, please guide me how to achieve it...

My Action...

package net.Candidate.application.action;

import java.io.*;
import java.sql.*;
import java.util.*;

import javax.naming.*;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;

import net.database.*;
import net.Candidate.application.model.Apply;
import net.Candidate.application.model.Appform;

public class SubmitAppFormAction extends ActionSupport {
	
	private Appform appform;
	private int onlineID = 0;
	
	public String add() {
		try 
		{
			Connection connect = null;
			ResultSet result = null;
			PreparedStatement pstmt = null;
			
			DBConnection getConnect = new DBConnection();
			connect = getConnect.getCon();
			System.out.println("JDBC MySQL Connection....");
				
				String query = "INSERT INTO applicant
VALUES(NULL,?,?,?,?,?,?.........NOW())";
				pstmt = connect.prepareStatement(query);
				pstmt.setString(1, appform.getName());
				pstmt.setString(2, appform.getSurname());
				pstmt.setString(3, appform.getDOB());
				pstmt.setString(4, appform.getGender());
				pstmt.setString(5, appform.getNationality());
                                pstmt.setString(6, appform.getEmail());	
                                ........................
                                ........................					
				pstmt.executeUpdate();
				
				*int OnlineID = 0;*
				ResultSet result_ID = null;
			
				String IDqry = "Select OnlineID from applicant where Email=?";
				pstmt = connect.prepareStatement(IDqry);
				pstmt.setString(1, appform.getEmail());
				result_ID = pstmt.executeQuery();
				{
				        while (result_ID.next())
				        {
				            OnlineID = result_ID.getInt(1);    
				        }
				 }
					
				String Securityquery = "INSERT INTO security
VALUES("+OnlineID+",?,?,?,?)";
				pstmt = connect.prepareStatement(Securityquery);
				pstmt.setString(1, appform.getEmail());
				pstmt.setString(2, appform.getReferee1Email());
				pstmt.setString(3, appform.getReferee2Email());
				pstmt.setString(4, appform.getPasswd());
				pstmt.executeUpdate();												 
                                *
				setOnlineID(this.onlineID);
				return "success";*
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	*public int getOnlineID() {
		return onlineID;
	}
	
	public void setOnlineID(int onlineID) {
		//Implementation of the application specific logic to retrieve the data
from the database//
			try 
			{
			Connection connect = null;
			PreparedStatement pstmt = null;
			DBConnection getConnect = new DBConnection();
			connect = getConnect.getCon();
			
			int OnlineID = 0;
			ResultSet result_OID = null;
			String OIDqry = "Select OnlineID from applicant where Email=?";
					pstmt = connect.prepareStatement(OIDqry);
			pstmt.setString(1, appform.getEmail());
			result_OID = pstmt.executeQuery();
			{
			        while (result_OID.next())
			        {
			            OnlineID = result_OID.getInt(1);
			        }
			    }
			}
			  
		 catch (SQLException e) {
			e.printStackTrace();
		 }	
		}*
	
	public Appform getAppform() {
		return appform;
	}	
	public void setAppform(Appform appform) {
		this.appform = appform;
	}
	
}

I TRIED BY THE METHOD HIGHLIGHTED IN BOLD:

I want to display the OnlineID which is generated on my JSP page, but it is
showing OnlineID as 0.

My JSP:

<%@ taglib prefix="s" uri="/struts-tags" %>

<div id="heading">
<s:text name="title_applysuccess"/>
</div>

<div id="index">	
<s:text name="apply_Success"/>

<s:text name="apply_SuccessID"/><s:property
value="%{onlineID}"></s:property>
</div>

Please help........

--
View this message in context: http://struts.1045723.n5.nabble.com/How-to-display-data-from-database-to-JSP-Page-in-Struts2-tp4566721p4566721.html
Sent from the Struts - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: How to display data from database to JSP Page in Struts2

Posted by Dale Newfield <da...@newfield.org>.
On 7/10/11 9:36 PM, Steven Yang wrote:
> some hints about Java naming convention
> 1. package names are all lower cases

They're actually camelCase after the first word.  Typically people 
figure out ways to keep each dotted component to one word to avoid that, 
though.

-Dale

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: How to display data from database to JSP Page in Struts2

Posted by Steven Yang <ke...@gmail.com>.
some hints about Java naming convention
1. package names are all lower cases
2. field/properties/variables begin with lower case letter, only
class/interface...etc. begin with upper case letter

yes you should make a separate class for retrieving data.

Action is for preparing data for your view(jsp) to use.

On Sat, Jul 9, 2011 at 8:32 PM, log2akshat <ak...@iiitmk.ac.in> wrote:

> Thanks a lot Dave for your really helpful suggestions,
>
> I was doing something rubbish by retrieving the OnlineID more than once,
> now
> I have rectified my code as ...
>
> public int getOnlineID() {
>                return OnlineID;
>        }
>
>        public void setOnlineID(int OnlineID) {
>                this.OnlineID = OnlineID;
>                }
>
> and declared OnlineID in the starting as : private int OnlineID;
>
> and removed the extra Variable from the Code and the Code is working.
>
> and yes, I am making Email as Unique ID at DB level as well as before user
> enters the form he will be validated that he has not already applied
> before.
>
> >> package net.Candidate.application.action;
>
> >Again, I would strongly urge you to follow normal Java naming conventions
>
> This is the directory structure where where my Action class is sitting, is
> that something wrong here....
>
> and shall I make another class file where DB transactions should be
> executed?
>
> --
> View this message in context:
> http://struts.1045723.n5.nabble.com/How-to-display-data-from-database-to-JSP-Page-in-Struts2-tp4566721p4568007.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: How to display data from database to JSP Page in Struts2

Posted by log2akshat <ak...@iiitmk.ac.in>.
Thanks a lot Dave for your really helpful suggestions,

I was doing something rubbish by retrieving the OnlineID more than once, now
I have rectified my code as ...

public int getOnlineID() {
		return OnlineID;
	}
	
	public void setOnlineID(int OnlineID) {
		this.OnlineID = OnlineID;
		}

and declared OnlineID in the starting as : private int OnlineID;

and removed the extra Variable from the Code and the Code is working.

and yes, I am making Email as Unique ID at DB level as well as before user
enters the form he will be validated that he has not already applied before.

>> package net.Candidate.application.action; 

>Again, I would strongly urge you to follow normal Java naming conventions 

This is the directory structure where where my Action class is sitting, is
that something wrong here....

and shall I make another class file where DB transactions should be
executed?

--
View this message in context: http://struts.1045723.n5.nabble.com/How-to-display-data-from-database-to-JSP-Page-in-Struts2-tp4566721p4568007.html
Sent from the Struts - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: How to display data from database to JSP Page in Struts2

Posted by Dave Newton <da...@gmail.com>.
On Fri, Jul 8, 2011 at 4:25 PM, log2akshat wrote:
> package net.Candidate.application.action;

Again, I would strongly urge you to follow normal Java naming conventions

> import java.io.*;
> import java.sql.*;
> import java.util.*;

And again, I would strongly urge you to *not* do all this database
work in the action itself.

> import net.Candidate.application.model.Appform;

If you're using a form object (or "command object" in some frameworks,
akin to the old Struts 1 ActionForm) I'd consider using the
ModelDriven approach.

>        private int onlineID = 0;
> [...]
>                                *int OnlineID = 0;*

You are creating an additional variable, "OnlineID", whereas your
getter method returns the class property "onlineID". This is very
confusing, and at least partially your problem.

>                                String IDqry = "Select OnlineID from applicant where Email=?";

Note that you may have a race condition here if someone else registers
with the same email, unless you're handling your transactions through
an interceptor somehow, or have DB-level constraints.

>                                setOnlineID(this.onlineID);

Here you pass the class property (0, as initialized) into a method
that does nothing with it.

>                                return "success";

Why do you use "success" here, but:

>                return SUCCESS;

SUCCESS here? Be consistent.

>        public void setOnlineID(int onlineID) {

Why does a method called "setOnlineID" retrieve the ID? Doesn't that
seem backwards to you? Plus I'm not really sure why you need it, since
you have already retrieved the value in the action code above.

Please, *please* refactor your code, clean it up, and pay very close
attention to what the code you're writing is actually doing.

Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org