Question

I I have JList and JList model that contains values of database fields (student table).

I want to get all the data form the JList and store the data in String variable call mySql

I want the data to be stored like this: -"SELECT " - then the data from jList separated by comma -" FROM"

How can I do this?

This is what I did so far:

ListModel a = myList.getModel();
String obj = null;
String obj2 = null;
System.out.print("SELECT ");

for (int i = 0; i < a.getSize(); i++) {
  if (i == a.getSize() - 1) {
    obj = (String) a.getElementAt(i);
    System.out.print(obj);
  } else {
    obj2 = a.getElementAt(i) + ",";
    System.out.print(obj2);
  }
}
System.out.print(" FROM ");

I changed the code to this

    ListModel a = list2.getModel();
    String obj = null;
    String obj2 = null;
  //  System.out.print("SELECT ");

    for (int i = 0; i < a.getSize(); i++) {
        if (i == a.getSize() - 1) {
            obj = (String) a.getElementAt(i);
        //    System.out.print(obj);
        } else {
            obj2 = a.getElementAt(i) + ",";
         //   System.out.print(obj2);        
        }
                    System.err.println("This is obj2 "+obj2 +"This is obj "+ obj);        


    }
   // System.out.print("SELECT "+obj2+obj+" FROM ");

No correct solution

OTHER TIPS

Have you tried adding them to a string then outputting that at the end? So some thing like String text = "SELECT " then add the other strings on by text =+ "whatever"

When you want to print the "SELECT ele,ele,ele,ele FROM" String out, then the following could be what you want:

ListModel a = myList.getModel();
System.out.print("SELECT ");
for (int i = 0; i < a.getSize(); i++) {
  System.out.print(a.getElementAt(i);
  if (i < a.getSize() - 1) {
    System.out.print(",");
  }
}
System.out.print(" FROM ");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top