我有一個index.jsp
頁面,我將name和id插入到oracle表中。我能夠成功地插入,但是當用戶再次輸入相同的員工ID index.jsp
頁面重定向到tomcat錯誤頁說如何獲得錯誤消息,如果記錄已經存在於jsp頁面的表中
HTTP Status 500 javax.servlet.ServletException: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint
我想內index.jsp
顯示錯誤注意當用戶輸入ID是已經存在於數據庫。
<%@ 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>
這更多的是針對應用程序的架構實現。 **在您的應用程序中未實現驗證的典型問題**。你有沒有實施驗證? –
你應該避免表示層中的java代碼:http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files –
使用java'try .. catch ..finally'處理錯誤並確保正確的連接狀態,例如關閉它。 – Serg