?
Solved

inserting into an SQL database from forms

Posted on 2003-03-10
22
Medium Priority
?
245 Views
Last Modified: 2010-03-31
I am trying to insert user information into an SQL database from a html form, where reason is one of my text fields, a sample of my code is as follows:

               String prob = request.getParameter("reason");
               
          PreparedStatement ps = con.prepareStatement(
                "insert into coconn_cais3_PatientAppointments(Problem) VALUES (?)");
           

     
          ps.setString(1, prob);
         
     
          if (ps.executeUpdate() == 1)
            {
                System.out.println("Row inserted into database");
            }

However I cannot seem to grab text from field and insert it. Any help greatly appreciated
0
Comment
Question by:karms
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 3
  • +4
22 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8103770
Make sure that it's

a) not null

b) in the correct case

c) of length > 0
0
 

Author Comment

by:karms
ID: 8103814
Putting a String into the text field but its not inserting into database
0
 

Author Comment

by:karms
ID: 8103829
Putting a String into the text field but its not inserting into database
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 8103863
Are you closing your statement and connection?
0
 

Author Comment

by:karms
ID: 8103926
Yep closed both connection and statement. It seems to be an error with

out.println("<TR><TD>Reason:<TD><INPUT TYPE=\"TEXT\" NAME=\"reason\"><BR><BR>");
String problem = request.getParameter("reason");

It doesn't seem to take the text from that field at all :-/
0
 
LVL 3

Expert Comment

by:jerelw
ID: 8103981
Did you do a commit?
0
 
LVL 3

Expert Comment

by:jerelw
ID: 8103989
what is the method type on the form?

get or post?
0
 

Author Comment

by:karms
ID: 8104029
Yep closed both connection and statement. It seems to be an error with

out.println("<TR><TD>Reason:<TD><INPUT TYPE=\"TEXT\" NAME=\"reason\"><BR><BR>");
String problem = request.getParameter("reason");

It doesn't seem to take the text from that field at all :-/
0
 

Author Comment

by:karms
ID: 8104040
method type is Post....what do you mean by commit?

By The Way sorry 4 sending u the same thing twice
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8104062
>>It doesn't seem to take the text from that field at all :-/

See my comments above ;-)
0
 

Author Comment

by:karms
ID: 8104112
method type is Post....what do you mean by commit?

By The Way sorry 4 sending u the same thing twice
0
 

Author Comment

by:karms
ID: 8104133
do u mean have we entered something into the text field 'reason'??
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8104276
You have to carry out the tests on it i described above
0
 

Expert Comment

by:dcdalton
ID: 8105583
I have to ask about the PreparedStatement preperation (no pun intended) Do you have a valid connection object to the db. This is too often the case.

As far as the incoming data here's how I like to handle it:

String variableName = null;
if (req.getParameter("field_name") != null && req.getParameter("field_name").trim().length() != 0) {
  varaibleName = req.getParameter("field_name").trim();
}

This keeps the binary "boogers" as I call them off of your data as well as handling null pointer exceptions.

Use this & then test the field (as mentioned above) for != null. In other words like this:

if (variableName != null) {
System.out.println("We have data");
}
else {
System.out.println("No data here");
}

If the field is coming in null you check case & spelling, if its not you are probably picking up a "gremlin" on the field & the trim will remove it, giving you good data.

trim() is one of those functions that is ingored WAY too much but it is an absolute lifesaver!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8105998
Well, it's good of you to implement my suggestions dcdalton ;-)

Why call getParameter and trim twice? I'd do:

String variableName = null;
if ((variableName = request.getParameter("field_name")) != null && variableName.trim().length() != 0) {
   // do something
   System.out.println(variableName);
}
0
 
LVL 3

Expert Comment

by:jerelw
ID: 8111312
If the method on the form is 'post' and you do a doGet(), your parameters will be null.  If your method is 'get' and you dod a doPost(), your parameters will be null.
0
 

Assisted Solution

by:dcdalton
dcdalton earned 100 total points
ID: 8112152
Sorry CEHJ, I really didnt mean to step on your toes but I thought a code example might help this person.

As far as using a doGet - doPost, I always do everyhting in the doPost & then in the doGet (I code them both in ALL servlets) I do this:

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException     {
 doPost(req, res);
}

This way it doesnt matter which way the data is coming in, you just do the code in one spot. Ive set up my templates this way. Its one  less thing you have to worry about.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8118346
Make it:

String prob = reason.getText () ;              
PreparedStatement ps = con.prepareStatement( "insert into coconn_cais3_PatientAppointments(Problem) VALUES (?) ; ") ; // notice the semi-colon

Mayank.

         

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8544944
How about rating it now?
0
 

Expert Comment

by:CleanupPing
ID: 9058818
karms:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 35

Expert Comment

by:girionis
ID: 9693389
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Split points between CEHJ and dcdalton

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question