可以用try catch机制,主键重复会有相应的exception

解决方案 »

  1.   

    try {
              getDBConnection();
              stmt = dbConnection.prepareStatement(queryStr);
              resultCount = stmt.executeUpdate();
    System.out.println(" dbConnection = " + dbConnection);
    System.out.println(" stmt = " + stmt);
              if ( resultCount != 1 ){
                System.out.println("queryString is: "+ queryStr);
                throw new OrderDAODBUpdateException("ERROR in ORDER_TABLE INSERT !! resultCount " + resultCount);
              }else {
               queryStr = "SELECT ordernum.currval from " + DatabaseNames.ORDER_TABLE;
               ResultSet rs = stmt.executeQuery(queryStr);
               if ( !rs.next() ) {
                  throw new OrderDAOAppException("ERROR in selecting OrderId !!");
              } else {
                  oid = rs.getInt(1);
                  if (oid < 1)
                   throw new OrderDAOAppException("ERROR in getting OrderId !! orderId = "+ oid);
              }
             } // end else
            } catch(SQLException se) {
               se.printStackTrace(); 
               throw new OrderDAOSysException("SQLException occured in insertOrder");
            } finally {
              try {
                if(stmt != null) stmt.close();
                closeConnection();
              } catch(SQLException se) {
                  throw new OrderDAOSysException("SQLException while closing statement and connection");
              }
           } // end finally