Informix JDBC Driver Programmer's Guide
Appendix A: Sample Code
Contents
Index
Interval.java
/***************************************************************************
* Title: Interval.java
*
* Description: Demo inserting and selecting Informix intervals
*
* An example of running the program:
*
* java Interval
* 'jdbc:informix- sqli://emily:1533:informixserver=emily3;user=rdtest;password=test'
*
* Expected result:
*
* Inserting and fetching Interval columns test.
*
* URL = "jdbc:informix- sqli://emily:1533:informixserver=emily3;user=rdtest;password=test"
*
* Create a table
* Create table rowcount = 0
*
* Insert an interval using a String host variable
* Insert rowcount = 1
*
* Select from intrvl_tab
* Select: ym = 102-11
* Select: ds = 5 10:03:55
*
* Test End
*
***************************************************************************
*/
import java.sql.*;
import java.util.*;
import java.lang.*;
/*
* Connect an existing database test db. Create a table and insert
* and fetch an interval value.
*/
public class Interval {
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 = "";
String testName = "Inserting and fetching Interval columns";
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();
}
Connection conn = null;
String cmd = null;
Statement stmt = null;
System.out.println(testName + " test.");
System.out.println("");
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.");
return;
}
try
{
conn = DriverManager.getConnection(newUrl);
}
catch (SQLException e)
{
System.out.println("ERROR: failed to connect: " + e.toString());
return;
}
/*
* Drop existing table (if any)
*/
try
{
stmt = conn.createStatement();
cmd = "drop table intrvl_tab";
stmt.executeUpdate(cmd);
}
catch (Exception e)
{
/* don't do anything if the table doesn't exist */
}
/*
* Create a table with two interval columns
*/
System.out.println("");
System.out.println("Create a table");
try
{
cmd = "create table intrvl_tab " +
"(ym interval year to month, ds interval day to second)";
int rowcount = stmt.executeUpdate(cmd);
System.out.println("Create table rowcount = " + rowcount);
}
catch (SQLException e)
{
System.out.println("ERROR: execution failed - statement: " + cmd);
System.out.println("ERROR: " + e.getMessage());
return;
}
/*
* Insert an interval using a String host variable.
*/
System.out.println("");
System.out.println("Insert an interval using a String host variable");
try
{
String ym = new String("102-11"); // 102 years and 11 months
String ds = new String("5 10:03:55"); // 5 days 10 hours 3 minutes
// and 55 seconds
cmd = "insert into intrvl_tab values (?, ?)";
PreparedStatement pstmt = conn.prepareStatement(cmd);
pstmt.setString(1, ym);
pstmt.setString(2, ds);
int rowcount = pstmt.executeUpdate();
System.out.println("Insert rowcount = " + rowcount);
}
catch (SQLException e)
{
System.out.println("ERROR: execution failed - statement: " + cmd);
System.out.println("ERROR: " + e.getMessage());
return;
}
/*
* Now fetch the row back
*/
System.out.println("");
System.out.println("Select from intrvl_tab");
try
{
cmd = "select * from intrvl_tab";
ResultSet rs = stmt.executeQuery(cmd);
String ym;
String ds;
while(rs.next())
{
ym = rs.getString(1);
System.out.println(" Select: ym = " + ym);
ds = rs.getString(2);
System.out.println(" Select: ds = " + ds);
}
}
catch (SQLException e)
{
System.out.println("ERROR: execution failed - statement: " + cmd);
System.out.println("ERROR: " + e.getMessage());
return;
}
System.out.println("");
System.out.println("Test End");
}
}
Informix JDBC Driver Programmer's Guide
, Version 1.22
Copyright © 1998, Informix Software, Inc. All rights reserved.