Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

inserting into an SQL database from forms

Posted on 2003-03-10
22
Medium Priority
?
247 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
  • 7
  • 5
  • 3
  • +4
21 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
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!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month11 days, 23 hours left to enroll

564 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