Informix JDBC Driver Programmer's Guide
Appendix A: Sample Code
Contents
Index
DBConnection.java
/***************************************************************************
*
* Title: DBConnection.java
*
* Description: Demo a connection to a database of a particular server
*
* An example of running the program:
*
* java DBConnection
* 'jdbc:informix- sqli://emily:1533:informixserver=emily3;user=rdtest;password=test'
*
* Expected result:
*
* >>>Database Connection Direct test.
* URL = "jdbc:informix- sqli://emily:1533/testDB:informixserver=emily3;user=rdtest;password=test"
* >>>End of Database Connection Direct test.
* >>>Database Connection Indirect test.
* URL = "jdbc:informix- sqli://emily:1533:informixserver=emily3;user=rdtest;password=test"
* >>>End of Database Connection Indirect test.
*
***************************************************************************
*/
import java.sql.*;
import java.util.*;
public class DBConnection {
public static void main(String[] args)
{
if (args.length == 0)
{
System.out.println("ERROR: connection URL must be provided in order to run the demo!");
return;
}
String url = args[0];
StringTokenizer st = new StringTokenizer(url, ":");
String token;
String newUrl = "";
for (int i = 0; i < 4; ++i)
{
if (!st.hasMoreTokens())
{
System.out.println("ERROR: incorrect URL format!");
return;
}
token = st.nextToken();
if (newUrl != "")
newUrl += ":";
newUrl += token;
}
newUrl += "/testDB";
while (st.hasMoreTokens())
{
newUrl += ":" + st.nextToken();
}
String cmd = null;
int rc;
String testName = "Database Connection Direct";
Connection conn = null;
System.out.println(">>>" + testName + " test.");
System.out.println("URL = \"" + newUrl + "\"");
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!");
}
try
{
conn.close();
}
catch (SQLException e)
{
System.out.println("ERROR: failed to close the connection!");
}
System.out.println(">>>End of " + testName + " test.");
url = args[0];
testName = "Database Connection Indirect";
conn = null;
System.out.println(">>>" + testName + " test.");
System.out.println("URL = \"" + url + "\"");
try
{
Class.forName("com.informix.jdbc.IfxDriver");
}
catch (Exception e)
{
System.out.println("ERROR: failed to load Informix JDBC driver.");
}
try
{
conn = DriverManager.getConnection(url);
}
catch (SQLException e)
{
System.out.println("ERROR: failed to connect!");
}
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());
}
try
{
conn.close();
}
catch (SQLException e)
{
System.out.println("ERROR: failed to close the connection!");
}
System.out.println(">>>End of " + testName + " test.");
}
}
Informix JDBC Driver Programmer's Guide
, Version 1.22
Copyright © 1998, Informix Software, Inc. All rights reserved.