11.1 Use Case: Opening a Connection to a JDBC Database

In general, NOC Script can call any Java-based library. There are two ways to access a Java package:

  • Use the full package name when accessing Java classes and interfaces.

  • Use the importPackage() mechanism in JavaScript.

The following example shows how to open a connection to a JDBC* database (in this case a Microsoft* SQL Server* database) using full package names:

try
{
   java.lang.Class.forName( 'com.inet.tds.TdsDriver' ) // Get the driver into memory.
   var props = new java.util.Properties()
   props.setProperty( 'user', 'formula' )
   props.setProperty( 'password', 'sesame' )
   var url = 'jdbc:inetdae:qaintel0:1433'
   var conn = java.sql.DriverManager.getConnection( url, props )
}
catch( Exception )
{
   writeln( 'Could not get connection: ' + Exception )
}
The following example shows how to use importPackage:
importPackage( java.sql )
importPackage( java.util )
importPackage( java.lang )

try
{
   Class.forName( 'com.inet.tds.TdsDriver' ) // Get the driver into memory.
   var props = new Properties()
   props.setProperty( 'user', 'formula' )
   props.setProperty( 'password', 'sesame' )
   var url = 'jdbc:inetdae:qaintel0:1433'
   var conn = DriverManager.getConnection( url, props )
}
catch( Exception )
{
   writeln( 'Could not get connection: ' + Exception )
}