使用JDBC驱动程序连接MATLAB和MySQL [英] Connecting MATLAB and MySQL with the JDBC Driver
问题描述
我买了Yair Altmam的书未记载的MATLAB;在本书的第2.2章中,他讨论了数据库连接并使用JDBC连接到数据库。我按照书中的步骤和文字。我下载了mysql-connector-java-5.1.30-bin.jar(来自 http:// dev.mysql.com/downloads/connector/j/ )并输入以下代码,详见本书:
I bought the book Undocumented MATLAB by Yair Altmam; in chapter 2.2 of the book he discusses database connectivity and using the JDBC to connect to databases. I followed the steps and text of the book. I downloaded the mysql-connector-java-5.1.30-bin.jar(from http://dev.mysql.com/downloads/connector/j/) and typed up the following code as detailed in the book:
clear all
%%Initializing JDBC driver
try
import java.sql.DriverManager;
javaclasspath('mysql-connector-java-5.1.30-bin.jar')
driverClassName = 'com.mysql.jdbc.Driver';
try
%This works when the class/JAR is on the static Java classpath
%Note: driver automatically registers with DriverManager
java.lang.Class.forName(driverClassName);
catch
try
%Try loading from the dynamic Java path
classLoader = com.mathworks.jmi.ClassLoaderManager.getClassLoaderManager;
driverClass = classLoader.loadClass(driverClassName);
catch %#ok<*CTCH>
try
%One more attempt, using the system class-loader
classLoader = java.lang.ClassLoader.getSystemClassLoader;
%An alternative, using the MATLAB Main Thread's context
%classLoader =
%java.lang.Thread.currentThread.getContextClassLoader;
driverClass = classLoader.loadClass(driverClassName);
catch
%One final attempt-load directly, like this:
driverClass = eval(driverClassName); %#ok<*NASGU>
%Or like this (if the driver name is known in advance):
driverClass = com.mysql.jdbc.Driver;
end
end
%Now manually register the driver with the DriverManager
%Note: silently fails if driver is not in the static classpath
DriverManager.registerDriver(driverClass.newInstance);
end
%continue with database processing
catch
error(['JDBC driver ' driverClassName ' not found!']);
%do some failover activity
end
%% Connecting to a database
import java.sql.*;
connStr = 'jdbc:mysql://localhost:3306/test';
con = DriverManager.getConnection(connStr,'root','1234');
每次运行代码的尝试都会收到以下错误消息:
Every attempt to run the code I get the following error message:
??? Java exception occurred:
java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost:3306/test
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
Error in ==> undocumentedMATLAB at 45
con = DriverManager.getConnection(connStr,'root','1234');
是否有人遇到此问题或有任何建议可以帮我解决。
Has anyone experienced this problem or have any suggestion that could help me resolve it.
提前致谢。
推荐答案
我的第一个怀疑是你的java类路径。而不是:
My first suspicion is your java class path. Instead of:
javaclasspath('mysql-connector-java-5.1.30-bin.jar')
使用
javaaddpath('C:\full\path\to\mysql-connector-java-5.1.30-bin.jar')
如果这不是问题,让我们跳过 DriverManager
(实际上没什么帮助),看看下面的代码是否有效,(或者它失败的地方)。
If that is not the problem, lets skip the DriverManager
(doesn't really help much) and see if the code below works, (or where it fails).
d = com.mysql.jdbc.Driver;
urlValid = d.acceptsURL('jdbc:mysql://localhost:3306/test'); %Should return true
props = java.util.Properties;
props.put('user','root'); props.put('password','1234');
con = d.connect('jdbc:mysql://localhost:3306/test',props)
DriverManager
构造并没有多大帮助。它似乎旨在允许开发人员加载一堆驱动程序,然后连接到任何支持的数据库,而不知道或关心数据库实现是什么(例如Mysql,Postgresql,Oracle等)我从未见过这是一个有用的功能。我认为(希望?)这个使用不太有利于 DataSource
构造。
The DriverManager
construct doesn't really help much. It seems to be designed to allow a developer to load up a bunch of drivers, and then connect to any supported database without knowing or caring what the DB implementation was (e.g. Mysql, Postgresql, Oracle etc.) I have never seen this as a useful feature. I think (hope?) that this is being used less in favor of a DataSource
construct.
无论如何,如果这是你第一次将Mysql连接到Matlab,你可能最好只使用提供的Driver类进行指导。
Regardless, if this is your first time connecting Mysql to Matlab, you probably are best just directing using the provided Driver class.
这篇关于使用JDBC驱动程序连接MATLAB和MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!