java - How can I move My ResultSet to a specific record -


i´m having troubles moving resultset current record.

i´m learning jdbc , came clipper , delphi. used have kind of "reference" record number or reference "tagged" record, can save reference , move position later. while trying same jdbc , mysql, tried use select / statement record want. thats ok. when try line of record, happens theres 1 line in resultset (i realised happen before write code, had test, cause it´s how learn. :)).

so, make things short. need way "find" record value primary key (with select/where maybe) , "save" position. can move database same record large collection of data (not filtered data). while (next()) or work arround sql codes make form work in diferent way. it´s not goal.

so. got tips on how that? thats dao class. want method "findcod" move resultset same record in rsconsulta.

dao class:

    package control;      import java.sql.connection;     import java.sql.drivermanager;     import java.sql.preparedstatement;     import java.sql.resultset;     import java.sql.sqlexception;     import java.util.arraylist;     import javax.sql.rowset.filteredrowset;     import javax.swing.joptionpane;     import model.pais;      /**      *      * @author x8      */     public class paisdao extends genericdao<pais> {          private connection conn;         private preparedstatement ps;         private resultset rollrs;          private final string select_query="select * paises";          public paisdao() {         }          /**          * cria conexao e retorna mesma.          * @return          * @throws sqlexception           */         @override         public connection getconnection() {             return conn;         }          /**          * @param conn           */         @override         public void startconnection(connection conn)  {             this.conn=conn;          } //startconnection          /**          * @throws sqlexception           */         @override         public void startdataset() throws sqlexception{             ps=conn.preparestatement(select_query,  resultset.type_scroll_sensitive, resultset.concur_updatable);             rollrs=ps.executequery();         }           @override         public void novo(pais p) throws sqlexception{              try {                  rollrs.movetoinsertrow(); //nova linha                 rollrs.updateint("pais", p.getpais());                 rollrs.updatestring("abreviado", p.getabreviado());                 rollrs.updatestring("descricao",p.getdescricao());                 rollrs.updatestring("icone",p.geticone());                 rollrs.insertrow(); //salva alteracoes.                 rollrs.movetocurrentrow();               } catch (sqlexception e) {                 throw new sqlexception(e.getmessage());             }          } //fim novo pais          @override         public void altera(pais p) throws sqlexception{             try {                  rollrs.updateint("pais", p.getpais());                 rollrs.updatestring("abreviado", p.getabreviado());                 rollrs.updatestring("descricao",p.getdescricao());                 rollrs.updatestring("icone",p.geticone());                 rollrs.updaterow();                 rollrs.movetocurrentrow();             } catch (sqlexception e) {                 throw new sqlexception(e.getmessage());             }          }          public void filtra(string filtro) throws sqlexception{             rollrs=ps.executequery(select_query+" "+filtro);          }          public string filtrapais(int ...pais) throws sqlexception{             string query="(";             int cont=0;             (int p:pais){                 query+="(pais="+integer.tostring(p)+") ";                 cont++;                 if (cont<pais.length)                     query+="or ";             }             query+=")";             filtra(query);             return query;         }           @override         public void exclui() throws sqlexception{             rollrs.deleterow();             rollrs.movetocurrentrow();         }          /**          *          * @return          * @throws sqlexception          */         @override         public int getrow() throws sqlexception{             return rollrs.getrow();         }         /**          *           * @param row          * @return          * @throws sqlexception           */         @override         public int gotorow(int row) throws sqlexception{             int oldrow=getrow();             rollrs.absolute(row);             return oldrow;         }         /**          *           * @throws sqlexception           */         @override         public void first() throws sqlexception{             rollrs.first();         }         /**          *           * @throws sqlexception           */         @override         public void last() throws sqlexception {             rollrs.last();         }          @override         public void previous() throws sqlexception{             if (rollrs.isbeforefirst()){                 rollrs.first();             } else {                 rollrs.previous();             }         }         /**          *           * @throws sqlexception           */         @override         public void next() throws sqlexception{             if (rollrs.isafterlast()){                 rollrs.last();             } else {                 rollrs.next();             }         }          /**          * retorna objeto no registro atual          * @return           * @throws java.sql.sqlexception           */         @override         public pais getcurrent() throws sqlexception {             pais p=new pais();              p.setpais(rollrs.getint("pais"));             p.setdescricao(rollrs.getstring("descricao"));             p.setabreviado(rollrs.getstring("abreviado"));             p.seticone(rollrs.getstring("icone"));             return p;         }          /**          * fecha todas conexoes e datasets          * @throws sqlexception           */             @override         public void closeall() throws sqlexception {             try {                  if (!rollrs.isclosed())                     rollrs.close();                 if (!ps.isclosed())                     ps.close();                 if (!conn.isclosed())                     conn.close();              } catch (sqlexception e) {                 throw new sqlexception(e.getmessage());              }          } //fim closeall          public arraylist<pais> asarraylist() throws sqlexception{             arraylist <pais> apais=new arraylist<>();             first();             apais.add(getcurrent());             while (rollrs.next()){                 apais.add(getcurrent());              }              return apais;         }          public string[] getcolumnnames(){             string[] nomes={"pais","abrev","descrição","icone"};               return nomes;         }          public void findcod(pais p) throws sqlexception{             int codpais=p.getpais();             preparedstatement psconsulta=conn.preparestatement(select_query+"  pais=?", resultset.type_scroll_sensitive, resultset.concur_updatable);             psconsulta.setint(1, codpais);             resultset rsconsulta=psconsulta.executequery();               rsconsulta.first();             //joptionpane debug              joptionpane.showmessagedialog(null, "line  number"+rsconsulta.getrow());             //there suposed move rollrs dataset same record   of rsconsulta             gotorow(rsconsulta.getrow());  //always go line 1.             }     } //eof 

thanks in advance. hope made easy understand.

your resultset contain rows selected sql query executed.

if query select * paises, all rows transferred database program.

if query select * paises pais=?, row(s) matching criteria selected. cannot navigate resultset other rows, you'll have execute query.

in general, never want all rows (except maybe code tables). should query rows want see.

to update row, have 2 choices:

  1. execute update sql statement.
  2. execute select statement row updated using updatable resultset, set values , call updaterow().

to insert new row, execute insert sql statement. don't want use updatable resultset this, because you'd have execute dummy select first, , that's inefficient.

also, might have @ cachedrowset, working data in disconnected fashion.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -