问题描述
我正在尝试从Mac/Linux上的R连接到Microsoft SQL Server,并且RJDBC出现问题.当我同时下载了Microsoft的驱动程序和JTDS之后,以下行均无效:
I'm trying to connect to a Microsoft SQL Server from R on Mac/Linux, and I have problems with RJDBC. When I've downloaded both Microsoft's driver and JTDS, but none of the following lines work:
library(RJDBC)
drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver",
"/Users/victor/Downloads/sqljdbc_3.0/enu/sqljdbc4.jar")
drv1 <- JDBC('net.sourceforge.jtds.jdbc.Driver',
"/Users/victor/Downloads/jtds-1/jtds-1.3.0.jar")
每个人都返回以下错误消息:
Each one returns the following error message:
Error in .jfindClass(as.character(driverClass)[1]) : class not found
我怀疑问题可能出在Java版本错误:我的默认Java是
I suspect the issue may be with the wrong Java version: my default java is
$ java -version
java version "1.7.0_11"
Java(TM) SE Runtime Environment (build 1.7.0_11-b21)
Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)
但是我的机器上也安装了Java 1.6.我已经安装了SquirrelSQL,并且使用JTDS驱动程序可以毫无问题地连接到MS SQL Server. SquirrelSQL运行的Java版本是1.7.0.11.
but I also have Java 1.6 installed on my machine. I have SquirrelSQL installed and it connects to MS SQL Server without any problem using JTDS driver; the Java version that SquirrelSQL runs under is 1.7.0.11.
我已经运行R CMD javareconf
,结果如下:
I have run R CMD javareconf
with the following result:
$ R CMD javareconf
Java interpreter : /usr/bin/java
Java version : 1.7.0_11
Java home path : /Library/Java/JavaVirtualMachines/jdk1.7.0_11.jdk/Contents/Home/jre
Java compiler : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/bin/jar
Java library path:
JNI linker flags : -framework JavaVM
JNI cpp flags : -I$(JAVA_HOME)/include
Updating Java configuration in /Library/Frameworks/R.framework/Resources
Done.
并再次删除/安装了RJDBC和rJava包,但仍然无济于事.
and removed/installed again RJDBC and rJava packages, and still nothing works.
我想我现在很困,因为我对Java/RJDBC及其交互并不十分熟悉.在Google中搜索时发现有几个人遇到类似的问题,但没有解决方法.
I guess I'm stuck right now, as I'm not very familiar with Java/RJDBC and their interaction. Searching google found several people with similar problems but no solution.
任何有关如何使JDBC行为或从R连接到MS SQL Server的任何其他方式的技巧都将受到高度赞赏!
Any tips on how to make JDBC behave, or any other way to connect to MS SQL Server from R, will be highly appreciated!
更新1 .好吧,第一条语句现在似乎可以正常工作-我正在建立连接并且可以查询数据库而没有任何问题.不知道是什么解决了这个问题-我可能需要重新启动mac/R会话.第二条语句仍然不起作用,并显示相同的错误消息.
UPDATE 1. Well, the first statement seems to work now - I'm getting a connection and can query the database without any problems. Not sure what fixed the problem - may be I needed to restart my mac/R session. The second statement still doesn't work, with the same error message.
推荐答案
我已经为此苦苦挣扎了一段时间.这就是我发现的东西.
I've been struggling for a while on this. Here's what I found.
- 从此处下载- -用于SQL Server的Microsoft JDBC驱动程序
- 解压缩该文件,您将在其中找到
sqljdbc4.jar
. -
使用:
- Download from here -- Microsoft JDBC driver for SQL server
- Unzip the file, where you will find
sqljdbc4.jar
. Use:
drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver", "<wherever sqljdbc4.jar is>")
这应该有效.
如果我是对的,那么我们的想法是对于drv
函数中的PATH
变量,将需要指定JDBC驱动程序所在的PATH(因此,如果没有,则首先下载它).否则,将收到常见的class not find
错误.
If i'm right, the idea is that for the PATH
variable in drv
function, one will need to specify the PATH where the JDBC driver is located (so if none, then downloaded it FIRST). Otherwise, one shall receive the common class not find
error.
这篇关于在Mac/Linux上从R连接到MS SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!