Why the getMethod for List<String> is not printing out the values in another Method?
I have been dealing with this problem for the longest time and I really hope someone can tell me what's happening and how to make the getMethod for List<String> returns the values.
Code:
public void insertTutor_Subject(int tutor_id, List<String> subjNames) throws MyDataException {
Tutor m = new Tutor();
System.out.println(tutor_id); //this will return the generated_id
System.out.println(m.getSubjects());//not able to print out
try {
//openConnection();
PreparedStatement ps3 = connection.prepareStatement("INSERT INTO project.tutor_subject(tutor_id, subjectName) VALUES (?, ?);");
ps3.setInt(1, tutor_id);
//System.out.println(m.getTutor_id()); //m.getTutor_id() will give you 0
ps3.setObject(2, m.getSubjects());
System.out.println(m.getSubjects());
ps3.addBatch();
ps3.setInt(1, tutor_id);
ps3.setObject(2, m.getSubjects());
ps3.addBatch();
At my controller :
Code:
String[] subj = request.getParameterValues("subject");
List<String>subjs = new ArrayList<String>(Arrays.asList(subj));
m4.setSubjects(subjs);
System.out.println(m4.getSubjects()); // able to see the values
My first question is m4.getSubjects at the controller is able to return the value so why is it not showing up at the insertTutor_Subject method?
My 2nd question is do I still need to OpenConnection(static method for JDBC Connection) in order for the insertion to work?
My last question is if the above - using Batch to insert the List<String> ? Is there anything wrong with this method ?
Thanks
Re: Why the getMethod for List<String> is not printing out the values in another Meth
Can you make and post a small complete program that compiles, executes and shows the problem?
Please wrap all posted code in code tags to preserve formatting.
Re: Why the getMethod for List<String> is not printing out the values in another Meth
The reason is that in the following method you are declaring a new Tutor object, but aren't populating it.
Code:
public void insertTutor_Subject(int tutor_id, List<String> subjNames) throws MyDataException {
Tutor m = new Tutor();
System.out.println(tutor_id); //this will return the generated_id
System.out.println(m.getSubjects());//not able to print out -- YES, because it's empty
The following works because you call m4.setSubjects (presumably m4 is an instance of the Tutor object).
Code:
String[] subj = request.getParameterValues("subject");
List<String>subjs = new ArrayList<String>(Arrays.asList(subj));
m4.setSubjects(subjs); // SETTING the VALUES
System.out.println(m4.getSubjects()); // able to see the values
Do you have to call setSubjects() first?
Code:
public void insertTutor_Subject(int tutor_id, List<String> subjNames) throws MyDataException {
Tutor m = new Tutor();
System.out.println(tutor_id);
m.setSubjects(subjNames);
System.out.println(m.getSubjects());