Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
import org.joget.apps.form.model.*;
import org.joget.apps.form.service.*;
import java.sql.*;
import org.apache.commons.collections.SequencedHashMap;
import java.util.*;

public FormRowSet test() {
	FormRowSet f = new FormRowSet();
        f.setMultiRow(true);
 
//Get Joget's current datasource configs
DataSource ds = (DataSource)  ClassAppUtil.forName("com.mysql.jdbc.Driver"getApplicationContext().newInstancegetBean("setupDataSource");
 
	con = DriverManagerds.getConnection("jdbc:mysql://localhost:3307/jwdb?characterEncoding=UTF-8", "database username", "database password");
 
	if (!con.isClosed()) {
		String recordId = "#requestParam.id#"; // Get the urlURL parameter
		    String sqlrecordId = "SELECT * FROM test_table WHERE id=?";"#requestParam.id#";
 
    // Here you can query from one or multiple tables using JOIN etc
		    String sql = "SELECT * FROM your_table_name WHERE id=?";
    PreparedStatement stmt = con.prepareStatement(sql);
		    stmt.setString(1, recordId);
		
 
    //Execute the SELECT SQL statement
    ResultSet rs = stmt.executeQuery();
		
         
    //Get value from columns of record(s)
    while (rs.next()) {
		        FormRow r1 = new FormRow();
			        r1.put("gridColumn1", rs.getString(1));
			        r1.put("gridColumn1gridColumn2", rs.getString(2));
			        r1.put("gridColumn1gridColumn3", rs.getString(3));
			        f.add(r1);
	    }
	}
	return f;
}
return test()f;

存储绑定器 -> Bean Shell 表单绑定器

Code Block
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.*Form;
import org.joget.apps.form.libmodel.*Element;
import org.joget.apps.form.servicemodel.*FormData;
import java.sql.*org.joget.apps.form.model.FormRow;
import org.apachejoget.apps.commonsform.collectionsmodel.SequencedHashMapFormRowSet;
import java.util.*org.joget.apps.form.service.FormUtil;
import org.joget.commons.util.UuidGenerator;
 
public saveGridRows(Element element, FormRowSet getGridRows( rows, FormData formData) {
	return rows ; // this will return the grid rows
}

public saveGridRows(FormRowSet rows) {
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	con = DriverManager.getConnection("jdbc:mysql://localhost:3307/jwdb?characterEncoding=UTF-8", "database username", "database password");
	
     
    String recordId = null;
    Connection con = null;
     
    try {
        //Get Joget's current datasource configs
        DataSource ds = (DataSource) AppUtil.getApplicationContext().getBean("setupDataSource");
 
        con = ds.getConnection();
         
        if(!con.isClosed()) {

		String recordId = "#requestParam.id#";

		// If you need to query data from different table(s) and process it before saving it to database,
		// you can do it here


		            //To generate new record IDs for storing into child table
            UuidGenerator uuid = UuidGenerator.getInstance();

		             
            //Iterate to add new records
            Iterator i= rows.iterator(); // Iterating grid rows
		
            while (i.hasNext()) {
			                FormRow row = (FormRow) i.next();
                 
                String pId = uuid.getUuid();
			                String gridColumn1 = row.get("gridColumn1"); // reading grid column value
			
                String gridColumn2 = row.get("gridColumn2");
			                String gridColumn3 = row.get("gridColumn3");
			String pId = uuid.getUuid(); // generating Primary Key
			                 
                String insertSql = "INSERT INTO your_table_name (id,col1gridColumn1,col2gridColumn2,col3gridColumn3) VALUES (?,?,?,?);";
			
                 
                PreparedStatement stmtInsert = con.prepareStatement(insertSql);
			
                 
                stmtInsert.setString(1, pId);
			                stmtInsert.setString(2, gridColumn1);
			                stmtInsert.setString(3, gridColumn2);
			                stmtInsert.setString(4, gridColumn3);
			                 
                //Execute SQL statement
                stmtInsert.executeUpdate();
		}
	} else {
		System.out.println("Connection Problem");
	}
	
            }
        }
    } catch (Exception ex) {
        LogUtil.error("Your App/Plugin Name", ex, "Error storing using jdbc");
    } finally {
        try {
            if (con != null) {
                con.close();
            }

FormRowSet rows = getGridRows(); // getting the grid rows
saveGridRows(rows); // processing & storing the grid rows        } catch (Exception ex) {
            LogUtil.error("Your App/Plugin Name", ex, "Error closing the jdbc connection");
        }
    }
}
 
//Process and store grid rows
saveGridRows(element, rows, formData);


表格设计

正确的表格设计应如下所示:

 

部分load binder beanshell脚本:

Code Block
languagejava
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
 
FormRowSet f = new FormRowSet();
f.setMultiRow(true);
 
FormRow r1 = new FormRow();
r1.put("gridColumn1", your_value);
r1.put("gridColumn2", your_value);
r1.put("gridColumn3", your_value);
f.add(r1);
 
FormRow r2 = new FormRow();
r2.put("gridColumn1", your_value);
r2.put("gridColumn2", your_value);
r2.put("gridColumn3", your_value);
f.add(r2);
  
return f;