import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.form.model.Element;
import org.joget.apps.form.model.FormData;
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
import org.joget.commons.util.LogUtil;
   
public FormRowSet load(Element element, String primaryKey, FormData formData) {
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    FormRowSet rows = new FormRowSet();
	// 在加载绑定器中,用于返回的FormRowSet 绑定数据,需要将FormRowSet  setMultiRow(true);
    rows.setMultiRow(true);
    try {
        // retrieve connection from the default datasource
        DataSource ds = (DataSource)AppUtil.getApplicationContext().getBean("setupDataSource");
        con = ds.getConnection();
        
        // execute SQL query
        if(!con.isClosed()) {
            String id = "18a8680c-1b6a-4425-b940-14230912db06";
            stmt = con.prepareStatement("SELECT c_requestId,c_code from app_fd_vt_ali_sms_log where id=?");
            stmt.setObject(1, id);
            rs = stmt.executeQuery();
            while (rs.next()) {
                FormRow row = new FormRow();
                row.setProperty("requestId", (rs.getObject("c_requestId") != null)?rs.getObject("c_requestId").toString():"");
                row.setProperty("code", rs.getObject("c_code"));
                rows.add(row);
            }
        }
    } catch(Exception e) {
        LogUtil.error("Sample app - Form 1", e, "Error loading user data in load binder");
    } finally {
        //always close the connection after used
        try {
			if (rs != null) {
				rs.close();
				rs = null;
			}
		} catch (SQLException e) {/* ignored */}
		try {
			if (stmt != null) {
				stmt.close();
				stmt = null;
			}
		} catch (SQLException e) {/* ignored */}
		try {
			if (con != null) {
				con.close();
				con = null;
			}
		} catch (SQLException e) {/* ignored */}
    }
    return rows;
}
	/* 
	element、primaryKey、formData为当前表单的全局属性变量,可根据需要使用,此处并没有使用这些数据,可以在方法中去掉这些参数
		element-此资料夹绑定到的元素。(org.joget.apps.form.model.Element)
		primaryKey-整个表单的中id主键(需要自行在表单中添加id元素)。(java.lang.String)
		formData-整个表单的数据持有人。(org.joget.apps.form.model.FormData)
	*/

//call load method with injected variable
return load(element, primaryKey, formData);

样例应用

APP_BeanShellTest-1-20200109105727.jwa

  • No labels