OpenConnection - Maple Help

Driver

 OpenConnection
 opens a connection to a database

Parameters

 driver - Driver module url - string; specifying the database username - string; username for logging into the database password - string or name; password to for logging into the database, 'none', or 'hidden' opts - (optional) equations of the form option=value where option is one of readonly or isolation

Description

 • OpenConnection opens a connection to a database using the JDBC Driver represented by driver.  The argument url specifies the database.   username is the name to use for logging in.  If password is a string then it is used as the password when logging in.  If password is none then no password is used when logging in.  If password is hidden, then a Maplet application, which allows the password to be entered without it appearing on screen, is opened.
 • The value of url depends on the type of database, the JDBC driver and where the database is installed.  However the basic format is "jdbc:driver://hostname/DBName". driver is an identifier for the JDBC driver being used. hostname is the name of the computer on which the database is installed. hostname might also include a port number if the default port is incorrect. DBName is the name of the particular database. For the correct values, consult the documentation for the JDBC driver you are using.  For more information about JDBC drivers, see JDBC.
 • For a general overview of using Database, see the usage page.
 • OpenConnection accepts all the arguments that can be set on a Connection using SetOptions. The valid options are readonly and isolation.
 The readonly optional argument informs the database that it can enable optimizations associated with a read-only connection.  The effect depends on the database.  Setting readonly=true may or may not prevent the execution of updates.  By default, connections are not opened read-only.
 The isolation optional argument determines how isolated transactions using this connection are from transactions occurring at the same time.
 - At serializable isolation, dirty reads (reading uncommitted data from parallel transactions), non-repeatable reads (reading the same row multiple times producing different results due to parallel updates), and phantom reads (WHERE conditions matching different numbers of rows due to parallel updates) are all prevented.
 The default isolation is database specific.
 • OpenConnection can be used to open multiple parallel connections to a single database or multiple connections to different databases (assuming they are all compatible with driver).

Examples

Load the JDBC driver and open a connection to the database.

 > $\mathrm{driver}≔\mathrm{Database}\left[\mathrm{LoadDriver}\right]\left('\mathrm{driver}'="com.database.Driver",'\mathrm{classpath}'="/path/to/jar/jdbc.jar"\right):$
 > $\mathrm{conn1}≔\mathrm{driver}:-\mathrm{OpenConnection}\left("jdbc:dbdriver://localhost/DBName","user1","passwd",'\mathrm{isolation}'='\mathrm{serializable}'\right)$
 ${\mathrm{conn1}}{≔}{\mathbf{module}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{option}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{unload}}{=}{\mathrm{Close}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{handle}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{export}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{ExecuteQuery}}{,}{\mathrm{ExecuteUpdate}}{,}{\mathrm{CreateStatement}}{,}{\mathrm{CreateCallableStatement}}{,}{\mathrm{CreatePreparedStatement}}{,}{\mathrm{Commit}}{,}{\mathrm{Rollback}}{,}{\mathrm{Close}}{,}{\mathrm{SetOptions}}{,}{\mathrm{GetOptions}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (1)

Open a read-only connection to the same database.

 > $\mathrm{conn2}≔\mathrm{driver}:-\mathrm{OpenConnection}\left("jdbc:dbdriver://localhost/DBName","user2",'\mathrm{none}','\mathrm{readonly}'=\mathrm{true}\right)$
 ${\mathrm{conn2}}{≔}{\mathbf{module}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{option}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{unload}}{=}{\mathrm{Close}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{handle}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{export}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{ExecuteQuery}}{,}{\mathrm{ExecuteUpdate}}{,}{\mathrm{CreateStatement}}{,}{\mathrm{CreateCallableStatement}}{,}{\mathrm{CreatePreparedStatement}}{,}{\mathrm{Commit}}{,}{\mathrm{Rollback}}{,}{\mathrm{Close}}{,}{\mathrm{SetOptions}}{,}{\mathrm{GetOptions}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (2)

Open a connection to a different database.

 > $\mathrm{conn3}≔\mathrm{driver}:-\mathrm{OpenConnection}\left("jdbc:dbdriver://localhost/DBName2","user3","secret"\right)$
 ${\mathrm{conn3}}{≔}{\mathbf{module}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{option}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{unload}}{=}{\mathrm{Close}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{handle}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{export}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{ExecuteQuery}}{,}{\mathrm{ExecuteUpdate}}{,}{\mathrm{CreateStatement}}{,}{\mathrm{CreateCallableStatement}}{,}{\mathrm{CreatePreparedStatement}}{,}{\mathrm{Commit}}{,}{\mathrm{Rollback}}{,}{\mathrm{Close}}{,}{\mathrm{SetOptions}}{,}{\mathrm{GetOptions}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (3)

Load a driver for a different kind of database and open a connection to that database.

 > $\mathrm{driver}≔\mathrm{Database}\left[\mathrm{LoadDriver}\right]\left('\mathrm{driver}'="com.database2.Driver",'\mathrm{classpath}'="/path/to/jar2/jdbc.jar"\right):$
 > $\mathrm{conn4}≔\mathrm{driver}:-\mathrm{OpenConnection}\left("jdbc:dbdriver2://localhost/DB2Name","user","passwd",'\mathrm{isolation}'='\mathrm{serializable}'\right)$
 ${\mathrm{conn4}}{≔}{\mathbf{module}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{option}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{unload}}{=}{\mathrm{Close}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{handle}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{export}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{ExecuteQuery}}{,}{\mathrm{ExecuteUpdate}}{,}{\mathrm{CreateStatement}}{,}{\mathrm{CreateCallableStatement}}{,}{\mathrm{CreatePreparedStatement}}{,}{\mathrm{Commit}}{,}{\mathrm{Rollback}}{,}{\mathrm{Close}}{,}{\mathrm{SetOptions}}{,}{\mathrm{GetOptions}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (4)