Using the DriveManager.getConnection() method, you can create a connection to either an Informix database or an Informix database server.
To create a connection to an 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 an Informix database server, you can execute an SQL statement that connects to a database in your Java program.
All connections to both databases and database servers must include the name of an Informix database server via the INFORMIXSERVER environment variable.
The example given in Using 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 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()); }Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]