Skip to main content

Jdbc R


# As an alternative to creating a DataTableDataReaderCommand which is in-memory (and easily created from an R DataFrame)
# this example shows how to create a DatabaseDataReaderCommand, which connects to a database via Jdbc.


# TODO update the following paths

jdbcPath <- "C:/Program Files/Microsoft JDBC Driver 4.0 for SQL Server/sqljdbc_4.0/enu/sqljdbc4.jar"
bayesServerPath <- "C:/Program Files/Bayes Server/Bayes Server 7.6/API/Java/bayesserver-7.6.jar"

jarPath <- paste(jdbcPath, bayesServerPath, sep = ";")

library(rJava) #load the rJava library
.jinit(classpath=jarPath)

driver <- "com.microsoft.sqlserver.jdbc.SQLServerDriver"
J("java.lang.Class")$forName(driver)

connectionUrl <- "jdbc:sqlserver://localhost\\sqlexpress;databaseName=DemoData;user=test;password=password-goes-here"

queryText <- "SELECT * FROM Iris"

dataReaderCommand <- new (J("com.bayesserver.data.DatabaseDataReaderCommand"), connectionUrl, queryText)

# the dataReaderCommand created above can be used in many of Bayes Server's data driven methods.