I need to update a query on comparing two values in two diff tables, if there is a match then update and also prompt me if there is any duplicate values in both the tables…
here is my code:
<%@ page import="java.sql.*" %>
<% Class.forName("oracle.jdbc.driver.OracleDriver");%>
<HTML>
<HEAD>
<script type="text/javascript" align="right">
document.write("<p>" + Date() + "</p>");
</script>
</head>
<BODY bgcolor="#99CCFF">
<p> </p>
<br>
<%
// ResultSet resultset;
String connectionURL = "jdbc:oracle:thin:@localhost:1521:xe";
String driver = "oracle.jdbc.driver.OracleDriver";
String user = "hr";
String pass = "hr";
String value1 = request.getParameter("value1");
String value2 = request.getParameter("value2");
String value3 = request.getParameter("value3");
String value4 = request.getParameter("value4");
Connection connection = null;
PreparedStatement pstatement = null;
try {
Class.forName(driver).newInstance();
connection = DriverManager.getConnection(connectionURL, user, pass);
pstatement = connection.prepareStatement("update Table1 set value1 ='"+ value1+"',value2='"+value2+"',value3 = '"+value3+"',value4 = '"+value4+"' where SerialNo in(select DB.SerialNo from Table1 DB, Table2 OUT where OUT.SerialNo = DB.SerialNo and DB.Transaction_Status = 'IN')");
pstatement.executeUpdate();
%>
<br><br>
<TABLE align="center" style="background-color: #efefef;"WIDTH="30%" border="1">
<tr><th>Order Issued, Database Updated Successfully</th></tr>
</TABLE>
<br><br>
<%
pstatement.close();
}
catch(ClassNotFoundException e){
out.println("Couldn't load database driver: " + e.getMessage());
}
catch(SQLException e){
out.println("SQLException caught: " + e.getMessage());
}
catch (Exception e){
out.println("AnyException: " +e.getMessage());
}
finally {
// Always close the database connection.
try {
if (connection != null) connection.close();
}
catch (SQLException ignored){
out.println(ignored);
}
}
%>
</BODY>
</HTML>
This code is working fine, It is updating if there is match, Now I need to prompt if there is any duplicate values in tables, The Program should prompt me if there a duplicates values….Please can anyone help me how to do this???
Thanks in advance,
Sailaja.
You can catch the DuplicateKeyException after the end of the try block
catch(DuplicateKeyException)
{
–Your code here to display for duplicate key entry
}
Happy coding 🙂