Informix JDBC Driver Programmer's Guide
Appendix A: Sample Code
Contents
Index
MultiRowCall.java
/***************************************************************************
*
* Title: MultiRowCall.java
*
* Description: Demo a procedure call with multiple return rows
*
* An example of running the program:
*
* java MultiRowCall
* 'jdbc:informix- sqli://emily:1533:informixserver=emily3;user=rdtest;password=test'
* Expected result:
*
* >>>Multiple Return Row Procedure Call Statement test.
* URL = "jdbc:informix- sqli://emily:1533/testDB:informixserver=emily3;user=rdtest;password=test"
* Execute procedure proc2() returns: 1
* Execute procedure proc2() returns: 2
* Execute procedure proc2() returns: 3
* Execute procedure proc2() returns: 4
* Execute procedure proc2() returns: 5
* >>>End of Multiple Return Row Procedure Call Statement test.
*
***************************************************************************
*/
import java.sql.*;
import java.util.*;
public class MultiRowCall {
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 = "Multiple Return Row Procedure Call Statement";
Connection conn = null;
String proc2 = "create procedure proc2( i integer ) " +
"returning integer ; " +
" define j integer ; " +
" for j in ( 1 to 5 ) " +
" let i = i + 1 ; " +
" return i with resume ; " +
" end for " +
"end procedure";
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
{
Statement stmt = conn.createStatement();
rc = stmt.executeUpdate(proc2);
stmt.close();
}
catch (SQLException e)
{
System.out.println("ERROR: execution failed - statement: " + proc2);
System.out.println("ERROR: " + e.getMessage());
}
try
{
PreparedStatement pstmt = conn.prepareStatement("{call proc2(0)};");
ResultSet r = pstmt.executeQuery();
while (r.next())
{
short i = r.getShort(1);
System.out.println("Execute procedure proc2() returns: " + i);
}
r.close();
pstmt.close();
}
catch (SQLException e)
{
System.out.println("ERROR: Prepare \"call proc2\" failed: " + e.getErrorCode());
}
try
{
Statement stmt = conn.createStatement();
cmd = "drop procedure proc2";
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.