mysql - Using query results in a list from one class to another in Java -


i retrieving data database in class. want use data , process it, separate different sections (i have timestamps, , count frequency of names in between specific dates). having trouble finding out how use data retrieved in class. here source code retrieval:

    package test;  import java.sql.drivermanager; //import java.sql.resultset; import java.sql.sqlexception; import java.util.arraylist; //import java.util.arrays; import java.util.list;  import javax.sql.rowset.cachedrowset; import javax.sql.rowset.rowsetprovider;  import com.mysql.jdbc.connection; import com.mysql.jdbc.statement;  public class enterd {  private static string url = "url"; private static string user = "user"; private static string password = "pass" ;  private static string sqlquery1 = "select component_type, ctime table;";  public static list<mydatatype> retrievefromdb() throws sqlexception {     list<mydatatype> datalist = new  arraylist<>();     try (connection conn = (connection) drivermanager.getconnection(url, user, password)) {         try (statement stmt = (statement) conn.createstatement()) {             try (cachedrowset crs = rowsetprovider.newfactory().createcachedrowset(); ) {                 crs.setcommand(sqlquery1);                 crs.execute(conn);                 conn.setautocommit(false);                 while(crs.next()){                     mydatatype datatype = new mydatatype();                     datatype.setcpntype(crs.getstring(1));                     datatype.setcpntimestamp(crs.gettimestamp(2));                     datalist.add(datatype);                          }             }         }     } catch (exception e) {         e.printstacktrace(system.out);     }            return datalist; } public static void main(string[] args) throws sqlexception {     retrievefromdb(); } } 

  1. if need process list on main method create list variable this: list<mydatatype> listofelements = retrievefromdb();.

  2. if need process list in other class go test package , create new class, example mylisthandler

  3. create attribute in mylisthandler of type list this: list<mydatatype> mylist;

  4. create constructor mylisthandler

public mylisthandler(list<mydatatype> mylist){ this.mylist = mylist; }

  1. create methods work list in mylisthandler ] methods:

    public void printlist(){ (mydatatype item: mylist) { system.out.println(item); } }

    public void clearlist(){     system.out.println("my list size: " + mylist.size());     mylist.clear();     system.out.println("my list size after clear: " + mylist.size()); } 
  2. finally instantiate mylisthandler in main method:

enterd class:

   public static void main(string[] args) throws sqlexception {         list<mydatatype> listofelements = retrievefromdb();         mylisthandler mylisthandler = new mylisthandler(listofelements);         mylisthandler.printlist();         mylisthandler.clearlist();     } 

see following complete example of mylisthandler:

mylisthandler.class

public class mylisthandler {      list<mydatatype> mylist;     public mylisthandler(list<mydatatype> mylist){         this.mylist = mylist;     }      public void printlist(){         (mydatatype item: mylist) {             system.out.println(item);         }     }      public void clearlist(){         system.out.println("my list size: " + mylist.size());         mylist.clear();         system.out.println("my list size after clear: " + mylist.size());     }  } 

regards.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -