while (rs.next) not returning all values saved in DB

Confluence User - 24 Jun, 2016

Hi all

I would like to call all Teacher names saved in a table against a ID. However the below code returns only one record whereas there are 3 records against one id. Please help

 

import org.joget.apps.app.service.*;
import org.joget.apps.form.model.*;
import org.joget.apps.form.service.*;
import java.sql.*;
import java.util.*;

public FormRowSet getData() {

FormRowSet results = null;

if (results == null) {
results = new FormRowSet();

Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://127.5.232.123:3306/jwdb?characterEncoding=UTF-8", "", ");

if(!con.isClosed()){
String pId = "#form.MLA_Main.studentID#";
String sql = "SELECT c_instructors FROM app_fd_Std_Ins WHERE c_studentId = ?";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, pId);

ResultSet rs = stmt.executeQuery();

while (rs.next()) {
FormRow row = new FormRow();
row.put("instructorNames", (rs.getString(1) != null)?rs.getString(1):"");
//row.put("email", (rs.getString(3) != null)?rs.getString(3):"");
results.add(row);

}

}

} catch(Exception ex) {
System.err.println("Exception: " + ex.getMessage());
} finally {
try {
if(con != null)
con.close();
} catch(SQLException e) {}
}
}
//------------------------------------------------------------------------------------

return results;
}

return getData();
database;userviews

5


26 Jun, 2016
confluenceUser
confluenceUser
String sql = "SELECT c_instructors FROM app_fd_Std_Ins WHERE c_studentId = ?";

Your SQL statement only returns one column. Please test it out and change the select query in your SQL client first.

27 Jun, 2016
confluenceUser
confluenceUser

Thank you Bastiana for your response. Yes, i have called one column. But, that column has three rows of data however only one row is returning. Looking forward to hear from you back Thank you again Kamran

28 Jun, 2016
confluenceUser
confluenceUser

Dear Bastiana,

I have run the SQL Select statement on SQL client.

The result has two records what i am expecting however in the Joget app only one one row is returning: I have attached below two screen shots instances for your reference:

 

SQL statement:

 

 

 

Joget App:

 

 

Your response will be highly appreciated. 

 

29 Jun, 2016
confluenceUser
confluenceUser

The form will typically make use of the first item in FormRowSet unless you put these codes into those form elements that expect multiple objects in FormRowSet such as the FormGrid / Grid elements.

04 Jul, 2016
confluenceUser
confluenceUser

Thank you Walter for your response. Can you please elaborate? I have tried by putting these codes into 'Grid' under Bean shell load binder but nothing is returning. Best regards Kamran

RELATED QUESTIONS

Your answer


To answer a question you'll need an account.

Print