Database Toolbox |
Syntax
conn = database('datasourcename', 'username', 'password')
conn = database('databasename', 'username', 'password',
'driver','databaseurl')
Description
conn = database('datasourcename', 'username', 'password')
connects a MATLAB session to a database via an ODBC driver, returning the connection object to conn
. The data source to which you are connecting is datasourcename
. You must have previously set up the data source--for instructions, see Setting Up a Data Source. username
and password
are the username and/or password required to connect to the database. If you do not need a username or a password to connect to the database, use empty strings as the arguments. After connecting, use exec
to retrieve data.
conn = database('databasename', 'username', 'password', 'driver',
'databaseurl'
)
connects a MATLAB session to a database, databasename
, via the specified JDBC driver,
returning the connection object to conn
. The username and/or password required to connect to the database are username
and password
. If you do not need a username or a password to connect to the database, use empty strings as the arguments. databaseurl
is the JDBC URL object, jdbc:subprotocol:subname
. The subprotocol
is a database type, such as oracle
. The subname
may contain other information used by driver
, such as the location of the database and/or a port number. The subname
may take the form //hostname:port/databasename
. Find the correct driver
name and databaseurl
format in the driver manufacturer's documentation. Some sample databaseurl
strings are listed in Example 3--Establish JDBC Connection.
If database
establishes a connection, MATLAB returns information about the connection object.
Instance: 'SampleDB' UserName: '' Driver: [] URL: [] Constructor: [1x1 com.mathworks.toolbox.database.databaseConnect] Message: [] Handle: [1x1 sun.jdbc.odbc.JdbcOdbcConnection] TimeOut: 0 AutoCommit: 'off' Type: 'Database Object'
Use logintimeout
before you use database
to specify the maximum amount of time for which database
tries to establish a connection.
You can have multiple database connections open at one time.
After connecting to a database, use the ping
function to view status information about the connection, and use dmd
, get
, and supports
to view properties of conn
.
The database connection stays open until you close it using the close
function. Always close a connection after you finish using it.
Example 1--Establish ODBC Connection
To connect to an ODBC data source called Pricing
, where the database has a user mike
and a password bravo
, type
Example 2--Establish ODBC Connection Without Username and Password
To connect to an ODBC data source SampleDB
, where a username and password are not needed, use empty strings in place of those arguments. Type
Example 3--Establish JDBC Connection
In this JDBC connection example, the database is oracle
, the username is scott
, and the password is tiger
. The oci7 JDBC driver name is oracle.jdbc.driver.OracleDriver
and the URL that specifies the location of the database server is jdbc:oracle:oci7
.
conn = database('oracle','scott',
'tiger',...
'oracle.jdbc.driver.OracleDriver','jdbc:oracle:oci7:');
The JDBC name and URL take different forms for different databases, as shown in the examples in the following table.
For the Oracle thin drivers example, in the database URL jdbc:oracle:thin:@
144.212.123.24:1822, the target machine that the database server resides on is 144.212.123.24, and the port number is 1822
.
For Microsoft SQL Server 2000, you may also need to pass the database name, username, and password via the URL. For example,
conn = database('pubs','sa','sec', 'com.microsoft.jdbc.sqlserver.SQLServerDriver', 'jdbc:microsoft:sqlserver://127.0.0.1:1403; database=pubs;user=sa;password=sec')
See Also
close
, dmd
, exec
, get
, isconnection
, isreadonly
, logintimeout
, ping
,
supports
crossreference | dmd |
© 1994-2005 The MathWorks, Inc.