Skip to content

Instantly share code, notes, and snippets.

@codegordi
Created May 31, 2013 18:07
Show Gist options
  • Save codegordi/5686806 to your computer and use it in GitHub Desktop.
Save codegordi/5686806 to your computer and use it in GitHub Desktop.
Example using RJDBC to connect to a MS SQL Server database (R on MacOS)
# JDBC driver library for R
library("RJDBC")
user <- "cgutierrez" # enter your own username
#pwd <- # enter in URL str below
# I chose open-source JTDS for a particular reason in this example (not related to R); you might choose another one
# Check that appropriate .jar is in indicated directory
drv <- JDBC("net.sourceforge.jtds.jdbc.Driver", "/usr/share/java/drivers/jdbc/jtds-1.2.7.jar", identifier.quote="`")
cxn <- dbConnect(drv, "jdbc:jtds:sqlserver://MYSERVER01ABC;useNTLMv2=true;domain=MY.DOMAIN.COM", user, #####) # enter pwd at runtime
#list tables in DB connected to :
dbListTables(cxn)
# SQL query str e.g.
dbGetQuery(cxn, "use MyDataBase select distinct UID from MyTable where (UID like '%ABC%' or UID like '%XYZ%') order by UID")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment