view m-toolbox/classes/+utils/@mysql/connect.m @ 6:2b57573b11c7
database-connection-manager
Add utils.mysql.execute
author
Daniele Nicolodi <nicolodi@science.unitn.it>
date
Mon, 05 Dec 2011 16:20:06 +0100 (2011-12-05)
parents
18e956c96a1b
children
977eb37f31cb
line source
+ − function conn = connect(hostname, database, username, password)
+ − % CONNECT Opens a connection to the given database.
+ − %
+ − % CALL:
+ − %
+ − % conn = utils.mysql.connect(hostname, database, username, password)
+ − %
+ − % This function returns a Java object implementing the java.sql.Connection
+ − % interface connected to the given database using the provided credentials.
+ − % If the connection fails because the given username and password pair is not
+ − % accepted by the server an utils:mysql:connect:AccessDenied error is thrown.
+ − %
+ −
+ − % informative message
+ − import utils.const.*
+ − utils.helper.msg(msg.PROC1, 'connection to mysql://%s/%s username=%s', hostname, database, username);
+ −
+ − % connection credential
+ − uri = sprintf('jdbc:mysql://%s/%s', hostname, database);
+ − db = javaObject('com.mysql.jdbc.Driver');
+ − pl = javaObject('java.util.Properties');
+ − pl.setProperty(db.USER_PROPERTY_KEY, username);
+ − pl.setProperty(db.PASSWORD_PROPERTY_KEY, password);
+ −
+ − try
+ − % connect
+ − conn = db.connect(uri, pl);
+ − catch ex
+ − % haven't decided yet if this code should be here or higher in the stack
+ − if strcmp(ex.identifier, 'MATLAB:Java:GenericException')
+ − % exceptions handling in matlab sucks
+ − if ~isempty(strfind(ex.message, 'java.sql.SQLException: Access denied'))
+ − throw(MException('utils:mysql:connect:AccessDenied', '### access denied').addCause(ex));
+ − end
+ − end
+ − rethrow(ex);
+ − end
+ − end