Informix JDBC Driver Programmer's Guide
Appendix A: Sample Code
Contents
Index
PropertyConnection.java
/***************************************************************************
*
* Title: PropertyConnection.java
*
* Description: Demo a connection to a server using a property list
*
* java PropertyConnection
* 'jdbc:informix- sqli://emily:1533:informixserver=emily3;user=rdtest;password=test'
*
* Expected result:
*
* >>>Property Connection test.
* URL = "jdbc:informix-sqli://emily:1533"
* Properties = "{password=test, informixserver=emily3, user=rdtest}"
* >>>End of Property Connection test.
*
***************************************************************************
*/
import java.sql.*;
import java.util.*;
public class PropertyConnection
{
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;
}
if (!st.hasMoreTokens())
{
System.out.println("ERROR: incorrect URL format!");
return;
}
token = st.nextToken();
if (st.hasMoreTokens())
{
System.out.println("ERROR: incorrect URL format!");
return;
}
StringTokenizer st1 = new StringTokenizer(token, ";");
Properties prop = new Properties();
while (st1.hasMoreTokens())
{
int index;
String t1 = st1.nextToken();
if ((index = t1.indexOf('=')) == -1 ||
(t1.indexOf('=') != t1.lastIndexOf('=')))
{
System.out.println("ERROR: incorrect URL format!");
return;
}
prop.put(t1.substring(0, index), t1.substring(index+1));
}
String testName = "Property Connection";
Connection conn = null;
System.out.println(">>>" + testName + " test.");
System.out.println("URL = \"" + newUrl + "\"");
System.out.println("Properties = \"" + prop.toString() + "\"");
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,prop);
}
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.");
}
}
Informix JDBC Driver Programmer's Guide
, Version 1.22
Copyright © 1998, Informix Software, Inc. All rights reserved.