skip to main content

Embedded SQL Statements : CONNECT

CONNECT
The CONNECT statement allows to connect to a database.
General format
Format 1
EXEC SQL
  CONNECT [ TO {Database-Name} [ AS Connection-Name ] ]
          [ USER User-Name [ USING Password ] ]
Format 2
EXEC SQL
  CONNECT : [ User-Name IDENTIFIED BY: Password [ AT Database ] USING DBstring ]
Syntax rules
1.
Database-Name, User-Name, Password , DBString, Connection-Name and UsrPwd are Nonnumeric Literals, as defined in the Definitions section of the Preface of this document or Host Variables.
2.
Connection-Name can be referenced by the SET CONNECTION statement
General rules
1.
2.
When the TO phrase is omitted, or DEFAULT is specified, no database name is passed to the jdbc driver. If USER and USING are omitted too, then no parameter is passed to the jdbc driver, assuming that all the necessary information is provided by the iscobol.jdbc.url configuration property.
3.
When the AS phrase is omitted, the connection name is the same as the database name.
4.
Once connected you can retrieve the connection handle by invoking the getCurrConnection() method of the com.iscobol.rts.EsqlRuntime object. This method returns a java.sql.Connection that can be passed to other java programs.
5.
UsrPwd contains both UserName and Password separated by "/". Example: UsrPWD = "scott/tiger".
6.
Database must be previously defined using a Format 4 DECLARE statement.
7.
If the iscobol.jdbc.datasource configuration property specifies a valid class, then the connect() method of that class is invoked to gain connection. If the property is not set, then the runtime invokes the Java SQL DriverManager to gain connection. The following snippet shows a prototype of a valid class
import java.sql.*;
import javax.sql.*;
public class myDataSource implements com.iscobol.rts.MyDataSource {
   public Connection connect (String dsn, String usr, String pwd)
      return conn;
   private Connection getMyConnection(String usr, String pwd) {
      // do the necessary operation to get the connection
Note - The parameters dsn, user and passwd are the ones used in the CONNECT statement. If the program performs just CONNECT because database name and login credentials were put in the JDBC URL, then the class does not receive any parameter.
 
To make isCOBOL use the above class for connection, set
Examples
Format 1 - Connect to the ctree database
move "ctreeSQL" to dbname
move "admin"    to userid
move "ADMIN"    to passwd
exec sql 
   connect to :dbname 
        using :passwd

Copyright (c) 2017 Veryant
Contact us
Please share your comments on this manual or on any
Veryant product documentation with the email button at the top left