如果记录已存在于jsp页面的表中,如何获取错误消息

时间:2016-09-16 09:20:30

标签: javascript java html sql jsp

我有一个index.jsp页面,我将名称和ID插入到oracle表中。我能够成功插入,但当用户再次输入相同的员工ID index.jsp页面重定向到tomcat错误页面时说

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

当用户输入已存在于数据库中的ID时,我想在index.jsp内显示错误提示。

<%@ 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>

3 个答案:

答案 0 :(得分:1)

将代码包含在try和catch块中,当控件转到catch块处理异常并显示要显示的消息时。

答案 1 :(得分:1)

更具体地说,处理异常 SQLIntegrityConstraintViolationException 。此异常表示违反了完整性约束(外键,主键或唯一键)。

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();
   }

答案 2 :(得分:1)

使用try-catch块来处理异常。

您可以采取哪些措施防止其发生,是检查记录是否已经存在于表中。

如果存在相同的id,则不执行插入操作。

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());
}