Informix JDBC Driver Programmer's Guide
Appendix A: Sample Code
Contents
Index
optofc.java
/***************************************************************************
*
* Title: optofc.java
*
* Description: Show the differnce in the behavior when OPTOFC is set
*
* Instructions to run the program
*
* 1. Use CreateDB to create the Database testdb if not already done
* java CreateDB 'jdbc:informix-sqli:
* //emily:1533:informixserver=emily3;user=rdtest;password=test'
* 2. Run the program
* java optofc
* 'jdbc:informix-sqli://emily:1533:informixserver=emily3;
* user=rdtest;password=test'
* 5. Expected Result
* Data in the table
>>>OPTOFC set close() followed by next() throws Exception no SQ_CLOSE sent from the client side test.
URL = "jdbc:informix- sqli://emily:1533/testDB:informixserver=emily3;user=rdtest;password=test"
Trying to insert data using Prepare Statement ...
Retreaving records:
Calling resultSet.next() = 26
Calling resultSet.next() = 27
Calling resultSet.next() = 28
Calling resultSet.next() = 29
Calling resultSet.next() = 30
Calling resultSet.close()
Calling resultSet.next()
OPTOFC is set next after close throws SQLException
Cursor not open.
*
***************************************************************************
*/
import java.sql.*;
import java.util.*;
/*
* OPTOFC is set.
* Calling rs.next() after calling rs.close()
* should throw a SQL EXception
*/
public class optofc
{
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];
Connection conn = null;
Statement stmt = null;
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;
String testName = "OPTOFC set close() followed by next() throws Exception no SQ_CLOSE sent from the client side";
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
{
Properties pr = new Properties();
pr.put("OPTOFC","1");
conn = DriverManager.getConnection(newUrl, pr);
}
catch (SQLException e)
{
System.out.println("ERROR: failed to connect!");
}
try
{
stmt = conn.createStatement();
stmt.executeUpdate("drop table tab1");
}
catch (SQLException e){ }
try
{
stmt = conn.createStatement();
stmt.executeUpdate("create table tab1(age int)");
}
catch ( SQLException e)
{
System.out.println("Failed to create table ..." + e.getMessage());
// e.printStackTrace();
}
System.out.println("Trying to insert data using Prepare Statement ...");
try
{
PreparedStatement pst = conn.prepareStatement("insert into tab1 values (?)");
int age = 25;
for (int i = 0; i < 5 ; i++)
{
age = age + 1;
pst.setInt (1, age);
pst.executeUpdate();
}
}
catch (SQLException e)
{
System.out.println("Failed to Insert into tab: " + e.toString());
// e.printStackTrace();
}
try
{
ResultSet r = stmt.executeQuery("select * from tab1");
int j;
System.out.println("Retreaving records: ");
int n = 0;
while(r.next())
{
n++;
int age1 = r.getInt(1);
System.out.println(" Calling resultSet.next() = " + age1);
}
System.out.println("Calling resultSet.close() ");
r.close();
System.out.println("Calling resultSet.next() ");
boolean flag = r.next();
System.out.println ("This is the output if the OPTOFC is not set");
System.out.println ("next() after close() returns "+ flag);
}
catch (SQLException e)
{
System.out.println ("OPTOFC is set next after close throws SQLException ");
System.out.println(e.getMessage());
}
}
}
Informix JDBC Driver Programmer's Guide
, Version 1.22
Copyright © 1998, Informix Software, Inc. All rights reserved.