java - How to add to an ArrayList the data of a ResultSet? -


i want have arraylist data in postgresql database.

for example:

column1   column2   column3   column4         b         c         d j         d         s         e 

arraylist.get(0) = (a, b, c, d) arraylist.get(1) = (j, d, s, e) 

i have this:

public static arraylist<ordenes> selectinstruct (int a) throws sqlexception     {         string driver = "org.postgresql.driver";         string server = "jdbc:postgresql://localhost:5432/postgres";         string user = "usuario";         string pass = "contraseƱa";          arraylist<ordenes> ordenes = new arraylist<ordenes>();         ordenes ord = new ordenes();          try         {             class.forname(driver);              connection conexion = drivermanager.getconnection(server, user, pass);             statement dato = conexion.createstatement();              resultset rs = dato.executequery("select * \"ordenes\"" + "where \"operario\" = "+a+";");              conexion.close();             rs.              if (rs.next())             {                 ord.setnumorden(rs.getstring("numero de orden"));                 ord.setstatus(rs.getstring("status"));                 ord.setfechainicio(rs.getstring("fecha inicio"));                 ord.getprd().setnumero(rs.getint("numero de producto"));                 ord.getprd().setdescripcion(rs.getstring("descripcion producto"));                 ord.setcantidad(rs.getint("cantidad solicitada"));                 ord.setdescripcion(rs.getstring("descripcion orden"));                 ord.getprd().getinstr().get(0).setcodins(rs.getint("codigo instruccion"));                  ord.getprd().getinstr().get(0).setmat(rs.getint("material"));                 ord.getoper().add(rs.getint("operario"));              }              ordenes.add(ord);           }         catch (exception e)         {          }          return ordenes;      } 

can insert while or for?

if excepting more result use while loop below code. change if while.

public static arraylist<ordenes> selectinstruct (int a) throws sqlexception     {         string driver = "org.postgresql.driver";         string server = "jdbc:postgresql://localhost:5432/postgres";         string user = "usuario";         string pass = "contraseƱa";          arraylist<ordenes> ordenes = new arraylist<ordenes>();           try         {             class.forname(driver);              connection conexion = drivermanager.getconnection(server, user, pass);             statement dato = conexion.createstatement();              resultset rs = dato.executequery("select * \"ordenes\"" + "where \"operario\" = "+a+";");              conexion.close();             rs.              while(rs.next())             {                 ordenes ord = new ordenes();                 ord.setnumorden(rs.getstring("numero de orden"));                 ord.setstatus(rs.getstring("status"));                 ord.setfechainicio(rs.getstring("fecha inicio"));                 ord.getprd().setnumero(rs.getint("numero de producto"));                 ord.getprd().setdescripcion(rs.getstring("descripcion producto"));                 ord.setcantidad(rs.getint("cantidad solicitada"));                 ord.setdescripcion(rs.getstring("descripcion orden"));                 ord.getprd().getinstr().get(0).setcodins(rs.getint("codigo instruccion"));                  ord.getprd().getinstr().get(0).setmat(rs.getint("material"));                 ord.getoper().add(rs.getint("operario"));              }              ordenes.add(ord);           }         catch (exception e)         {           // handle exception here          }          return ordenes;      }     

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -