comparison m-toolbox/classes/+utils/@mysql/connect.m @ 2:18e956c96a1b database-connection-manager

Add LTPDADatabaseConnectionManager implementation. Matlab code
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Sun, 04 Dec 2011 21:23:09 +0100
parents
children 977eb37f31cb
comparison
equal deleted inserted replaced
1:2014ba5b353a 2:18e956c96a1b
1 function conn = connect(hostname, database, username, password)
2 % CONNECT Opens a connection to the given database.
3 %
4 % CALL:
5 %
6 % conn = utils.mysql.connect(hostname, database, username, password)
7 %
8 % This function returns a Java object implementing the java.sql.Connection
9 % interface connected to the given database using the provided credentials.
10 % If the connection fails because the given username and password pair is not
11 % accepted by the server an utils:mysql:connect:AccessDenied error is thrown.
12 %
13
14 % informative message
15 import utils.const.*
16 utils.helper.msg(msg.PROC1, 'connection to mysql://%s/%s username=%s', hostname, database, username);
17
18 % connection credential
19 uri = sprintf('jdbc:mysql://%s/%s', hostname, database);
20 db = javaObject('com.mysql.jdbc.Driver');
21 pl = javaObject('java.util.Properties');
22 pl.setProperty(db.USER_PROPERTY_KEY, username);
23 pl.setProperty(db.PASSWORD_PROPERTY_KEY, password);
24
25 try
26 % connect
27 conn = db.connect(uri, pl);
28 catch ex
29 % haven't decided yet if this code should be here or higher in the stack
30 if strcmp(ex.identifier, 'MATLAB:Java:GenericException')
31 % exceptions handling in matlab sucks
32 if ~isempty(strfind(ex.message, 'java.sql.SQLException: Access denied'))
33 throw(MException('utils:mysql:connect:AccessDenied', '### access denied').addCause(ex));
34 end
35 end
36 rethrow(ex);
37 end
38 end