?
Solved

Urgent cant get update to work

Posted on 2005-04-26
10
Medium Priority
?
226 Views
Last Modified: 2012-05-05
I have a page that displays a number of fields with the patients entries in them already. he/she is giving option to update but I am getting the following error:
javax.servlet.ServletException: ORA-00001: unique constraint (W99436582.PK_TBLPATIENT) violated

Im not updating the primary key so shouldnt be affecting it. its been passed through text filed alrite. Here is code i have at moment:

<%
  String Patientid = "'" + request.getParameter("PatientID");
    String name = "'" + request.getParameter("PatientFirstname") + "'";
    String surname = "'" + request.getParameter("PatientSecondname") + "'";
     String address = "'" + request.getParameter("PatientAddress") + "'";
    String telephone =  request.getParameter("PatientTelephone");
    String mobile  =  request.getParameter("PatientMobile");
    String ssno =  request.getParameter("PatientSSNO");
     String localdoctor = "'" + request.getParameter("PatientLocalDoctor") + "'";
    String gender = "'" + request.getParameter("PatientGender") + "'";
    String age = request.getParameter("PatientAge");
      
//stmt.execute( "INSERT INTO TblPatient(PatientID,PatientFirstName,PatientSurName,PatientAddress,PatientTelephone,PatientMobile,PatientSSNO,PatientLocalDoctor,PatientGender,PatientAge)Values(" + Patientid + "," + name + "," + surname + "," + address + "," + telephone + "," + mobile + "," + ssno + "," + localdoctor + "," + gender + "," + age + ")" );

      
  Statement stmt = connection.createStatement();
  //ResultSet rs = stmt.executeQuery("UPDATE Supervisor SET Password='"+NewPassword+"' where Username = '" + session.getValue("Username") +"'");
//stmt.execute( "UPDATE  TblPatient SET Values=" + Patientid + "," + name + "," + surname + "," + address + "," + telephone + "," + mobile + "," + ssno + "," + localdoctor + "," + gender + "," + age + ")" );
 ResultSet rs = stmt.executeQuery("UPDATE TblPatient set PatientFirstName = '" & name & "', PatientSurname = '" & surname & "', PatientAddress = '" &_
address & "', PatientTelephone = '" & telephone & "', PatientMobile = '" & mobile & "', PatientSSNO = '" & ssno & "' , PatientLocalDoctor = '" & localdoctor & "', PatientGender = '" & gender & "', PatientAge = '" & age & "' WHERE PatientID = '" &  Patientid & "'"
 


  String redirectURL = "/sad/changed.jsp";
  response.sendRedirect(redirectURL);
%>

<%
connection.close();
%>
0
Comment
Question by:act1ve
  • 6
  • 4
10 Comments
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13868389
You might be entering NULLS by accident with your request parameters

Use this for your request parameter assignments to prevent it.

String bert = request.getParameter("bert")!=null ? "'"+request.getParameter("bert")+"'":"''" ;

0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13868406
but only for the Strings/VARCHARS....
0
 

Author Comment

by:act1ve
ID: 13868495
Well i changed my code to disable the patientid field. seetting session instead and going to update where session = value in database
Here is code i have:

<%
  //String Patientid = "'" + request.getParameter("PatientID");
  String user  = (String)session.getAttribute( "Name" );
   String name = "'" + request.getParameter("PatientFirstname") + "'";
    String surname = "'" + request.getParameter("PatientSecondname") + "'";
     String address = "'" + request.getParameter("PatientAddress") + "'";
    String telephone =  request.getParameter("PatientTelephone");
    String mobile  =  request.getParameter("PatientMobile");
    String ssno =  request.getParameter("PatientSSNO");
     String localdoctor = "'" + request.getParameter("PatientLocalDoctor") + "'";
    String gender = "'" + request.getParameter("PatientGender") + "'";
    String age = request.getParameter("PatientAge");
      
//stmt.execute( "INSERT INTO TblPatient(PatientID,PatientFirstName,PatientSurName,PatientAddress,PatientTelephone,PatientMobile,PatientSSNO,PatientLocalDoctor,PatientGender,PatientAge)Values(" + Patientid + "," + name + "," + surname + "," + address + "," + telephone + "," + mobile + "," + ssno + "," + localdoctor + "," + gender + "," + age + ")" );

      
  Statement stmt = connection.createStatement();
  //ResultSet rs = stmt.executeQuery("UPDATE Supervisor SET Password='"+NewPassword+"' where Username = '" + session.getValue("Username") +"'");
//stmt.execute( "UPDATE  TblPatient SET Values=" + Patientid + "," + name + "," + surname + "," + address + "," + telephone + "," + mobile + "," + ssno + "," + localdoctor + "," + gender + "," + age + ")" );
 ResultSet rs = stmt.executeQuery("UPDATE TblPatient set PatientFirstName = '" & name & "', PatientSurname = '" & surname & "', PatientAddress = '" &_
address & "', PatientTelephone = '" & telephone & "', PatientMobile = '" & mobile & "', PatientSSNO = '" & ssno & "' , PatientLocalDoctor = '" & localdoctor & "', PatientGender = '" & gender & "', PatientAge = '" & age & "' WHERE PatientID = '" &  user & "'"
 


  String redirectURL = "/sad/changed.jsp";
  response.sendRedirect(redirectURL);
%>

<%
connection.close();
%>

Getting the following mistake now:
javax.servlet.ServletException: ORA-02290: check constraint (W99436582.CHECKPATIENT) violated

Here is code i have used to create table for patient:

CREATE TABLE TblPatient (
PatientID VARCHAR2(10) NOT NULL CONSTRAINT CheckPatient CHECK(PatientID LIKE ('p%')),
         PatientFirstName VARCHAR2(15) NOT NULL,
          PatientSurName VARCHAR2(15) NOT NULL,
        PatientAddress VARCHAR2(40) NOT NULL,
PatientTelephone NUMBER(10),
PatientMobile NUMBER(10),
PatientSSNO NUMBER(10) NOT NULL CONSTRAINT Unique_PatSSNO    UNIQUE,
         PatientLocalDoctor VARCHAR2(40),
PatientGender CHAR(1) NOT NULL CONSTRAINT Check_Gender  CHECK (PatientGender IN ('M','F')),
          PatientAge NUMBER(3) NOT NULL,
          CONSTRAINT PK_TblPatient PRIMARY KEY (PatientID)
);

Will i still check for nulls?

0
Industry Leaders: 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 29

Accepted Solution

by:
bloodredsun earned 1500 total points
ID: 13868576
You really should be using a Prepared Statement for this. It would be so much easier to code and to read, and also less error prone as the jdbc driver takes care of data types.

Check that the PatientID starts with the letter "p" (i think this is the culprit) and that all the parameters are filled and are not null.

Also checkn for any foreign key violations were your db schema checks for duplicates.
0
 

Author Comment

by:act1ve
ID: 13868683
The page with the update button has text fields with the values that are currently stored about that patient, for example:
<input name="PatientID" disabled="disabled" type="text" id="ID" VALUE="<%= session.getAttribute( "Name" ) %>">

the patient id is in a session so that must be passing to the next page so cant see where it is being violated.

What do you mean a prepared statement
0
 

Author Comment

by:act1ve
ID: 13868729
PatientID is a foreign key in other tables but im not letting the primary key to be updated just the values associated with it
0
 

Author Comment

by:act1ve
ID: 13868781
Actually im real sorry im after seeing where went wrong i never changed action on previous page to update.jsp instead of register.jsp
So stupid.
But getting the following error now:

An error occurred between lines: 35 and 64 in the jsp file: /sad/updatesuccess2.jsp

Generated servlet error:
G:\Apache\work\Standalone\localhost\_\sad\updatesuccess2$jsp.java:111: ')' expected.
                 ResultSet rs = stmt.executeQuery("UPDATE TblPatient set PatientFirstName = '" & name & "', PatientSurname = '" & surname & "', PatientAddress = '" &_


ResultSet rs = stmt.executeQuery("UPDATE TblPatient set PatientFirstName = '" & name & "', PatientSurname = '" & surname & "', PatientAddress = '" &_
address & "', PatientTelephone = '" & telephone & "', PatientMobile = '" & mobile & "', PatientSSNO = '" & ssno & "' , PatientLocalDoctor = '" & localdoctor & "', PatientGender = '" & gender & "', PatientAge = '" & age & "' WHERE PatientID = '" &  user & "'"
 
0
 

Author Comment

by:act1ve
ID: 13868796
sorry again i put query all on one line and got this mistake

Generated servlet error:
G:\Apache\work\Standalone\localhost\_\sad\updatesuccess2$jsp.java:111: ')' expected.
                 ResultSet rs = stmt.executeQuery("UPDATE TblPatient set PatientFirstName = '" & name & "', PatientSurname = '" & surname & "', PatientAddress = '" & address & "', PatientTelephone = '" & telephone & "', PatientMobile = '" & mobile & "', PatientSSNO = '" & ssno & "' , PatientLocalDoctor = '" & localdoctor & "', PatientGender = '" & gender & "', PatientAge = '" & age & "' WHERE PatientID = '" &  user & "'"
                                                                                                                                                                                                                                                                                                                                                                                                                                        ^
0
 

Author Comment

by:act1ve
ID: 13869042
ill accept your answer because you did kinda help me out. its really a new question im asking
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13870289
thanks act1ve

Why do you have & rather than + in your sql String with no closing bracket and semicolon?

   ResultSet rs = stmt.executeQuery("UPDATE TblPatient set PatientFirstName = '" & name & "',............& "'"

should be
   ResultSet rs = stmt.executeQuery("UPDATE TblPatient set PatientFirstName = '" + name + "'..........+ "'");

does this help?

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

Applications for our next round of the Experts Exchange Scholarship Contest are starting to roll in. It made us wonder what our past winners are up to these days. Here's a look at what four winners experienced with the contest and what they're doing…
Tech giants such as Amazon and Google have sold Alexa and Echo to such an extent that they have become household names. And soon they are expected to be used by commoners in their homes, ordering takeout, picking out a song, answering trivia questio…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

807 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