0

I have an index.jsp page from where I am inserting name and id into oracle table. I am able to insert successfully, but when user enters same employee id again index.jsp page redirects to tomcat error page says

HTTP Status 500 javax.servlet.ServletException: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint

I want to display an error note within index.jsp when user enters id which is already present in database.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body><center>
  <H1>Inserting record into a Database</H1>
       <%@ page language="java" %>
        <%@ page import="java.sql.*" %>
         <%@ page import="java.sql.DriverManager.*" %>

            <% 
        int flag=0;
        PreparedStatement ps=null;
        Connection con= null;
        ResultSet rs= null;
        Class.forName("oracle.jdbc.driver.OracleDriver");
        con=DriverManager.getConnection("hostname","user","admin");
        Statement st=con.createStatement();

            String id = request.getParameter("id");  
            String name= request.getParameter("name");


            ResultSet resultset = 
                    rs=st.executeQuery("INSERT INTO employee (employeeid,employeename)VALUES ('"+id+"','"+name+""); 
            out.println("Data is successfully inserted!");

       %> 
 </center>           
</body>
</html>
Prakash Thete
  • 3,802
  • 28
  • 28
stephenjacob
  • 141
  • 2
  • 3
  • 14
  • This is more towards the architectural implementation of the application. Typical issue of **not implementing validation in your application**. have you implemented validations ? – Nikhil Nanjappa Sep 16 '16 at 09:27
  • And you should avoid java code in presentation layer : http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – Ankit Tripathi Sep 16 '16 at 09:39
  • 1
    Use java `try.. catch ..finally` to process errors and ensure proper connection state, close it for example. – Serg Sep 16 '16 at 09:45
  • 1
    Does any of the below answer solved your problem ? If yes then you can accept it so that it will be beneficial for the others having same problem. – Prakash Thete Sep 22 '16 at 08:56

3 Answers3

1

Enclose the code in try and catch block ,when the control goes to catch block handle the exception and display the message you want to display.

Ankur Saxena
  • 177
  • 1
  • 11
1

To be more specific, handle the exception SQLIntegrityConstraintViolationException. This exception indicates that an integrity constraint (foreign key, primary key or unique key) has been violated.

try{
    int flag=0;
    PreparedStatement ps=null;
    Connection con= null;
    ResultSet rs= null;
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con=DriverManager.getConnection("hostname","user","admin");
    Statement st=con.createStatement();

        String id = request.getParameter("id");  
        String name= request.getParameter("name");


        ResultSet resultset = 
                rs=st.executeQuery("INSERT INTO employee (employeeid,employeename)VALUES ('"+id+"','"+name+""); 
        out.println("Data is successfully inserted!");

   }catch(SQLIntegrityConstraintViolationException e ){
        //Duplicate entry '' for key 'PRIMARY'
        System.out.println(e.getMessage());//you can modify the output as per your requirement.
   } catch (Exception e) {
        e.printStackTrace();
   }
Rohit Gaikwad
  • 3,677
  • 3
  • 17
  • 40
1

Use the try-catch block to handle the exception.

What you can do to prevent it from coming is to check if the record is already present in table or not.

If the same id is present then do not execute the insert operation.

try{
        int flag=0;
        PreparedStatement ps=null;
        Connection con= null;
        ResultSet rs= null;
        Class.forName("oracle.jdbc.driver.OracleDriver");
        con=DriverManager.getConnection("hostname","user","admin");
        Statement st=con.createStatement();

        String id = request.getParameter("id");  
        String name= request.getParameter("name");

        // Check if the "id" is already present in table or not
        rs = st.executeQuery("SELECT * FROM employee WHERE employeeid" = id );   
        if (!rs.next() ) {

            // id is not present then insert the new record
            rs = st.executeQuery("INSERT INTO employee (employeeid,employeename)VALUES ('"+id+"','"+name+""); 
            System.out.println("Data is successfully inserted!");

        } else{

            // id is already present then show the error message
            System.out.println("Data is already present in table!");
        }

} catch(Exception e){

    // Handle any other exception occuring 
    System.out.println("Exception : " + e.printStackTrace());
}
Prakash Thete
  • 3,802
  • 28
  • 28
  • Thanks Prakash .. this was useful for me ...it worked for me.. sorry for the late response as i was out of town for the past week :). – stephenjacob Sep 23 '16 at 12:55