コード例 #1
0
ファイル: DataRow.java プロジェクト: duxingzhe311/system4j
  public void doDeleteDB(Connection conn, String tablename, String uniquecol) throws SQLException {
    StringBuilder delsql = new StringBuilder();
    delsql.append("delete from ").append(tablename);
    delsql.append(" where ").append(uniquecol).append("=?");

    PreparedStatement ps = null;
    try {
      ps = conn.prepareStatement(delsql.toString());
      Object tmpo = GDB.prepareObjVal(this.getValue(uniquecol));
      if (tmpo == null) throw new SQLException("no key value found");
      ps.setObject(1, tmpo);
      ps.executeUpdate();
    } finally {
      if (ps != null) ps.close();
    }
  }
コード例 #2
0
ファイル: DataRow.java プロジェクト: duxingzhe311/system4j
  public void doUpdateDB(Connection conn, String tablename, String uniquecol, String[] cols)
      throws SQLException {
    StringBuilder upsql = new StringBuilder();
    upsql.append("update ").append(tablename).append(" set ");
    upsql.append(cols[0]).append("=?");

    for (int i = 1; i < cols.length; i++) {
      upsql.append(",").append(cols[i]).append("=?");
    }
    upsql.append(" where ").append(uniquecol).append("=?");

    String[] allcols = new String[cols.length + 1];
    System.arraycopy(cols, 0, allcols, 0, cols.length);
    allcols[cols.length] = uniquecol;
    PreparedStatement ps = null;
    try {
      // System.out.println(upsql.toString());
      ps = conn.prepareStatement(upsql.toString());
      for (int i = 0; i < allcols.length; i++) {
        Object tmpo = GDB.prepareObjVal(this.getValue(allcols[i]));
        if (tmpo != null) {
          ps.setObject(i + 1, tmpo);
        } else {
          int sqlt =
              this.belongToDT
                  .getColumn(allcols[i])
                  .getJdbcType(); // JavaColumnInfo.Class2SqlType(c)
          if (sqlt == java.sql.Types.BLOB) // for sqlserver driver
            // NullPointer error
            ps.setObject(1 + i, new byte[0]);
          else ps.setNull(1 + i, sqlt);
        }
      }

      ps.executeUpdate();
    } finally {
      if (ps != null) ps.close();
    }
  }
コード例 #3
0
ファイル: DataRow.java プロジェクト: duxingzhe311/system4j
  /**
   * 根据数据库连接和表和相关列,做数据库的插入操作
   *
   * @param conn
   * @param tablename
   * @param colnames
   * @throws SQLException
   */
  public void doInsertDB(Connection conn, String tablename, String[] colnames) throws SQLException {
    StringBuilder insertsql = new StringBuilder();
    insertsql.append("insert into ").append(tablename);
    StringBuilder lstr = new StringBuilder(), rstr = new StringBuilder();
    lstr.append("(").append(colnames[0]);
    rstr.append("(?");

    for (int i = 1; i < colnames.length; i++) {
      lstr.append(",").append(colnames[i]);
      rstr.append(",?");
    }
    lstr.append(")");
    rstr.append(")");

    insertsql.append(lstr).append("values").append(rstr);

    PreparedStatement ps = null;
    try {
      // System.out.println(insertsql.toString()) ;
      ps = conn.prepareStatement(insertsql.toString());
      for (int i = 0; i < colnames.length; i++) {
        Object tmpo = GDB.prepareObjVal(this.getValue(colnames[i]));
        if (tmpo != null) {
          ps.setObject(i + 1, tmpo);
        } else {
          int sqlt =
              this.belongToDT
                  .getColumn(colnames[i])
                  .getJdbcType(); // JavaColumnInfo.Class2SqlType(c)
          if (sqlt == java.sql.Types.BLOB) // for sqlserver driver
            // NullPointer error
            ps.setObject(1 + i, new byte[0]);
          else ps.setNull(1 + i, sqlt);
        }
      }

      ps.executeUpdate();
    } finally {
      if (ps != null) ps.close();
    }
  }