Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. public boolean keyExists(BigDecimal primaryKey, String column) throws SQLException {
  2.          Connection con = null;
  3.          PreparedStatement ps= null;
  4.          ResultSet rs=null;
  5.          int count=0;
  6.          try {
  7.              con = DBConnect.getConnection();
  8.              if (column.equalsIgnoreCase("product_id")) {
  9.                  column="product_id";
  10.                  ps=con.prepareStatement("select count(*) from CSC342.Product where product_id=?");
  11.              } else if (column.equalsIgnoreCase("material_id")) {
  12.                  column="material_id";
  13.                  ps=con.prepareStatement("select count(*) from CSC342.Raw_Material where material_id=?");
  14.              } else {
  15.                  return false;
  16.              }
  17.              
  18.              
  19.              //ps.setString(1,column);
  20.              ps.setBigDecimal(1,primaryKey);
  21.              rs=ps.executeQuery();
  22.              rs.next();
  23.              //System.out.println(count);
  24.              count=rs.getInt(1);
  25.          }catch(SQLException e)
  26.          {
  27.              System.out.println("Error in keyExists access: " + e.getSQLState());
  28.              System.out.println("/nError Code: " + e.getErrorCode());
  29.              System.out.println("/nMessage: " + e.getMessage());
  30.              System.exit( 1 );
  31.          }
  32.          catch(Exception e)
  33.          {
  34.              System.out.println("unknown Error in keyExists");
  35.              System.out.println("/nMessage: " + e.getMessage());
  36.              System.exit( 1 );
  37.          }
  38.          finally
  39.         {
  40.             if (con != null)
  41.                 System.out.println("closing Uses connection \n");
  42.                 rs.close();
  43.                 ps.close();
  44.            
  45.         }
  46.          if (count==1) {
  47.              return true;
  48.          } else {
  49.              return false;
  50.          }
  51.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement