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(); } }
if need process list on main method create list variable this:
list<mydatatype> listofelements = retrievefromdb();
.if need process list in other class go test package , create new class, example
mylisthandler
create attribute in mylisthandler of type list this:
list<mydatatype> mylist;
create constructor mylisthandler
public mylisthandler(list<mydatatype> mylist){ this.mylist = mylist; }
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()); }
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
Post a Comment