1
0
-1

Hi all,

I wish to insert values inside custom db with bean shell store binder. I am using the following code.

I am not getting any errors in the log.


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

public void StoreUsingJDBC(Element element, FormRowSet rows, FormData formData, String id) {
Connection con = null;

if (results == null) {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jogettest", "root", "root");
if(!con.isClosed()){
String sql = "Insert into po(poId) values('4040')";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.executeUpdate();
}
} catch (Exception ex) {
System.err.println("Exception: " + ex.getMessage());
} finally {
try {
if(con != null)
con.close();
} catch(SQLException e) {}
}
}

return storeData();

Am I doing something wrong here?

    CommentAdd your comment...

    1 answer

    1.  
      2
      1
      0

      Hi Punam,

      I' using code like below i don't know if this can help but library list and database connection may usefull


      // Add recherche client pour preselection
      import java.sql.Connection;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.text.DecimalFormat;
      import javax.sql.DataSource;
      import org.joget.apps.app.service.AppUtil;
      import org.joget.apps.form.model.FormRow;
      import org.joget.apps.form.model.FormRowSet;
      import org.joget.apps.form.service.FormUtil;
      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 test() {
      FormRowSet frs = new FormRowSet();
      frs.setMultiRow(true);
      try {
      // retrieve connection from the default datasource
      DataSource ds = (DataSource)AppUtil.getApplicationContext().getBean("setupDataSource");
      con = ds.getConnection();
      if(!con.isClosed()){
      String sql = "SELECT SQL  YOUR REQUEST " ;
      PreparedStatement stmt = con.prepareStatement(sql);
      ResultSet rs = stmt.executeQuery();

      /*here my code display some information from database but begining may help you */



      }
      }
      } catch(Exception e) {
      System.err.println("------------------>Beanshell Error");
      System.err.println("------------------>Exception in form [FormName]>[ElementName]: " + e.getMessage());
      System.err.println("------------------>Beanshell Error");
      } finally {
      try {
      if(con != null) con.close();
      } catch(SQLException e) {}
      }
      return frs;
      }
      return test();

      1. Punam

        Thanks phillippe,

        Is it possible to store multiple rows in a text field?

      2. Philippe GOBARDHAN

        Hello Punam,

        I'm using listForm in a web page, so based on this you can adapt to Text Field.

        below code must be under :ResultSet rs = stmt.executeQuery();



        FormRow r1;
        r1 = new FormRow();
        r1.put(FormUtil.PROPERTY_VALUE, "");
        r1.put(FormUtil.PROPERTY_LABEL, "---Select---");

        frs.add(r1);

        while (rs.next()) {

        r1 = new FormRow();


        r1.put(FormUtil.PROPERTY_VALUE, (rs.getString("DatabaseField1")+"*"+ rs.getString("DatabaseField2")+"*"+ rs.getString("DatabaseField3")+"*"+ rs.getString("DatabaseField4")+"*"+ rs.getString("DatabaseField5") ) );



        r1.put(FormUtil.PROPERTY_LABEL, ( rs.getString("c_SI_Client_Name") ));



        frs.add(r1);
        }


        Hope this will help

      CommentAdd your comment...