database - Connect to database

GUI Alternatives

Connect to databases using Visual Query Builder. For more information on Visual Query Builder, see Using Visual Query Builder.

Syntax

conn = database('datasourcename','username','password')
conn = database('databasename','username',...
'password','driver','databaseurl')

Description

conn = database('datasourcename','username','password') connects a MATLAB® software session to a database via an ODBC driver and assigns the returned connection object to conn. The arguments passed to this function are as follows:

conn = database('databasename','username',...
'password','driver','databaseurl')
connects a MATLAB software session to a database and assigns the returned connection object to conn. The arguments passed to this function are as follows:

If database establishes a database connection, it returns information about the connection object, as shown in the following example:

       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'

Examples

Example 1 — Establish an ODBC Connection

Connect to an ODBC data source called Pricing, specifying user name mike, and password bravo.

conn = database('Pricing', 'mike', 'bravo');

Example 2 — Establish an ODBC Connection without Specifying a User Name and Password

Connect to an ODBC data source SampleDB where a user name and password are not required to access the database.

conn = database('SampleDB','','');

Example 3 — Establish a JDBC Connection

In this example, the following arguments are passed to the database function:

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.

DatabaseJDBC Driver and Database URL Examples

IBM® Informix®

JDBC driver: com.informix.jdbc.IfxDriver

Database URL: jdbc:informix-sqli://161.144.202.206:3000:
INFORMIXSERVER=stars

MySQL®

JDBC driver: twz1.jdbc.mysql.jdbcMysqlDriver

Database URL: jdbc:z1MySQL://natasha:3306/metrics

JDBC driver: com.mysql.jdbc.Driver

Database URL: jdbc:mysql://devmetrics.mrkps.com/testing

Oracle oci7 drivers

JDBC driver: oracle.jdbc.driver.OracleDriver

Database URL: jdbc:oracle:oci7:@rex

Oracle oci8 drivers

JDBC driver: oracle.jdbc.driver.OracleDriver

Database URL: jdbc:oracle:oci8:@111.222.333.44:1521:

Database URL: jdbc:oracle:oci8:@frug

Oracle thin drivers

JDBC driver: oracle.jdbc.driver.OracleDriver

Database URL: jdbc:oracle:thin:@144.212.123.24:1822:

Oracle 10 connections with JDBC (thin drivers)

JDBC driver: oracle.jdbc.driver.OracleDriver
Database URL: jdbc:oracle:thin: (do not specify the target name and port)

In this example, the target machine on which the database server resides is 144.212.123.24 and the port number is 1822.

PostgreSQL

JDBC driver: org.postgresql.Driver

Database URL: jdbc:postgresql://masd/MOSE

PostgreSQL with SSL connection

JDBC driver: org.postgresql.Driver

Database URL: jdbc:postgresql:servername:dbname:ssl=
true&sslfactory=org.postgresql.ssl.NonValidatingFactory&
(the trailing & is required)

Microsoft® SQL Server™

JDBC driver: com.microsoft.jdbc.sqlserver.SQLServerDriver

Database URL: jdbc:sqlserver://localhost:port;database=databasename

Sybase SQL Server® and Sybase® SQL Anywhere®

JDBC driver: com.sybase.jdbc.SybDriver

Database URL: jdbc:sybase:Tds:yourhostname:yourportnumber/

See Also

close, dmd, exec, fastinsert, get, getdatasources, isconnection, isreadonly, logintimeout, ping, supports, update ,Using Visual Query Builder

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS