Example #1
0
  public static void getObject(Object obj, DBInfo info, Connection conn) throws Exception {

    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {

      StringBuffer sql = new StringBuffer();
      sql.append("select ");
      int i = 0;
      for (DBField f : info.getAllFields()) {
        sql.append(f.getDBName());
        if (i < (info.getAllFields().size() - 1)) {
          sql.append(", ");
        }
        i++;
      }
      sql.append(" from " + info.getTable() + " where ");

      i = 0;
      for (DBField f : info.getPKeys()) {
        sql.append(f.getDBName() + " = ?");
        if (i < (info.getPKeys().size() - 1)) {
          sql.append(" and ");
        }
        i++;
      }

      pstmt = conn.prepareStatement(sql.toString());

      i = 0;
      for (DBField f : info.getPKeys()) {
        DBHelper.setPreparedStatement(obj, pstmt, (i + 1), f);
        i++;
      }

      rs = pstmt.executeQuery();

      if (rs.next()) {
        for (DBField f : info.getAllFields()) {
          ObjectHelper.setObjectField(obj, f, rs);
        }

      } else {
        throw new Exception("Binder -> Sorry can not find object");
      }

    } catch (Exception e) {
      throw e;
    } finally {
      DBUtil.closePreparedStatement(pstmt, rs);
    }
  }
Example #2
0
  public static void insert(Object obj, DBInfo info, Connection conn) throws Exception {

    for (DBField f : info.getAutoKeys()) {
      long id = DBHelper.getAutoId(obj, info, f, conn);
      ObjectHelper.setAutoObjectField(obj, f, id);
    }

    PreparedStatement pstmt = null;
    try {

      String sql = "insert into " + info.getTable();
      sql += "(";
      int i = 0;

      for (DBField f : info.getAllFields()) {
        sql += f.getDBName();
        if (i < (info.getAllFields().size() - 1)) {
          sql += ",";
        }
        i++;
      }

      sql += ")";
      sql += " values(";
      i = 0;
      for (; i < info.getAllFields().size(); i++) {
        sql += "?";
        if (i < (info.getAllFields().size() - 1)) {
          sql += ",";
        }
      }

      sql += ")";

      pstmt = conn.prepareStatement(sql);

      i = 0;
      for (DBField f : info.getAllFields()) {
        DBHelper.setPreparedStatement(obj, pstmt, (i + 1), f);
        i++;
      }

      pstmt.execute();

    } catch (Exception e) {
      throw e;
    } finally {
      DBUtil.closePreparedStatement(pstmt);
    }
  }
Example #3
0
  public static void update(Object obj, DBInfo info, Connection conn) throws Exception {

    PreparedStatement pstmt = null;
    try {

      StringBuffer sql = new StringBuffer();
      sql.append("update " + info.getTable() + " set ");
      int i = 0;

      for (DBField f : info.getAllFields()) {
        sql.append(f.getDBName() + " = ?");

        if (i < (info.getAllFields().size() - 1)) {
          sql.append(", ");
        }
        i++;
      }

      sql.append(" where ");
      i = 0;

      for (DBField f : info.getPKeys()) {
        sql.append(f.getDBName() + " = ?");
        if (i < (info.getPKeys().size() - 1)) {
          sql.append(" and ");
        }
        i++;
      }

      pstmt = conn.prepareStatement(sql.toString());

      i = 0;
      for (DBField f : info.getAllFields()) {
        DBHelper.setPreparedStatement(obj, pstmt, (i + 1), f);
        i++;
      }

      for (DBField f : info.getPKeys()) {
        DBHelper.setPreparedStatement(pstmt, (i + 1), f.getValue());
        i++;
      }

      pstmt.execute();

    } catch (Exception e) {
      throw e;
    } finally {
      DBUtil.closePreparedStatement(pstmt);
    }
  }
Example #4
0
  public static boolean exists(Object obj, DBInfo info, Connection conn) throws Exception {
    boolean flag = false;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {

      StringBuffer sql = new StringBuffer();
      sql.append("select ");
      int i = 0;
      for (DBField f : info.getAllFields()) {
        sql.append(f.getDBName());
        break;
      }
      sql.append(" from " + info.getTable() + " where ");

      i = 0;
      for (DBField f : info.getPKeys()) {
        sql.append(f.getDBName() + " = ?");
        if (i < (info.getPKeys().size() - 1)) {
          sql.append(" and ");
        }
        i++;
      }

      pstmt = conn.prepareStatement(sql.toString());

      i = 0;
      for (DBField f : info.getPKeys()) {
        DBHelper.setPreparedStatement(obj, pstmt, (i + 1), f);
        i++;
      }

      rs = pstmt.executeQuery();

      if (rs.next()) {
        flag = true;
      }

    } catch (Exception e) {
      throw e;
    } finally {
      DBUtil.closePreparedStatement(pstmt, rs);
    }
    return flag;
  }