accessing list elements in java - java
I created a linkedlist object as follows
importBuffer = new BufferedReader(new FileReader(importcsvFile));
while ((line = importBuffer.readLine()) != null) {
// use comma as separator
String[] importedFile = line.split(cvsSplitBy); //cap,comune,provincia,stato
System.out.println("Codice Azienda " + importedFile[0] + " , Codice Cliente=" + importedFile[1] + " , Regione Sociale=" + importedFile[2] + " , Indrizzo=" + importedFile[3] + " , comune=" + importedFile[4] + " , provincia=" + importedFile[5] + " , stato=" + importedFile[6] +"]");
counter++;
PublicDefinition.importList.add(importBuffer.toString());
List customers = select.select(importedFile[0],importedFile[1], importedFile[3]);
if(!customers.isEmpty())
{
System.out.println("selected Customer : " + customers.size());
buffureList = customers;
Object a=List.class.cast(customers);
PublicDefinition.testingList.add(buffureList.toString());
System.out.println("selected Customer : " + PublicDefinition.importList.get(0));
System.out.println("selected Customer : " + PublicDefinition.testingList.getFirst());
updateCustomer = customers;
if(customers.get(0)==importedFile[0])
System.out.println("Matched Codice Azienda");
select.updateTable(importedFile[1], importedFile[3], "10.34", "11.40"); //String CodiceCliente, String indrizzo, String latitude, String longitute
}
}
when I try to access the elements for the linkedlist using
System.out.println("selected Customer : " + PublicDefinition.importList.get(0));
I got the output:
selected Customer : java.io.BufferedReader#420dc55b
I think this is the memory reference, but I want to retrieve the value of the linkedlist
my select function is:
public List<Customer> select(String codiceAzienda, String codiceCliente, String indrizzo) {
return jdbcTemplate.query(
"SELECT * FROM customers WHERE CodiceAzienda= ?",
new Object[] { codiceAzienda},
(rs, rowNum) -> new Customer(rs.getLong("id"),
rs.getString("CodiceAzienda"), rs.getString("Indrizzo"), rs.getString("codice_cliente"), rs.getString("Indrizzo")));
}
You added the toString() value of the importBuffer object, not the actual contents. The default toString() implementation (which every object inherits from... Object) returns ClassName#HashCode. So your output isn't wrong, but your input is.
See Object.toString() in the javadoc
Go ahead and perform:
PublicDefinition.importList.add(importBuffer.readLine());
Instead of :
PublicDefinition.importList.add(importBuffer.toString());
Since you are trying to output the contents of the buffered reader instead of the ClassName#Hashcode contents.
Replace
PublicDefinition.importList.add(importBuffer.toString());
with
PublicDefinition.importList.add(importedFile);
You are accidentally adding the string representation of BufferReader object, not the list of import files which sounds like your intention.
Related
How can i return multiple getters from my method?
I have made a method that is requesting a column from my MySQL database. Fortunately this is working, but i am trying to acces the return value. Because i want to acces the getters and setters for Voedingsmiddel. A new Voedingsmiddel is created inside the method and later returned. The values of the database are assigned as the attributes of the voedingsmiddel. This method is created in my Database class and i am trying to acces it within another class. If i am trying to acces voedingsmiddel outside of the method, it is nog giving me an object, instead it is giving me the datalocation Voedingsmiddel#6f7fd0e6. I was able to get some of the result i am trying to get. I achieved it by changing public Voedingsmiddel getVoedingsmiddelBijId to public double getVoedingsmiddelBijId. I also changed return voedingsmiddel; into voedingsmiddel.getCalorieen; So i was able to get 1 of the getters. By calling the method Database.getVoedingsmiddelBijId(int).getCalorieen(); Intellij is telling me the following (Result of'Voedingsmiddel.getCalorieen()' is ignored). public Voedingsmiddel getVoedingsmiddelBijId(int idVoedingsmiddel) { Voedingsmiddel voedingsmiddel = new Voedingsmiddel("", 0, 0,0,0,0); try { String query = "select * from voedingsmiddel where idVoedingsmiddel = ?"; PreparedStatement preparedStatement = myCon.prepareStatement(query); preparedStatement.setInt(1, idVoedingsmiddel); myRes = preparedStatement.executeQuery(); while (myRes.next()) { int idvoedingsmiddel = myRes.getInt("idVoedingsmiddel"); String naam = myRes.getString("naam"); int gram = myRes.getInt("gram"); double calorieen = myRes.getDouble("calorieën"); double koolhydraten = myRes.getDouble("koolhydraten"); double eiwitten = myRes.getDouble("eiwitten"); double vetten = myRes.getDouble("vetten"); voedingsmiddel = new Voedingsmiddel(naam, gram, calorieen, koolhydraten, eiwitten, vetten); System.out.println("idVoedingsmiddel " + idvoedingsmiddel + " naam " + naam + " gram " + gram + " calorieën " + calorieen + " koolhydraten " + koolhydraten + " eiwitten " + eiwitten + " vetten " + vetten); } } catch (Exception exc) { exc.printStackTrace(); System.out.println("Error: " + exc); } return voedingsmiddel; } First of all, i would really appreciate it, if someone could explain me what i am doing wrong. Also a solution to the problem would be more then welcome! The expected result is that the return value of the method would be an object instead of what i think is a datalocation. So i can call the getters and setters for that object. I have a history of posting too much code and not enough information. So i hope this is better, if not feel free to correct me.
Why do I get, and how do I solve this "String to object of type <objecttype>" error
I am (being an absolute beginner), trying to create a simple tool, that creates some objects and links them. The objects are: Customers Licenses (2 types, extends class) The idea is to use (one of) the customer company name when creating a license, so the license is linked to a customer. I use ArrayLists to store the data. I tried to use the getter for Customer cCompany, but when I try to actually create a new license object, I get errors about incompatible types (String to object of type customer) How can I fix that error? Any help is highly appreciated, but please explain well, me being an absolute beginner. I probably overcomplicate stuff.... Some code extracts: From Main: public class Main { public static void main(String[] args) { //Create customers List <Customer> customers = new ArrayList <> (10); customers.add(new Customer("TestCompany","John Doe",1234567890,"John#testcompany.com")); .... //Create Elvis licenses (based on superclass License) List <ElvisLicense> ellicenses = new ArrayList <> (10); ellicenses.add(new ElvisLicense("TestCompany","VendorA",1234,"1234-A","Solutions Server gold","1234-dtbk-87654-nlof",10, true , true)); Class: Customer: class Customer { String cCompany; private String cName; private int cPhone; private String cEmail; public Customer( String cCompany, String cName,int cPhone, String cEmail) { this.cCompany = cCompany; this.cName = cName; this.cPhone = cPhone; this.cEmail = cEmail; } //This getter should be used to link the license to the customer (Done in License.java) public String getcCompany() { return cCompany; } Class License (Superclass) class License { // Used no modifier to set access for Class/Package and Subclass inside the package Customer licenseCompany; String lVendor; int lContractNumber; String lCertificateNumber; String lProductName; String lLicenseKey; int lNumberOfSeats; public License(Customer cCompany, String lVendor, int lContractNumber, String lCertificateNumber, String lProductName, String lLicenseKey, int lNumberOfSeats) { licenseCompany = cCompany; this.lVendor = lVendor; this.lVendor = lVendor; this.lContractNumber = lContractNumber; this.lCertificateNumber = lCertificateNumber; this.lProductName = lProductName; this.lLicenseKey = lLicenseKey; this.lNumberOfSeats = lNumberOfSeats; } public Customer getLicenseCompany() { return licenseCompany; } public void setLicenseCompany(Customer licenseCompany) { this.licenseCompany = licenseCompany; } //preparations to allow for example printing the content of an arraylist element #Override public String toString(){ return "Customer name " + getLicenseCompany() + "\n" + "Vendor name " + getlVendor() + "\n" + "Contract number: " + getlContractNumber() + "\n" + "Certificate number: " + getlCertificateNumber() + "\n" + "Product name " + getlProductName() + "\n" + "Licence key: " + getlLicenseKey() + "\n" + "Number of seats: " + getlNumberOfSeats(); } } And the extended class: public class ElvisLicense extends License{ private boolean elIsBundle; private boolean elIsSubscription; public ElvisLicense( Customer licenseCompany, String lVendor, int lContractNumber, String lCertificateNumber, String lProductName, String lLicenseKey, int lNumberOfSeats, boolean elIsBundle, boolean elIsSubscription ) { super( licenseCompany, lVendor, lContractNumber, lCertificateNumber, lProductName, lLicenseKey, lNumberOfSeats); this.elIsBundle = elIsBundle; this.elIsSubscription = elIsSubscription; } ..... #Override public String toString(){ return "Customer name " + licenseCompany + "\n" + "Vendor name " + lVendor + "\n" + "Contract number: " + lContractNumber + "\n" + "Certificate number: " + lCertificateNumber + "\n" + "Product name " + lProductName + "\n" + "Licence key: " + lLicenseKey + "\n" + "Number of seats: " + lNumberOfSeats + "\n" + "Number of seats: " + elIsBundle + "\n" + "Number of seats: " + elIsSubscription; } } I expect that the Customername is used when creating a new license.
Below line is wrong. ellicenses.add(new ElvisLicense("TestCompany","VendorA",1234,"1234-A","Solutions Server gold","1234-dtbk-87654-nlof",10, true , true)); As license need customer object an parameter. Instead, you should create customer object first. ellicenses.add(new ElvisLicense(new Customer("TestCompany","VendorA",1234,"1234-A"),"Solutions Server gold","1234-dtbk-87654-nlof",10, true , true)); for reusing that customer list to avoid create company. for(Customer customer : customers){ // here you need some way to offer other parameters except customer parameter. License license = new new ElvisLicense(customer,"Solutions Server gold","1234-dtbk-87654-nlof",10, true , true); ellicenses.add(license); }
What you need to do is to use one of the Customer objects you have already created when creating the ElvisLicense object. To more easily find that customer by name I suggest you store them in a map instead of a list with the name as a key. Map<String, Customer> customerMap = new HashMap<>(); Customer customer = new Customer("TestCompany","John Doe",1234567890,"John#testcompany.com")); customerMap.put(customer.getcCompany(), customer); so when creating the license you look up the customer List <ElvisLicense> ellicenses = new ArrayList <> (10); Customer customer = customerMap.get("TestCompany"); if (customer != null) { ElvisLicense license = new ElvisLicense(customer,"VendorA",1234,"1234-A","Solutions Server gold","1234-dtbk-87654-nlof",10, true , true)); ellicenses.add(license); } else { //If the customer isn't found you need some kind of error handling, better than below :) System.out.println("Can't create a license, no customer found"); }
why is "+" added to all my coordinates in url builder?
I have this code to build a string of coordinates: public String generateUrl(CompleteRoutingResponseShort completeRoutingResponse) { if (completeRoutingResponse == null) { return "CompleteRoutingResponse is null"; } UriBuilder builder = UriBuilder .fromPath(Constants.LIVEMAP_BASE_URL) .scheme("http"); builder.queryParam("lineGeom", getCoordsLists(completeRoutingResponse)); return builder.build().toString(); } private String getCoordsLists() { List<String> lists = new ArrayList<>(); for (AlternativeShort alternative : completeRoutingResponses.alternatives) { String list = "( " + alternative.coords.stream().map(item -> String.format("%.4f , %.4f", item.x, item.y)) .collect(Collectors.joining(", ")) + ")"; // list.replace("+",""); lists.add(list); } return String.join(", ", lists); } however I get this url as an output: "http:/myhost.com/tiles/internal?lineGeom=(+34.9178+,+31.9550,+34.9175+,+31.9552,+34.9174+,+31.9553,+34.9172+,+31.9555,+34.9171+,+31.9556,+34.9171+,+31.9556,+34.9168+,+31.9559,+34.9168+,+31.9559,+34.9167+,+31.9561,+34.9165+,+31.9562,+34.9164+,+31.9564,+34.9161+,+31.9567,+34.9161+,+31.9568,+34.9160+,+31.9570,+34.9159+,+31.9571,+34.9158+,+31.9572,+34.9158+,+31.9573,+34.9157+,+31.9574,+34.9157+,+31.9574,+34.9166+,+31.9577,+34.9166+,+31.9577,+34.9178+,+31.9581,+34.9184+,+31.9583,+34.9188+,+31.9583,+34.9192+,+31.9584,+34.9195+,+31.9585,+34.9201+,+31.9585,+34.9204+,+31.9585,+34.9208+,+31.9585,+34.9211+,+31.9585,+34.9214+,+31.9585,+34.9216+,+31.9585,+34.9222+,+31.9584,+34.9224+,+31.9584,+34.9227+,+31.9583,+34.9233+,+31.9582,+34.9233+,+31.9582,+34.9238+,+31.9580,+34.9246+,+31.9577,+34.9257+,+31.9572,+34.9262+,+31.9571,+34.9265+,+31.9569,+34.9266+,+31.9569,+34.9268+,+31.9569,+34.9271+,+31.9568,+34.9276+,+31.9567,+34.9281+,+31.9566,+34.9284+,+31.9566,+34.9289+,+31.9566,+34.9307+,+31.9565,+34.9314+,+31.9564,+34.9320+,+31.9564,+34.9325+,+31.9563,+34.9325+,+31.9563,+34.9330+,+31.9563,+34.9335+,+31.9563,+34.9340+,+31.9562,+34.9345+,+31.9562,+34.9345+,+31.9562,+34.9357+,+31.9560,+34.9361+,+31.9560,+34.9365+,+31.9559,+34.9365+,+31.9559,+34.9369+,+31.9559,+34.9373+,+31.9558,+34.9373+,+31.9558,+34.9376+,+31.9557,+34.9377+,+31.9556,+34.9382+,+31.9554,+34.9387+,+31.9549,+34.9387+,+31.9549,+34.9389+,+31.9547,+34.9394+,+31.9542,+34.9394+,+31.9542,+34.9395+,+31.9543,+34.9395+,+31.9543,+34.9398+,+31.9545,+34.9399+,+31.9546,+34.9399+,+31.9546,+34.9401+,+31.9548,+34.9407+,+31.9555,+34.9414+,+31.9565,+34.9417+,+31.9571,+34.9417+,+31.9571,+34.9418+,+31.9572,+34.9418+,+31.9572,+34.9420+,+31.9578,+34.9420+,+31.9579,+34.9421+,+31.9585,+34.9421+,+31.9585,+34.9421+,+31.9590,+34.9421+,+31.9599,+34.9421+,+31.9599,+34.9422+,+31.9607,+34.9422+,+31.9608,+34.9423+,+31.9609,+34.9423+,+31.9610,+34.9423+,+31.9610,+34.9423+,+31.9611,+34.9424+,+31.9611,+34.9425+,+31.9612,+34.9426+,+31.9613,+34.9426+,+31.9613,+34.9426+,+31.9613,+34.9429+,+31.9616,+34.9432+,+31.9618,+34.9436+,+31.9621,+34.9449+,+31.9630,+34.9449+,+31.9630,+34.9487+,+31.9649,+34.9487+,+31.9649,+34.9525+,+31.9668,+34.9525+,+31.9668,+34.9531+,+31.9671,+34.9535+,+31.9672,+34.9538+,+31.9674,+34.9542+,+31.9676,+34.9546+,+31.9678,+34.9550+,+31.9680,+34.9553+,+31.9682,+34.9554+,+31.9682,+34.9558+,+31.9684,+34.9566+,+31.9689,+34.9570+,+31.9691,+34.9573+,+31.9694,+34.9579+,+31.9698,+34.9582+,+31.9701,+34.9586+,+31.9704,+34.9591+,+31.9709,+34.9594+,+31.9713,+34.9599+,+31.9720,+34.9602+,+31.9725,+34.9605+,+31.9730,+34.9608+,+31.9736,+34.9610+,+31.9742,+34.9612+,+31.9746,+34.9613+,+31.9752,+34.9614+,+31.9756,+34.9615+,+31.9764,+34.9627+,+31.9857,+34.9630+,+31.9883,+34.9632+,+31.9892,+34.9634+,+31.9902,+34.9637+,+31.9912,+34.9654+,+31.9961,+34.9658+,+31.9974,+34.9659+,+31.9979,+34.9660+,+31.9984,+34.9662+,+31.9994,+34.9663+,+32.0002,+34.9664+,+32.0011,+34.9671+,+32.0154,+34.9671+,+32.0166,+34.9671+,+32.0169,+34.9671+,+32.0176,+34.9670+,+32.0179,+34.9669+,+32.0185,+34.9668+,+32.0192,+34.9666+,+32.0198,+34.9663+,+32.0206,+34.9660+,+32.0211,+34.9656+,+32.0218,+34.9652+,+32.0225,+34.9649+,+32.0229,+34.9644+,+32.0236,+34.9627+,+32.0256,+34.9607+,+32.0279,+34.9602+,+32.0285,+34.9596+,+32.0292,+34.9587+,+32.0300,+34.9534+,+32.0351,+34.9530+,+32.0356,+34.9525+,+32.0361,+34.9520+,+32.0368,+34.9515+,+32.0374,+34.9414+,+32.0522,+34.9408+,+32.0531,+34.9404+,+32.0539,+34.9399+,+32.0548,+34.9396+,+32.0555,+34.9392+,+32.0567,+34.9381+,+32.0603,+34.9381+,+32.0603,+34.9357+,+32.0679,+34.9357+,+32.0679,+34.9341+,+32.0730,+34.9341+,+32.0730,+34.9336+,+32.0747,+34.9331+,+32.0765,+34.9331+,+32.0765,+34.9330+,+32.0767,+34.9330+,+32.0768,+34.9328+,+32.0781,+34.9328+,+32.0781,+34.9328+,+32.0793,+34.9328+,+32.0793,+34.9333+,+32.0832,+34.9341+,+32.0899,+34.9351+,+32.0989,+34.9358+,+32.1057,+34.9359+,+32.1064,+34.9359+,+32.1064,+34.9359+,+32.1075,+34.9361+,+32.1093,+34.9363+,+32.1106,+34.9366+,+32.1119,+34.9369+,+32.1130,+34.9369+,+32.1131,+34.9373+,+32.1141,+34.9375+,+32.1146,+34.9381+,+32.1159,+34.9387+,+32.1171,+34.9391+,+32.1178,+34.9391+,+32.1178,+34.9399+,+32.1189,+34.9403+,+32.1195,+34.9410+,+32.1204,+34.9411+,+32.1204,+34.9414+,+32.1208,+34.9442+,+32.1238,+34.9477+,+32.1273,+34.9477+,+32.1273,+34.9497+,+32.1294,+34.9497+,+32.1294,+34.9533+,+32.1331,+34.9533+,+32.1331,+34.9554+,+32.1350,+34.9556+,+32.1351,+34.9558+,+32.1352,+34.9562+,+32.1354,+34.9564+,+32.1355,+34.9566+,+32.1355,+34.9578+,+32.1358,+34.9582+,+32.1360,+34.9586+,+32.1361,+34.9589+,+32.1363,+34.9591+,+32.1364,+34.9593+,+32.1366,+34.9595+,+32.1368,+34.9596+,+32.1370,+34.9598+,+32.1372,+34.9598+,+32.1374,+34.9599+,+32.1377,+34.9600+,+32.1379,+34.9600+,+32.1381,+34.9600+,+32.1383,+34.9599+,+32.1385,+34.9599+,+32.1387,+34.9598+,+32.1389,+34.9597+,+32.1392,+34.9596+,+32.1393,+34.9595+,+32.1395,+34.9594+,+32.1397,+34.9592+,+32.1399,+34.9591+,+32.1400,+34.9589+,+32.1402,+34.9587+,+32.1403,+34.9560+,+32.1421,+34.9560+,+32.1421,+34.9515+,+32.1449,+34.9515+,+32.1449,+34.9513+,+32.1451,+34.9513+,+32.1451,+34.9509+,+32.1453,+34.9509+,+32.1453,+34.9504+,+32.1457,+34.9504+,+32.1457,+34.9456+,+32.1488,+34.9439+,+32.1500,+34.9431+,+32.1505,+34.9431+,+32.1505,+34.9423+,+32.1512,+34.9403+,+32.1527,+34.9399+,+32.1531,+34.9371+,+32.1556,+34.9324+,+32.1598,+34.9297+,+32.1622,+34.9297+,+32.1622,+34.9280+,+32.1635,+34.9265+,+32.1645,+34.9254+,+32.1650,+34.9237+,+32.1658,+34.9237+,+32.1658,+34.9237+,+32.1658,+34.9235+,+32.1659,+34.9223+,+32.1663,+34.9213+,+32.1665,+34.9209+,+32.1666,+34.9207+,+32.1667,+34.9196+,+32.1670,+34.9167+,+32.1676,+34.9167+,+32.1676,+34.9146+,+32.1681,+34.9124+,+32.1686,+34.9121+,+32.1686,+34.9112+,+32.1688,+34.9101+,+32.1689,+34.9089+,+32.1690,+34.9065+,+32.1693,+34.9056+,+32.1694,+34.9045+,+32.1695,+34.9038+,+32.1697,+34.9032+,+32.1698,+34.9031+,+32.1698,+34.9025+,+32.1700,+34.9022+,+32.1701,+34.9018+,+32.1702,+34.9015+,+32.1703,+34.9002+,+32.1708,+34.8985+,+32.1714,+34.8985+,+32.1714,+34.8982+,+32.1714,+34.8981+,+32.1714,+34.8977+,+32.1714,+34.8975+,+32.1714,+34.8972+,+32.1714,+34.8970+,+32.1714,+34.8963+,+32.1716,+34.8938+,+32.1721,+34.8935+,+32.1722,+34.8935+,+32.1722,+34.8935+,+32.1723,+34.8935+,+32.1723,+34.8935+,+32.1727,+34.8935+,+32.1727,+34.8935+,+32.1728,+34.8935+,+32.1729,+34.8935+,+32.1729,+34.8935+,+32.1730,+34.8934+,+32.1732,+34.8933+,+32.1734,+34.8933+,+32.1734,+34.8922+,+32.1744,+34.8922+,+32.1744,+34.8913+,+32.1753,+34.8913+,+32.1753,+34.8906+,+32.1761,+34.8906+,+32.1761,+34.8903+,+32.1763,+34.8899+,+32.1769,+34.8896+,+32.1772,+34.8896+,+32.1772,+34.8891+,+32.1775,+34.8887+,+32.1778,+34.8886+,+32.1781,+34.8886+,+32.1781,+34.8891+,+32.1804,+34.8891+,+32.1804,+34.8893+,+32.1812,+34.8893+,+32.1812,+34.8900+,+32.1846,+34.8900+,+32.1846,+34.8902+,+32.1853,+34.8902+,+32.1856,+34.8902+,+32.1857,+34.8898+,+32.1886,+34.8898+,+32.1886,+34.8896+,+32.1896,+34.8896+,+32.1896,+34.8893+,+32.1912,+34.8893+,+32.1914,+34.8892+,+32.1916,+34.8892+,+32.1917,+34.8893+,+32.1919,+34.8893+,+32.1920,+34.8894+,+32.1920,+34.8895+,+32.1921,+34.8896+,+32.1922,+34.8897+,+32.1922,+34.8899+,+32.1922,+34.8900+,+32.1922,+34.8902+,+32.1922,+34.8903+,+32.1921,+34.8903+,+32.1920,+34.8904+,+32.1919,+34.8904+,+32.1918,+34.8905+,+32.1917,+34.8905+,+32.1916,+34.8904+,+32.1912,+34.8904+,+32.1912,+34.8898+,+32.1911,+34.8898+,+32.1911,+34.8891+,+32.1911,+34.8891+,+32.1911,+34.8885+,+32.1910,+34.8872+,+32.1912,+34.8872+,+32.1912,+34.8858+,+32.1913,+34.8858+,+32.1913,+34.8848+,+32.1914,+34.8844+,+32.1915,+34.8844+,+32.1915,+34.8838+,+32.1916,+34.8838+,+32.1916,+34.8820+,+32.1920,+34.8820+,+32.1920,+34.8811+,+32.1922,+34.8811+,+32.1922,+34.8810+,+32.1925,+34.8810+,+32.1928,+34.8810+,+32.1928,+34.8808+,+32.1934,+34.8808+,+32.1934,+34.8807+,+32.1940,+34.8807+,+32.1940,+34.8805+,+32.1953,+34.8805+,+32.1953,+34.8805+,+32.1958)", how can I remove the + signs ?
In your getCoordsLists() method you are joining the collectors with ", " thats why, after building the url it is adding spaces after every coordinate. so just replace the ", " with "," in the following line: String list = "( " + alternative.coords.stream().map(item -> String.format("%.4f , %.4f", item.x, item.y)) .collect(Collectors.joining(", ")) + ")"; It is happening because default url encoder is replacing the spaces by "+" so that the link will not be broken. You should also replace the last line in the same way, where you are returning the string.
java result set only writing one line instead of all selected into .csv
I have a java function that is meant to take strings from jlist called "readyList" and pulling data from mysql workbench tables with the intent to write a line for each string in a .csv file. With the current code it sucessfully pulls the data one at a time like i intended but it only writes the last line instead of all the lines. I want to have all the lines written in the .csv file. Please help! int[] selectedIx = readyList.getSelectedIndices(); for (int i = 0; i < selectedIx.length; i++) { // while (i < selectedIx.length) { Object sel = readyList.getModel().getElementAt(selectedIx[i]); Statement s1 = DBConnect.connection.createStatement(); String selTable01 = "SELECT Sku As s, Qty As q, Orig_Retail As prce, Orig_Sku As orgsk, Store As strcd " + "FROM completed_lines WHERE Form_Name = '" + sel + "' AND Warranty = 'true'"; s1.execute(selTable01); try (ResultSet rs01 = s1.getResultSet()) { fWriter = new FileWriter("Exports/Transfers/" + /* frmNm.replace(":", "_") */"EBW_" + GtDates.fdate + ".csv", false); writer = new BufferedWriter(fWriter); String header = "slip_number,slip_type,out_store,in_store,item_number,quantity,price,comment,to_num"; writer.write(header); writer.newLine(); while (rs01.next()) { String strcode = rs01.getString("strcd"); String sku = rs01.getString("s"); String qty = rs01.getString("q"); String price = rs01.getString("prce"); String orgsku = rs01.getString("orgsk"); //System.out.println(frmNm.split("_")[1] + qty + "," + sku + "," + vendor + "," + desc1 + "," + reas + "," + descdmg + "," + orgR + "," + nwsku + "," + desc2 + "," + qtyI); String line = ""+","+"out"+","+strcode+","+"RTV"+","+sku+","+qty+","+price+","+"EBW"+","+orgsku; writer.write(line); writer.newLine(); } } // JOptionPane.showMessageDialog(frame, "All Data from Selected Forms has been Exported"); } // FormCompelted(); writer.close(); } }
A few issues with this code. The reason you're only getting the last result is because of this line: fWriter = new FileWriter("Exports/Transfers/" + /* frmNm.replace(":", "_") */"EBW_" + GtDates.fdate + ".csv", false); This line is inside your loop. The false as the last parameter tells FileWriter not to append. In other words, a false means overwrite the file if it exists. Since this is in your loop, each result overwrites the file that the last result created. You should create the FileWriter outside of your loop, probably in a try with resources. That will allow you to remove your writer.close() call, which should have been in a finally block anyway. Not related to your original question but something you should be aware of: You're creating a new Statement with each loop iteration. This can be an expensive operation. You should use a PreparedStatement instead. Create it outside your loop and then just set the parameter and execute it inside the loop. It also implements AutoCloseable, so you can create it in a try with resources too, probably the same one you create your FileWriter in.
Java - Using Jena APi - Get data from RDF file
My question concerns the class Person with the datatype properties hasFirstName, hasLastName, hasDateOfBirth, hasGender. I'm using Java and Jena API. Here is how one person is represented in my RDF file. <rdf:Description rdf:about="http://www.fam.com/FAM#Bruno04/02/1980 "> <j.0:FAMhasGender>H</j.0:FAMhasGender> <j.0:FAMhasDateOfBirth>04/02/1980</j.0:FAMhasDateOfBirth> <j.0:FAMhasLastName>DS </j.0:FAMhasLastName> <j.0:FAMhasFirstName> Bruno</j.0:FAMhasFirstName> </rdf:Description> I want to write this line below if gender is female : [label= \"" +firstName+ " \"\n\n\"D.Naiss:"+dnai1+"\", "+shape2+"] so if there is, for example, 3 females the file must contain 3 lines with that format. The shape value( and then the output line) will depend on the gender, that's why i cannot not use the same line for both genders. Shape2 if female and shape if male. For each person whose gender is male I want to output this line below: [label= \"" +firstName+ " \"\n\n\"D.Naiss:"+dnai1+"\", "+**shape**+"] The problem I have is that he outputs only one woman and one man with the corresponding line. However, I have more than one woman and man in my rdf file. Here is the relevant code. Can you tell me what should I modify to solve this? Thank you very much. public void accessProp() { readFile(inputFile); // rdf String fname; String dd; String gen; ExtendedIterator instances = onto.person.listInstances(); Individual instance = null; Individual firstInstance = null; while (instances.hasNext()) { instance = (Individual) instances.next(); gen = instance.getPropertyValue(onto.hasGender).toString(); fname = instance.getPropertyValue(onto.hasFirstName).toString(); dd = instance.getPropertyValue(onto.hasDateOfBirth).toString(); writeFile(fname, dd, genr); } } // Write text file public void writeFile(String fn, String dbir, String gn) { String fileout = "D:/file1.txt"; String firstName = fn; String dateB = dbir; String gender = gn; BufferedWriter out; try { out = new BufferedWriter(new FileWriter(fileout, true)); if (gender.equals("F")) { out.write("[label= \"" + firstName + " \"\n\n\"D.Naiss:" + dnai1 + "\", " + shape + "]"); } else if (gender.equals("M")) { out.write("[label= \"" + firstName + " \"\n\n\"D.Naiss:" + dnai1 + "\", " + shape2 + "]"); } out.newLine(); // flushes and closes the stream out.close(); } catch (IOException e) { System.out.println("There was a problem:" + e); } }
Without knowing Jena, I do not see any place in your code where you only select the male entries. Check that while (instances.hasNext()) { loop to see what instances it loops through. Because you write for each of that instances a line, the writeLine() method writes both, male and female entries, it might be that ExtendedIterator instances = onto.person.listInstances(); returns the two male and female entries you see in your file. Also, your example RDF entry has a value of H for gender, but in your code you are using M and Fto check it.