Database versus database server connections

Using the DriveManager.getConnection() method, you can create a connection to either the HCL Informix® database or the Informix® database server.

To create a connection to the Informix® database, specify the name of the database in the dbname variable of the database URL. If you omit the name of a database, a connection is made to the database server specified by the INFORMIXSERVER environment variable of the database URL or the connection property list.

If you connect directly to the Informix® database server, you can execute an SQL statement that connects to a database in your Java program.

The example given in The DriverManager.getConnection() method shows how to create a connection directly to the Informix® database called testDB with the database URL.

The following example from the DBConnection.java program shows how to first create a connection to the Informix® database server called myserver and then connect to the database testDB by using the Statement.executeUpdate() method.

The following database URL is passed in as a parameter to the program when the program is run at the command line; note that the URL does not include the name of a database:
jdbc:informix-sqli://123.45.67.89:1533:INFORMIXSERVER=myserver;
   user=rdtest;password=test
The code is:
String cmd = null;
int rc;
Connection conn = null;

try
{
   Class.forName("com.informix.jdbc.IfxDriver");   
}
catch (Exception e)   
{
   System.out.println("ERROR: failed to load Informix JDBC driver.");   
}
try   
{
   conn = DriverManager.getConnection(newUrl);   
}
catch (SQLException e)   
{
   System.out.println("ERROR: failed to connect!");
   e.printStackTrace();
   return;
}
try   
{
   Statement stmt = conn.createStatement();
   cmd = "database testDB;";
   rc = stmt.executeUpdate(cmd);
   stmt.close();
}
catch (SQLException e)   
{
   System.out.println("ERROR: execution failed - statement:
      " + cmd);
   System.out.println("ERROR: " + e.getMessage());        }