Javascript 如果jsp页面的表中已经存在记录,如何获取错误消息
我有一个Javascript 如果jsp页面的表中已经存在记录,如何获取错误消息,javascript,java,html,sql,jsp,Javascript,Java,Html,Sql,Jsp,我有一个index.jsp页面,从中我将名称和id插入到oracle表中。我能够成功插入,但当用户再次输入相同的员工id时,index.jsp页面将重定向到tomcat错误页面 HTTP Status 500 javax.servlet.ServletException:java.sql.SQLIntegrityConstraintViolationException:ORA-00001:唯一约束 当用户输入数据库中已经存在的id时,我想在index.jsp中显示一个错误注释 <%@ pa
index.jsp
页面,从中我将名称和id插入到oracle表中。我能够成功插入,但当用户再次输入相同的员工id时,index.jsp
页面将重定向到tomcat错误页面
HTTP Status 500 javax.servlet.ServletException:java.sql.SQLIntegrityConstraintViolationException:ORA-00001:唯一约束
当用户输入数据库中已经存在的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>
在此处插入标题
将记录插入数据库
当控件转到catch块处理异常并显示要显示的消息时,将代码括在try-and-catch块中。当控件转到catch块处理异常并显示要显示的消息时,将代码括在try-and-catch块中。更具体地说,处理异常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();
}
更具体地说,处理异常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();
}
使用
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());
}
使用
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());
}
这更倾向于应用程序的体系结构实现。未在应用程序中实现验证的典型问题。您是否实现了验证?您应该避免在表示层使用java代码:使用java
try。。catch..finally
要处理错误并确保连接状态正确,请关闭它。下面的任何答案是否解决了您的问题?如果是的话,那么你可以接受它,这样对其他有同样问题的人会有好处。这更倾向于应用程序的架构实现。未在应用程序中实现验证的典型问题。您是否实现了验证?您应该避免在表示层使用java代码:使用javatry。。catch..finally
要处理错误并确保连接状态正确,请关闭它。下面的任何答案是否解决了您的问题?如果是,那么你可以接受它,这样对其他有同样问题的人会有好处。谢谢Prakash。。这对我很有用…它对我有用。。很抱歉,由于我过去一周不在城里,所以回复太晚:)。谢谢Prakash。。这对我很有用…它对我有用。。很抱歉,由于过去一周我不在城里,所以回复太晚:)。谢谢Rohit。。。我试着使用这个异常处理方法,它奏效了…谢谢你。。。我试着使用这个异常处理方法,它奏效了。。