需要在drools中建立数据库连接,以便在执行规则时根据需要获取一些数据。我该怎么做?
答案 0 :(得分:0)
我试图从Drools文件建立Cassandra db连接,它对我有用。
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
答案 1 :(得分:-1)
Drools功能看起来像:
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");
}
查看示例项目https://github.com/abhijithumbe/jbpm6Examples/tree/master/Drools_DBConnection