JDBC.java 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import java.lang.*;
  2. import java.sql.*;
  3. /**
  4. * An example UDR using the server-side JDBC driver.
  5. */
  6. public class JDBC
  7. {
  8. public static int sum1to10() throws SQLException
  9. {
  10. Connection myConn = null;
  11. String connURL = "jdbc:informix-direct:";
  12. int res = 0;
  13. try
  14. {
  15. // Loading JDBC Driver
  16. Class.forName("com.informix.jdbc.IfxDriver");
  17. // Establishing a connection
  18. myConn = DriverManager.getConnection(connURL);
  19. // create the test table
  20. PreparedStatement pstmt = myConn.prepareStatement(
  21. "create table foo (i int)");
  22. pstmt.executeUpdate();
  23. pstmt.close();
  24. // insert 10 rows using PreparedStatement
  25. pstmt = myConn.prepareStatement("insert into foo values(?)");
  26. for (int i = 1; i <= 10; ++ i)
  27. {
  28. pstmt.setInt(1, i);
  29. pstmt.executeUpdate();
  30. }
  31. pstmt.close();
  32. // retrieve the rows using Statement and ResultSet
  33. Statement stmt = myConn.createStatement();
  34. ResultSet rs = stmt.executeQuery( "select i from foo");
  35. while (rs.next())
  36. {
  37. int intval = rs.getInt(1);
  38. res += intval;
  39. }
  40. stmt.close();
  41. } catch (Exception e)
  42. {
  43. throw new SQLException(e.toString());
  44. }
  45. return res;
  46. };
  47. }