4 Replies Latest reply: May 29, 2014 1:32 PM by jschellSomeoneStoleMyAlias RSS

    primary key validation in java

    Tshifhiwa

      hi how do you do validation in java for primary key  lets say the table got combination of two columns as primary key,how can i validate that if use enter already existing value

        • 1. Re: primary key validation in java
          rp0428
          hi how do you do validation in java for primary key  lets say the table got combination of two columns as primary key,how can i validate that if use enter already existing value

          The database will do that automatically and raise an exception when the user tries to INSERT the rows in the table.

           

          Your code should trap that exception and take appropriate action.

          • 2. Re: primary key validation in java
            Tshifhiwa

            ok so how can i trap that exception so the use can see that error or display that with meaningful message not with ORA error

            • 3. Re: primary key validation in java
              rp0428
              ok so how can i trap that exception so the use can see that error or display that with meaningful message not with ORA error

              I dont' understand your question.

               

              If you are asking how to write an exception handler see the 'Exceptions' trails in The Java Tutorials:

              http://docs.oracle.com/javase/tutorial/essential/exceptions/

              Lesson: Exceptions

              The Java programming language uses exceptions to handle errors and other exceptional events. This lesson describes when and how to use exceptions.

              Create a simple test case that does an INSERT of a duplicate value and look at the displayed stack trace to see what exception got thrown.

               

              Then create an exception handler to 'catch' that exception and substitute your own message.

              • 4. Re: primary key validation in java
                jschellSomeoneStoleMyAlias

                Steps

                - Add the integrity constraint to the table. I suggest that you explicitly name it.

                - Test the database to figure out what exception it returns for that constraint.  With luck the name you used will be in the text of the exception.

                - Add a catch to your database layer that catches SQLException, looks for the error message, and then throws a different exception, probably your own.

                - In your UI and/or webservice layer catch that specific exception and convert it to something appropriate.

                 

                The above presumes you have a basic understanding of java, try/catch, UI code, and how layers work.