Informix JDBC Driver Programmer's Guide
Appendix A: Sample Code
Contents
Index
SimpleCall.java
/***************************************************************************
* Title: SimpleCall.java
*
* Description: Demo a simple procedure call statement
*
* An example of running the program:
*
* java SimpleCall
* 'jdbc:informix- sqli://emily:1533:informixserver=emily3;user=rdtest;password=test'
*
* Expected result:
*
* >>>Simple Procedure Call Statement test.
* URL = "jdbc:informix- sqli://emily:1533/testDB:informixserver=emily3;user=rdtest;password=test"
* Execute procedure proc1() returns: 3
* Escape call Procedure proc1() returns: 3
* >>>End of Simple Procedure Call Statement test.
*
***************************************************************************
*/
import java.sql.*;
import java.util.*;
public class SimpleCall {
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 testName = "Simple Procedure Call Statement";
Connection conn = null;
String proc1 = "create procedure proc1() returning int;\n" +
"define var1 int;\n" +
"let var1 = 3;\n" + "return var1;\n" +
"end procedure;";
int rc;
String cmd=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
{
Statement stmt = conn.createStatement();
rc = stmt.executeUpdate(proc1);
stmt.close();
}
catch (SQLException e)
{
System.out.println("ERROR: execution failed - statement: " + proc1);
System.out.println("ERROR: " + e.getMessage());
}
try
{
PreparedStatement pstmt = conn.prepareStatement("execute procedure proc1();");
ResultSet r = pstmt.executeQuery();
if (r.next())
{
short i = r.getShort(1);
System.out.println("Execute procedure proc1() returns: " + i);
}
r.close();
pstmt.close();
}
catch (SQLException e)
{
System.out.println("Prepare \"execute procedure\" failed: " + e.getErrorCode());
}
try
{
PreparedStatement pstmt = conn.prepareStatement("{call proc1()};");
ResultSet r = pstmt.executeQuery();
if (r.next())
{
short i = r.getShort(1);
System.out.println("Escape call Procedure proc1() returns: " + i);
}
r.close();
pstmt.close();
}
catch (SQLException e)
{
System.out.println("ERROR: Prepare \"escape call\" failed: " + e.getErrorCode());
}
try
{
Statement stmt = conn.createStatement();
cmd = "drop procedure proc1";
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.