如何在执行update语句时提示表列中的重复值?

时间:2011-06-21 05:28:21

标签: jsp

我需要更新一个有关比较两个差异表中两个值的查询,如果匹配则更新,如果两个表中都有重复值,也会提示我...

这是我的代码:

<%@ 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>&nbsp;</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>

此代码工作正常,如果有匹配则更新,现在我需要提示表中是否有任何重复值,如果有重复值,程序应该提示我....请任何人都可以帮助我怎么做???

提前致谢, Sailaja。

1 个答案:

答案 0 :(得分:-1)

您可以在try块结束后捕获DuplicateKeyException

赶上(DuplicateKeyException) { - 这里的代码显示重复键输入 }

快乐编码:)