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