Exemple #1
0
  public String add_()
      throws IllegalArgumentException, SecurityException, ClassNotFoundException,
          IllegalAccessException, NoSuchFieldException, SQLException {
    super.add();
    if (1 == getCfg_res() && !Util.notEmptyString(isPage)) {
      Property newp = (Property) bean;
      if (newp.getProperty().indexOf(".") < 0) {
        String lengthNew = newp.getLength();
        String propertyNameNew = newp.getProperty();
        String typeNew = newp.getType();
        // ①alter table 表名 add 列名 列类型 列参数【加的列在表的最后面】
        // 例:alter table test add username char(20) not null default
        // '';
        // alter table test add birth date not null default
        // '0000-00-00';

        // ②alter table 表名 add 列名 列类型 列参数 after 某列【把新列加在某列后面】
        // 例:alter table test add gender char(1) not null default ''
        // after username;

        // ③alter table 表名 add 列名 列类型 列参数 first【把新列加在最前面】
        // 例:alter table test add pid int not null default 0 first;
        Connection con = null;
        PreparedStatement ps = null;
        try {
          con = LocalConnetionPool.getConnection();
          Bean beanBean = baseDao.get(Bean.class, newp.getBean().getId());
          String sql =
              "alter table "
                  + beanBean.getTableName()
                  + " add "
                  + propertyNameNew
                  + " "
                  + typeNew
                  + "("
                  + lengthNew
                  + ")";
          ps = con.prepareStatement(sql);
          ps.execute();
        } catch (SQLException e) {

        } finally {
          ps.close();
          con.close();
        }
      }
    }
    return SUCCESS;
  }
Exemple #2
0
  public String edit()
      throws SecurityException, IllegalArgumentException, ClassNotFoundException,
          NoSuchFieldException, IllegalAccessException, SQLException {
    DetachedCriteria dc = DetachedCriteria.forClass(bean.getClass());
    dc.add(Restrictions.eq(Constant.ID, idField.get(bean)));
    Property old = baseDao.findBean(dc);
    super.edit();
    // 相应修改数据库的表中的字段
    if (1 == getCfg_res() && !Util.notEmptyString(isPage)) {
      Property oldp = (Property) old;
      String lengthOld = oldp.getLength();
      String propertyNameOld = oldp.getProperty();
      String typeOld = oldp.getType();
      Property newp = (Property) bean;
      String lengthNew = newp.getLength();
      String propertyNameNew = newp.getProperty();
      String typeNew = newp.getType();
      if (lengthOld == null) lengthOld = "";
      if (typeOld == null) typeOld = "";
      if (propertyNameOld.equals(propertyNameNew)) {
        if (!lengthOld.equals(lengthNew) || !typeOld.equals(typeNew)) {
          // alter table 表名 modify 列名 新类型 新参数【修改列类型】
          // 例:alter table test modify gender char(4) not null
          // default '';
          Connection con = null;
          PreparedStatement ps = null;
          try {
            con = LocalConnetionPool.getConnection();
            Bean beanBean = baseDao.get(Bean.class, oldp.getBean().getId());
            if (!Util.notEmptyString(lengthNew)) {
              lengthNew = "0";
            }
            String sql =
                "alter table "
                    + beanBean.getTableName()
                    + " modify "
                    + propertyNameOld
                    + " "
                    + typeNew
                    + "("
                    + lengthNew
                    + ")";
            ps = con.prepareStatement(sql);
            ps.execute();
          } finally {
            ps.close();
            con.close();
          }
        }

      } else {
        // alter table 表名 change 旧列名 新列名 新类型 新参数【修改列名和列类型】
        // 例:alter table test change pid uid int unsigned not null
        // default 0;
        Connection con = null;
        PreparedStatement ps = null;
        try {
          con = LocalConnetionPool.getConnection();
          Bean beanBean = baseDao.get(Bean.class, oldp.getBean().getId());
          String sql =
              "alter table "
                  + beanBean.getTableName()
                  + " change "
                  + propertyNameOld
                  + " "
                  + propertyNameNew
                  + " "
                  + typeNew
                  + "("
                  + lengthNew
                  + ")";
          ps = con.prepareStatement(sql);
          ps.execute();
        } finally {
          ps.close();
          con.close();
        }
      }
    }

    return SUCCESS;
  }