Need to establish database connectivity in drools to get some data as and when required while executing the rules. How do I go about that?
Asked
Active
Viewed 2,702 times
0
-
Use whatever API your DB provides. – laune Jan 16 '18 at 11:27
-
How do I create a connection from drools file? – ankitom Jan 16 '18 at 11:40
-
Same was as you do it from Java. – laune Jan 16 '18 at 14:15
-
You can write your own function in drl file to connect with DB – Abhijit Humbe Jan 17 '18 at 02:26
-
@AbhijitHumbe It'll be great if you can share a sample code for connecting DB from drl file. – ankitom Jan 17 '18 at 04:55
-
Does this answer your question? [Need to establish Database connectivity in DRL file](https://stackoverflow.com/questions/67498424/need-to-establish-database-connectivity-in-drl-file) – bjalexmdd Oct 05 '21 at 16:37
2 Answers
0
I have tried to establish Cassandra db connection from Drools file, It's working for me.
import com.datastax.driver.core.Cluster
import com.datastax.driver.core.Session
import com.datastax.driver.core.ResultSet;
function String ConnectDBase(String query) {
Session session;
Cluster cluster;
Cluster.Builder builder = Cluster.builder().
withoutJMXReporting().
addContactPoints("127.0.0.1");
cluster = builder.build();
session = cluster.connect("droolstest");
System.out.println("---------Execute Creation query-----------");
session.execute(query);
return "Table Created";
}
rule "DB Connectio
n rule"
when
//Write your code
then
String query = "CREATE TABLE emp(emp_id int PRIMARY KEY, "
+ "emp_name text, "
+ "emp_city text, "
+ "emp_sal varint, "
+ "emp_phone varint );";
System.out.println(ConnectDBase(query));
end

Arul A M
- 31
- 7
-
-
You can directly add the dependencies with below format, `import com.datastax.driver.core.Cluster` – Arul A M Jan 14 '22 at 02:57
-1
Drools function will look like as:
function String ConnectDB(String ConnectionClass,String url,String user, String password) {
Class.forName(ConnectionClass);
java.sql.Connection con = DriverManager.getConnection(url, user, password);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from Employee where employee_id=199");
rs.first(); return rs.getString("employee_name");
}
Take a look at sample project https://github.com/abhijithumbe/jbpm6Examples/tree/master/Drools_DBConnection

Abhijit Humbe
- 1,563
- 1
- 12
- 13
-
-
@Abhijit, do you know how to import data from excel into drl file. Here's the question: [link](https://stackoverflow.com/q/48418609/3628540) – ankitom Jan 24 '18 at 09:21