java - query returning amount of rows including ones that were deleted, -


so i'm trying amount of rows exist in mysql table using jdbc using first column reference since amount of rows exist, column on auto_increment but, lets n rows deleted table, new ones inserted, row number new ones keep count in same order, naturally, , when want count number of rows table has, , gettableinfo method returns amount of rows including ones deleted.. how solve ? can done differently this:

public int gettableinfo(string tablename, string db, boolean columncount,         boolean rowcount) {      table = tablename;      connection gettableinfoconn = this.getconnect_to_db(db);      try {         preparedstatement prepstatement = gettableinfoconn                 .preparestatement("select * `" + tablename + "`");          resultset res = prepstatement.executequery();          if ((columncount == false) && (rowcount == false)                 || (columncount == null) && (rowcount == false)                 || (columncount == false) && (rowcount == null)                 || (columncount == null) && (rowcount == null)) {              joptionpane.showmessagedialog(null,                     "uno de los booleanos debe ser true", null,                     joptionpane.error_message, null);          } else {              if (columncount == false && rowcount == true                     || columncount == null) {                  while (res.next()) {                      resultmetadata = res.getint(1);                 }             } else if (columncount == true && rowcount == false                     || rowcount == null) {                  metadata = res.getmetadata();                  resultmetadata = metadata.getcolumncount();              }          }      } catch (sqlexception e) {          joptionpane.showmessagedialog(null, "error in gettableinfo():" + e);     }      return resultmetadata;  } 

if want count number of rows in table can with:

select count(*) mytable 

then res.getint(1) should number of rows in table.

if want count deleted rows, , have auto-increment on id want like:

select max(id) mytable 

then res.getint(1) should number of rows in table + deleted rows.

this depends on correctness of auto-increment. auto-increments skip numbers sake of database optimization. isn't purpose of auto-incrementing, it's not guaranteed of them. being said, if configuration can guarantee right work.

alternatively can have mytabledeleted table. can have delete trigger on mytable populate row mytabledeleted. getting number of deleteds easy.


Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

how to receive file in java(servlet/jsp) -