简体   繁体   English

通过JDBC将MATLAB连接到Apache Derby数据库

[英]connecting MATLAB to Apache Derby database via JDBC

I've been asked to connect an Apache Derby database to our existing MATLAB data-mining tools. 我被要求将Apache Derby数据库连接到我们现有的MATLAB数据挖掘工具。 I am running MATLAB R2014a (no Database toolbox) 我正在运行MATLAB R2014a(无数据库工具箱)

After some research and trial and error, here's what I did: 经过研究和反复试验,这是我做的:

went to http://www.java2s.com/Code/Jar/d/Downloadderbyclientjar.htm and downloaded derbyclient.jar and saved it, and unzipped it to a shared folder. 转到http://www.java2s.com/Code/Jar/d/Downloadderbyclientjar.htm并下载derbyclient.jar并保存,然后将其解压缩到共享文件夹中。

edited the text file C:\\Users\\tyler.davis\\AppData\\Roaming\\MathWorks\\MATLAB\\R2014a\\javaclasspath.txt and added full path to derbyclient.jar, saved it, restarted MATLAB. 编辑了文本文件C:\\ Users \\ tyler.davis \\ AppData \\ Roaming \\ MathWorks \\ MATLAB \\ R2014a \\ javaclasspath.txt,并将完整路径添加到derbyclient.jar,将其保存,然后重新启动MATLAB。

At MATLAB command line 在MATLAB命令行

javaclasspath

shows "S:\\SHARED...\\derbyclientjar\\derbyclient.jar\\derbyclient.jar" at the end of the static path list. 在静态路径列表的末尾显示“ S:\\ SHARED ... \\ derbyclientjar \\ derbyclient.jar \\ derbyclient.jar”。 So far so good. 到现在为止还挺好。

Next, try 接下来,尝试

driverClassName = 'org.apache.derby.jdbc.ClientDriver';
java.lang.Class.forName(driverClassName);

reports error: 报告错误:

Java exception occurred:
java.lang.ClassNotFoundException: org/apache/derby/jdbc/ClientDriver

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Unknown Source)

Tried an alternative: 尝试了一种替代方法:

classLoader = com.mathworks.jmi.ClassLoaderManager.getClassLoaderManager;
driverClass = classLoader.loadClass(driverClassName);

That seemed to work, created objects "driverClass, type 1x1 java.lang.Class" and "classLoader, value 1x1 com.mathworks.jmi.ClassLoaderManager" 这似乎可行,创建了对象“ driverClass,类型1x1 java.lang.Class”和“ classLoader,值1x1 com.mathworks.jmi.ClassLoaderManager”

Then I tried 然后我尝试

DriverManager.registerDriver(driverClass.newInstance);

which reports error 哪个报告错误

Undefined variable "DriverManager" or class "DriverManager.registerDriver".

and if I try to create a sample database using 如果我尝试使用创建示例数据库

cxnStr = 'jdbc:derby:sampleDB:create=true';
cxn = java.sql.DriverManager.getConnection(cxnStr);

I get 我懂了

Java exception occurred:
java.sql.SQLException: No suitable driver found for jdbc:derby:sampleDB:create=true

at java.sql.DriverManager.getConnection(Unknown Source)

at java.sql.DriverManager.getConnection(Unknown Source)

I really don't know what I'm doing here; 我真的不知道我在这里做什么。 just copying other's code from around the web. 只是从网上复制他人的代码。 Any suggestions on what to try next? 关于下一步尝试的任何建议?

Since you're just getting started with Derby, can I recommend that you take an hour or two and run through the Derby tutorial: http://db.apache.org/derby/docs/10.11/getstart/cgstutorialintro.html 由于您只是刚开始使用Derby,我是否可以建议您花一两个小时来完成Derby教程的运行: http : //db.apache.org/derby/docs/10.11/getstart/cgstutorialintro.html

Also, can I recommend that you get in the habit of downloading Derby from the Apache website rather than other Internet sites? 另外,我是否可以建议您养成从Apache网站而不是其他Internet网站下载Derby的习惯? The copies of Derby on other sites are probably fine, but it seems most reliable and safest to get Derby from the Apache website: http://db.apache.org/derby/derby_downloads.html 在其他站点上复制Derby可能很好,但是从Apache网站获得Derby似乎是最可靠和最安全的: http : //db.apache.org/derby/derby_downloads.html

Most of what you did in your description seems OK to me; 在我的描述中,您所做的大部分工作对我来说似乎都可以。 I suspect the place where you went astray is when you unzipped derbyclient.jar itself. 我怀疑您误入歧途的地方是当您解压缩derbyclient.jar本身时。

Although a '*.jar' file is in fact a valid ZIP archive, you are not expected to unzip those packaged jar files. 尽管'* .jar'文件实际上是有效的ZIP存档,但您不应将这些打包的jar文件解压缩。 Instead, you should leave the jar as 'derbyclient.jar' and then set your CLASSPATH to include 'derbyclient.jar' as one of the entries in the CLASSPATH list. 相反,您应该将jar保留为“ derbyclient.jar”,然后将CLASSPATH设置为包括“ derbyclient.jar”作为CLASSPATH列表中的条目之一。

Lastly, note that the JDBC Connection URL 最后,请注意JDBC连接URL

jdbc:derby:sampleDB:create=true

is a correct JDBC Connection URL for the embedded configuration of Derby, not for the client-server configuration of Derby. 是Derby 嵌入式配置的正确JDBC连接URL,而不是Derby 客户端-服务器配置的正确JDBC连接URL。

So even if you get a good copy of derbyclient.jar into your CLASSPATH correctly, you won't be able to use 因此,即使您正确地将derbyclient.jar的良好副本复制到CLASSPATH中,也将无法使用

jdbc:derby:sampleDB:create=true

Instead, you'll either have to use a client-server style JDBC Connection URL, such as: 相反,您将不得不使用客户端-服务器样式的JDBC连接URL,例如:

jdbc:derby://my.server.name:1527/sampleDB

(and learn how to operate a Derby Network Server), or you'll need to switch to using the Embedded Driver (org.apache.derby.jdbc.EmbeddedDriver). (并了解如何操作Derby Network Server),或者您需要切换到使用嵌入式驱动程序(org.apache.derby.jdbc.EmbeddedDriver)。

For more information about this notion of configurations, see: http://db.apache.org/derby/docs/10.11/getstart/cgsquck70629.html 有关此配置概念的更多信息,请参见: http : //db.apache.org/derby/docs/10.11/getstart/cgsquck70629.html

And for more information on the JDBC Connection URL and the two JDBC driver classes, see: http://db.apache.org/derby/docs/10.11/getstart/cgsquck19524.html 有关JDBC连接URL和两个JDBC驱动程序类的更多信息,请参见: http : //db.apache.org/derby/docs/10.11/getstart/cgsquck19524.html

Once you've had a chance to work through the Derby Getting Started guide, I recommend asking some follow-on questions based on what you learn during that experience. 在您有机会阅读《 Derby入门指南》之后,我建议您根据在此经验中学到的知识提出一些后续问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM