• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP coding  RSS feed

 
phillip atienza
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir please help me...

how can I insert the four fileds in my page to the database then show them below on the same page?


String companyid = request.getParameter("companyid");
String companyname = request.getParameter("companyname");
String upsplan = request.getParameter("upsplan");
String location = request.getParameter("location");
String mainoffice = request.getParameter("mainoffice");

String query="";
ResultSet rs=null;

boolean flag=true;


if (companyid != null)
{

if(companyid.trim().equals(""))
{
out.println("<li>Company ID field should not be blank!</li>");
flag=false;
}
if(companyname.trim().equals(""))
{
out.println("<li>Company Name field should not be blank!</li>");
flag=false;
}
if(upsplan.trim().equals(""))
{
out.println("<li>UPS Plan Number field should not be blank!</li>");
flag=false;
}
if(location.trim().equals(""))
{
out.println("<li>Location field should not be blank!</li>");
flag=false;
}


if(mainoffice.trim().equals(""))
{
out.println("<li>Location field should not be blank!</li>");
flag=false;
}



if(flag)

try
{
query ="insert into cmr_ups.dbo.[company] (company_id, company_name, ups_plan_number, company_location) values ('"+companyid.trim()+"','"+companyname.trim()+"','"+upsplan.trim()+"','"+location.trim()+"')";


statement.executeUpdate(query);

out.println("<li>Company successfully Added!</li>");

}
catch(Exception e)
{
out.println(e);
}


}
[ May 18, 2006: Message edited by: Bear Bibeault ]
 
Sri Gnana
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First create connection to the database.
My suggestions:-
1. try to avoid validation in the serverside(if its required then you have to do) and do it in clientside using javascript.
2. do trim() kind of things before you insert the data's into the DB.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
phillip,
Welcome to JavaRanch!

In an effort to help you get the most from our forums, we've compiled a
list of tips for asking questions here. You can find the list in our
FAQ section here. In particular, please see EaseUp.
Typing with the cap lock key down is the equivalent to yelling in web forums and is considered to be quite rude.

Also, when posting more than a line or two of your code, it is a good idea to wrap the code in a set of UBB code tags. Doing so preserves your code's indenting; making it a lot easier to read. If it's easier to read, more people are likely to read it and you stand a better chance of getting help.

You can fix your subject line by clicking on the link in the top post of this thread.

Thank you.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have adjusted the title to remove the all uppercase for you.
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sri Gnana:
First create connection to the database.
My suggestions:-
1. try to avoid validation in the serverside(if its required then you have to do) and do it in clientside using javascript.
2. do trim() kind of things before you insert the data's into the DB.


1) NEVER rely solely on clientside anything, it's unreliable and invites trouble.
2) could be good, could be bad, depending on the data. Maybe the whitespace must be preserved, maybe not.

Addendum to 1):
relying purely on clientside validation causes major problems.
It allows clients to send data that's deliberately wrong and get it inserted into your business systems anyway.
This has in the past taken the form (there are documented cases) in which people have fooled online stores into sending them items without payment.
They figured out that there was no validation of the prices and numbers ordered in the online store, and just sent data with all prices set to zero using tweaked web pages.
They were often only found out a lot later, and in several cases courts have afterwards decided that the person comitting this fraud is NOT guilty of anything.
And that's a minor case. In more severe cases omitting serverside validation can cause major security leaks in that it could allow crackers to execute any code they wish through your database statements.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sri Gnana:

1. try to avoid validation in the serverside(if its required then you have to do) and do it in clientside using javascript.


Re-inforcing Jeroen's post: never avoid validation on the server side. Always assume that any client-side validation that you perform in JavaScript has not been executed. Client-side code is easy to circumvent and just as easy to spoof. Never ever trust anything coming at you from the client.
 
naveen gupta
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
philip, the answer for your question is

basically your code goes inside the Form tag like

<form action=" " method=post>

so before you write the form tag, write your scriptlet code, where you will do the validation saying if you successfully inserted the code then display else do your database operation

and in your form tag, do not specify the action attribute which will refereshes/redirects your action to the same page when you click your submit button

something like

<%
if not yet inserted into database
{
%>
<form action="" method=post>
.....
....
</form>
<%
}
else
{
%>

<form action="" method=post>

display your results

</form>



<%
}
%>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"thgisisnaveen", you have previously been warned on one or more occasions regarding adjusting your display name to meet JavaRanch standards. This is not optional. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it prior to your next post.

Be aware that accounts with invalid display names are removed.

bear
JavaRanch Sheriff
 
naveen gupta
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didn't check my previous posting after i replied, i changed it now

thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"naveen g", please actually read my replies.

Your display name must be a first and a last name separated by a space character, and must not be obviously fictitious. An initial for your last name is not acceptable.

bear
JavaRanch sheriff
 
phillip atienza
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to all of you..

my problem now is this how can I edit my first list using the firsat column as a link that it goes back to the textfield?
 
naveen gupta
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didn't understand your question
 
phillip atienza
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a page for adding company id name and location. i have a submit button that submits to the database and list it down below on the same page.

if the detail is on the list, how can i make the company id editable that it goes back to the textfields?
 
phillip atienza
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks bear and ben
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!