1
0
-1

Hi,

This is my code for jdbc load data binder. But i dont know why joget log keep response possible db unclosed. Is there any problem with my source code?

import org.joget.apps.form.model.*;
import org.joget.apps.form.service.*;
import java.sql.*;
import javax.sql.DataSource;
import org.joget.apps.app.service.AppUtil;
import org.apache.commons.collections.SequencedHashMap;
import java.util.*;
 
public FormRowSet getIslas() {
    FormRowSet f = new FormRowSet();
        f.setMultiRow(true);

 //  Connection con = null;
        DataSource ds = (DataSource)AppUtil.getApplicationContext().getBean("setupDataSource");// add this
        con = ds.getConnection();// add this
    if(!con.isClosed()){
        String recordId = "#currentUser.lastName#"; // Get the url parameter
        String sql = "SELECT c_firstName, c_username FROM app_fd_registration WHERE c_username=?"; // Here you can query from one or multiple tables using JOIN etc
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setString(1, recordId);
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
        FormRow r1 = new FormRow();
            
            r1.put("nama_syarikat", rs.getString(1));
            r1.put("id_pemohon", rs.getString(2));

         
            f.add(r1);
       }
    }
    return f;
    con.close();
}
return getIslas();

    CommentAdd your comment...

    1 answer

    1.  
      2
      1
      0

      you pass return first that why is not reach to con.close () linn 

      try this code 

      public FormRowSet getIslas() {
          FormRowSet f = new FormRowSet();
          f.setMultiRow(true);

          Connection con = null; // declare the connection variable
          try {
              DataSource ds = (DataSource) AppUtil.getApplicationContext().getBean("setupDataSource");
              con = ds.getConnection();

              if (!con.isClosed()) {
                  String recordId = "#currentUser.lastName#";
                  String sql = "SELECT c_firstName, c_username FROM app_fd_registration WHERE c_username=?";
                  PreparedStatement stmt = con.prepareStatement(sql);
                  stmt.setString(1, recordId);
                  ResultSet rs = stmt.executeQuery();
                  while (rs.next()) {
                      FormRow r1 = new FormRow();
                      r1.put("nama_syarikat", rs.getString(1));
                      r1.put("id_pemohon", rs.getString(2));
                      f.add(r1);
                  }
              }
          } catch (SQLException e) {
              // Handle SQLException
              e.printStackTrace();
          } finally {
              if (con != null) {
                  try {
                      con.close();
                  } catch (SQLException e) {
                      // Handle SQLException while closing connection
                      e.printStackTrace();
                  }
              }
          }

          return f;
      }

        CommentAdd your comment...