LO.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import java.lang.*;
  2. import java.io.*;
  3. import java.sql.*;
  4. import com.informix.jdbc.*;
  5. import com.informix.udr.*;
  6. /**
  7. * Examples of accessing large objects using JDBC.
  8. *
  9. */
  10. public class LO
  11. {
  12. /**
  13. * Make a Clob in a table and return it.
  14. */
  15. public static Clob lo() throws SQLException
  16. {
  17. Connection myConn = null;
  18. String connURL = "jdbc:informix-direct:";
  19. try
  20. {
  21. // Loading the JDBC driver
  22. Class.forName("com.informix.jdbc.IfxDriver");
  23. // Establising the connection
  24. myConn = DriverManager.getConnection(connURL);
  25. // create a LO table
  26. PreparedStatement pstmt = myConn.prepareStatement(
  27. "delete from mytable");
  28. pstmt.executeUpdate();
  29. pstmt.close();
  30. // insert a new CLOB object made from a String
  31. pstmt = myConn.prepareStatement( "insert into mytable values(?)");
  32. String inp = "This was a String -- now it's a large object";
  33. byte ba[] = inp.getBytes();
  34. InputStream is = new ByteArrayInputStream(ba);
  35. pstmt.setAsciiStream(1, is, ba.length);
  36. pstmt.executeUpdate();
  37. pstmt.close();
  38. // retrieve the LO from the table and return it.
  39. Statement stmt = myConn.createStatement();
  40. ResultSet rs = stmt.executeQuery("select c from mytable");
  41. rs.next();
  42. // notice the use of ResultSet2 -- this is a JDBC2.0 version
  43. // of the ResultSet interface
  44. Clob c = ((ResultSet2)rs).getClob(1);
  45. stmt.close();
  46. return (Clob)c;
  47. }
  48. catch (Exception ex)
  49. {
  50. throw new SQLException(ex.toString());
  51. }
  52. }
  53. }