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