insert value_from_field in database

Good Afternoon,

i would like to know how i could retrieve data from specefic filed and insert to database.

Morespecifically,i used to Bonita 5.9 and mysqlserever(wampserver).I have two field in bonita_proccess_step

-------------------------------------------form1------------------------

Username:(field_username)

Name:(filed_name)

              submit(button)

----------------------------------------------------------------------------

i want to take data from two fields(username and name) and insert to database.

When i create a connector,i use this code:

import com.mysql.jdbc.Driver;
import groovy.sql.Sql;
//import java.text.SimpleDateFormat;

"INSERT INTO  alphabank.customers(username,name)VALUES('?1','?2');" 
What i shoud write ?1 and ?2 for insert the value from two filed??(when i wrote for example $filed_name,it appeared error)

Thank in advance

1 Like

Hi

I am going to exaplin how you reach your goal step-by-step

  1. create two process variables: username and name. Both are text variable
  2. add a new human task. Let me call it "Fill data"
  3. add a form on this human task
  4. add two input text widget field ad bind these two widget to your process data (save to...)
  5. add an automatic task after and add the db connector with the sql to insert a new row in databs
INSERT INTO utenti (username,name) VALUES( '${username}','${name}');

 

You can have same behaviour if you use task data variables instead process data variables and if you put the JDBC connector into the human task triggered at end event.

If you have a SP versione and not community you can use dynamic form features those allow you to execute action basing on event on another widget. But because those features are not available for community and because the right way to save date is after submitted form, let me skip that exaplanation.

Hope that can help you

Domenico.

Thank u for answer....:)

But i have a problem with code in sql.When i wrote

"INSERT INTO alphabank.customers(username,name)VALUES('${username}','${name}')";

The Groovy appears Error:

=============================================================================

org.ow2.bonita.util.GroovyException: Error in Groovy script: unable to use element "username"

Possible cause:
- missing import
- variable not found (wrong name, undefined)
- ...

Script:
"import com.mysql.jdbc.Driver;
import groovy.sql.Sql;
import java.text.SimpleDateFormat;
//"INSERT INTO 'alphabank'.'customers'('username','name')VALUES(${name},'vlemmenaki5')";
"INSERT INTO alphabank.customers(username,name)VALUES('${username}','${name}');""
 cause:
No such property: username for class: Script1
groovy.lang.MissingPropertyException: No such property: username for class: Script1
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
    at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
    at Script1.run(Script1.groovy:6)............................

...................................................................................

============================================================================

How can i correct the error??Should change any to sal-code??

Thank again

Robogiannaki Katerina

hola entiendo que se guarda bd h2 pero yo lo quiero guardar en otra bd sql
tengo dos tareas .la primera su BDM tiene datos: nombre ,fecha ,observación , aprobado que es tipo boolen
OK en la segunda tarea en su contrato solo cree una variable tipo boolean asi que cuando genero mi formulario lo que hago es solo llamr a BDM de la primera tarea . arrastro dos botones aprobado y rechazado en cada uno ensu propiedad de valor pongo {“aprobado” : true} ,{“aprobado” : false}
forma 1 usando el editor consulta o lotes de secuencia de comando
cuando lo hago de esta forma me inserta bien pero no esta reconociendo cuando es aprobado o rechazado ya que por defecto le estoy poniendo uno lo que quiero es que cuando se rechaze en mi base datos se ponga 0
insert into aprobacion(usuario,fecha,estado,numeroCaso,numeroTarea)
values(‘elmner’,getdate(),1,${validar.numeroCaso},${validar.numeroTarea})

forma 2 si hago clic cambiar editor y uso script si me acepta y me inserta en BD como quiero pero no me reconoce ‘${validar.numeroCaso}’

if (aprobado ==true){
“insert into aprobacion(usuario,fecha,estado,numeroCaso,numeroTarea) values(‘elmner’,getdate(),1,${validar.numeroCaso},${validar.numeroTarea})”
}
else if (aprobado ==false)
{
“insert into aprobacion(usuario,fecha,estado,numeroCaso,numeroTarea) values(‘elmner’,getdate(),0,${validar.numeroCaso},${validar.numeroTarea})”
}

me pueden orientar

Hi Keterine.

If I've understood weel you're using a Groovy script connector instead a JDBC connector. Why? Anyway I am sending to you the whole Groovy script to save the same information in the same table...

import providedscripts.BonitaSql;

sql = BonitaSql.newInstance("jdbc:postgresql://localhost:5432/your_database_name","postgres", "giordano", new org.postgresql.Driver());
sql.execute("INSERT INTO utenti (username,name) VALUES ('" + username + "','" + name + "')");

you have to put this code in a Groovy Script connector. "username" e "name" are process data.

Let me know.