Thankyou Ernest for pointing out my errors with the jdbc code (throwing exceptions)
I tried to get a string value from jess and return it back to jess but I get a no such variable text message when I run the jess program - I am not sure why. I wanted to test this before I try to send things to and retrieve from the database.
Working with string value:
public class test2 implements Userfunction
{
public String getName () {return "compute";}
public Value call(ValueVector vv, Context c)throws JessException
{
Value abc = vv.get(2).resolveValue(c);
String result = abc.stringValue(c);
Value res = new Value(result, RU.STRING);
c.setVariable(vv.get(1).variableValue(c), res);
return res;
}
}
jess code:
(compute ?arg1 ?text) {note: ?text holds a value selected from a combobax}
(printout t ?arg1)
I tried implementating a Userfunction to insert strings into the database and though both jess program and the userfuntion don't give errors when compiled individally nothing gets added to the database. I am not sure if the problem is getting the database to connect through a userfunction or whether it is becasue of the way I retrieve strings from jess. I've given the jdbc code again. Connecting to the database through an ordinary java program work fine whereby i type in the the values to be inserted.
import jess.*;
import java.sql.*;
public class Insert implements Userfunction
{
public String getName () {return "insert";}
public Value call(ValueVector vv, Context c)throws JessException
{
Connection con = null;
Statement stmt = null;
try
{
String url = "";
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url,"","");
stmt = con.createStatement();
Value abc = vv.get(1).resolveValue(c);
Value def = vv.get(2).resolveValue(c);
String ABC = abc.stringValue(c);
String DEF = def.stringValue(c);
stmt.executeUpdate("INSERT INTO table1 ( field1, field2)"+
"VALUES ('"+ABC+"','"+DEF+"')");
}
catch (SQLException e){throw new JessException ("insert", "SQL Error:"+e.getMessage(),e);
}
catch (Exception e) {}
finally
{
if (con !=null)
{
try
{
con.close();
stmt.close();
}
catch (Exception e){}
}
}
return Funcall.TRUE;
}
}
calling the insert function in jess:
(insert arg1 arg2)
Hope all this isn't too confusing.
Thanks in advance to Earnest and others for suggestions.
Jenny
Do you Yahoo!?
Yahoo! Small Business - Try our new resources site!