Informix JDBC Driver Programmer's Guide
Appendix A: Sample Code
Contents
Index
TextType.java
/***************************************************************************
*
* Title: TextType.java
*
* Description: Demo a textDataType operation
*
* An example of running the program:
*
* java TextType
* 'jdbc:informix- sqli://emily:1533:informixserver=emily3;user=rdtest;password=test'
*
* Expected result:
*
* >>>Simple text data type insert and fetch operation test.
* URL = "jdbc:informix- sqli://emily:1533/testDB:informixserver=emily3;user=rdtest;password=test"
* Trying to insert data using Prepare Statement ...
* Inserting data ...
*
* Entering 1024 bytes of data to text/byte at column-1
*
* Reading data now ...
*
*
* Result of row #1, size = 1024 from getAsciiStream(1) ..
*
* 12345678901234567890123456789012345678901234567890123456789012345678901234567890 1234567890123456789
* 101- 56789012345678901234567890123456789012345678901234567890123456789012345678901234 567890123456789
* 201- 56789012345678901234567890123456789012345678901234567890123456789012345678901234 567890123456789
* 301- 56789012345678901234567890123456789012345678901234567890123456789012345678901234 567890123456789
* 401- 56789012345678901234567890123456789012345678901234567890123456789012345678901234 567890123456789
* 501- 56789012345678901234567890123456789012345678901234567890123456789012345678901234 567890123456789
* 601- 56789012345678901234567890123456789012345678901234567890123456789012345678901234 567890123456789
* 701- 56789012345678901234567890123456789012345678901234567890123456789012345678901234 567890123456789
* 801- 56789012345678901234567890123456789012345678901234567890123456789012345678901234 567890123456789
* 901- 56789012345678901234567890123456789012345678901234567890123456789012345678901234 567890123456789
* 1001-6789012345678901234
* Trying to drop the table ...
*
* >>>End of Simple text data type insert and fetch operation test.
*
***************************************************************************
*/
import java.sql.*;
import java.util.*;
import java.math.BigDecimal ;
import java.io.*;
import java.lang.*;
public class TextType {
static Statement stmt;
static PreparedStatement pstmt;
static Connection conn = null;
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;
String testName = "Simple text data type insert and fetch operation";
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
{
stmt = conn.createStatement();
stmt.executeUpdate("drop table tab1");
}
catch (SQLException e){ }
try
{
stmt = conn.createStatement();
stmt.executeUpdate("create table tab1(col1 text)");
}
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
{
pstmt = conn.prepareStatement("insert into tab1 values (?)");
}
catch (SQLException e)
{
System.out.println("Failed to Insert into tab: " + e.toString());
// e.printStackTrace();
}
File file = new File("data.dat");
int fileLength = (int) file.length();
InputStream value = null;
int row = 0;
String str = null;
int rc = 0;
ResultSet rs = null;
System.out.println("Inserting data ...\n");
set_AsciiStream(1, file,1024); // set 1st column with 1024 byte Ascii
set_execute();
System.out.println("Reading data now ...\n");
try
{
stmt = conn.createStatement();
rs = stmt.executeQuery("Select * from tab1");
while( rs.next() )
{
row++;
value = rs.getAsciiStream(1);
System.out.println("\nResult of row #" + row + ", size = " + value.available() + " from getAsciiStream(1) ..\n");
}
}
catch (Exception e) { }
dumpValue(value);
System.out.println("Trying to drop the table ...\n");
try
{
stmt = conn.createStatement();
rc = stmt.executeUpdate("drop table tab1");
}
catch ( SQLException e)
{ }
try
{
stmt.close();
pstmt.close();
conn.close();
}
catch ( SQLException e)
{
}
System.out.println(">>>End of " + testName + " test.");
}
public static void set_AsciiStream(int index, File file, int length)
{
FileInputStream fin;
System.out.println("Entering " + length + " bytes of data to text/byte at column-"+index+" \n");
try
{
fin = new FileInputStream(file);
pstmt.setAsciiStream(index , fin, length);
}
catch ( Exception e)
{
System.out.println("Failed to perform setAsciiStream: " + e.toString());
}
}
public static void set_execute()
{
try
{
pstmt.executeUpdate();
}
catch (SQLException e)
{
System.out.println("Failed to Insert into tab: " + e.toString());
e.printStackTrace();
}
}
public static void dumpValue(InputStream in)
{
int size;
byte buf;
int count = 0;
try
{
size = in.available();
byte ary[] = new byte[size];
buf = (byte) in.read();
while(buf!=-1)
{
ary[count] = buf;
count++;
buf = (byte) in.read();
}
System.out.println( new String(ary).trim());
}
catch (Exception e)
{
System.out.println("Error occur during reading stream ... \n");
}
}
}
Informix JDBC Driver Programmer's Guide
, Version 1.22
Copyright © 1998, Informix Software, Inc. All rights reserved.