//
  // Examine BLOBs and CLOBs.
  //
  private void vetLargeObjects(
      Connection conn, HashSet<String> unsupportedList, HashSet<String> notUnderstoodList)
      throws Exception {
    Statement stmt = conn.createStatement();

    stmt.execute("CREATE TABLE t (id INT PRIMARY KEY, " + "b BLOB(10), c CLOB(10))");
    stmt.execute(
        "INSERT INTO t (id, b, c) VALUES (1, "
            + "CAST ("
            + TestUtil.stringToHexLiteral("101010001101")
            + "AS BLOB(10)), CAST ('hello' AS CLOB(10)))");

    ResultSet rs = stmt.executeQuery("SELECT id, b, c FROM t");

    rs.next();

    Blob blob = rs.getBlob(2);
    Clob clob = rs.getClob(3);

    vetObject(blob, unsupportedList, notUnderstoodList);
    vetObject(clob, unsupportedList, notUnderstoodList);

    stmt.close();
    conn.rollback();
  }
  public void testAny() {

    try {
      String ddl =
          "drop table PRICE_RELATE_USER_ORDER_V2 if exists;"
              + "create table PRICE_RELATE_USER_ORDER_V2 "
              + "(ID_ORDER_V2 BIGINT, ID_USER NUMERIC, DATE_CREATE TIMESTAMP)";
      String sql =
          "insert into PRICE_RELATE_USER_ORDER_V2 "
              + "(ID_ORDER_V2, ID_USER, DATE_CREATE) "
              + "values "
              + "(?, ?, ?)";
      Statement st = connection.createStatement();

      st.execute(ddl);

      PreparedStatement ps = connection.prepareStatement(sql);

      ps.setLong(1, 1);
      ps.setNull(2, Types.NUMERIC);
      ps.setTimestamp(3, new java.sql.Timestamp(System.currentTimeMillis()));
      ps.execute();
    } catch (SQLException e) {
      e.printStackTrace();
      System.out.println("TestSql.testAny() error: " + e.getMessage());
    }

    System.out.println("testAny complete");
  }
Example #3
0
 public void tearDown() {
   try {
     Connection con = AllTests.getConnection();
     Statement st = con.createStatement();
     st.execute("drop table " + table);
     st.close();
   } catch (Throwable e) {
     // e.printStackTrace();
   }
 }
Example #4
0
  public void setUp() {
    tearDown();
    try {
      Connection con = AllTests.getConnection();
      Statement st = con.createStatement();
      st.execute(
          "create table " + table + "(a " + testValue.dataType + ", b " + testValue.dataType + ")");
      st.close();
      PreparedStatement pr = con.prepareStatement("INSERT into " + table + "(a,b) Values(?,?)");

      pr.setObject(1, testValue.small);
      pr.setObject(2, testValue.large);
      pr.execute();

      pr.setObject(1, testValue.small);
      pr.setObject(2, testValue.small);
      pr.execute();

      pr.setObject(1, testValue.large);
      pr.setObject(2, testValue.large);
      pr.execute();

      pr.setObject(1, testValue.large);
      pr.setObject(2, testValue.small);
      pr.execute();

      pr.setObject(1, null);
      pr.setObject(2, testValue.small);
      pr.execute();

      pr.setObject(1, testValue.small);
      pr.setObject(2, null);
      pr.execute();

      pr.setObject(1, null);
      pr.setObject(2, null);
      pr.execute();
      pr.close();
    } catch (Throwable e) {
      e.printStackTrace();
    }
  }
  public void testMetaData() {

    String ddl0 =
        "DROP TABLE ADDRESSBOOK IF EXISTS; DROP TABLE ADDRESSBOOK_CATEGORY IF EXISTS; DROP TABLE USER IF EXISTS;";
    String ddl1 =
        "CREATE TABLE USER(USER_ID INTEGER NOT NULL PRIMARY KEY,LOGIN_ID VARCHAR(128) NOT NULL,USER_NAME VARCHAR(254) DEFAULT ' ' NOT NULL,CREATE_DATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,UPDATE_DATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,LAST_ACCESS_DATE TIMESTAMP,CONSTRAINT IXUQ_LOGIN_ID0 UNIQUE(LOGIN_ID))";
    String ddl2 =
        "CREATE TABLE ADDRESSBOOK_CATEGORY(USER_ID INTEGER NOT NULL,CATEGORY_ID INTEGER DEFAULT 0 NOT NULL,CATEGORY_NAME VARCHAR(60) DEFAULT '' NOT NULL,CONSTRAINT SYS_PK_ADDRESSBOOK_CATEGORY PRIMARY KEY(USER_ID,CATEGORY_ID),CONSTRAINT FK_ADRBKCAT1 FOREIGN KEY(USER_ID) REFERENCES USER(USER_ID) ON DELETE CASCADE)";
    String ddl3 =
        "CREATE TABLE ADDRESSBOOK(USER_ID INTEGER NOT NULL,ADDRESSBOOK_ID INTEGER NOT NULL,CATEGORY_ID INTEGER DEFAULT 0 NOT NULL,FIRST VARCHAR(64) DEFAULT '' NOT NULL,LAST VARCHAR(64) DEFAULT '' NOT NULL,NOTE VARCHAR(128) DEFAULT '' NOT NULL,CONSTRAINT SYS_PK_ADDRESSBOOK PRIMARY KEY(USER_ID,ADDRESSBOOK_ID),CONSTRAINT FK_ADRBOOK1 FOREIGN KEY(USER_ID,CATEGORY_ID) REFERENCES ADDRESSBOOK_CATEGORY(USER_ID,CATEGORY_ID) ON DELETE CASCADE)";
    String result1 = "1";
    String result2 = "2";
    String result3 = "3";
    String result4 = "4";
    String result5 = "5";

    try {
      stmnt.execute(ddl0);
      stmnt.execute(ddl1);
      stmnt.execute(ddl2);
      stmnt.execute(ddl3);

      DatabaseMetaData md = connection.getMetaData();

      {

        //                System.out.println(md.getDatabaseMajorVersion());
        //                System.out.println(md.getDatabaseMinorVersion());
        System.out.println(md.getDatabaseProductName());
        System.out.println(md.getDatabaseProductVersion());
        System.out.println(md.getDefaultTransactionIsolation());
        System.out.println(md.getDriverMajorVersion());
        System.out.println(md.getDriverMinorVersion());
        System.out.println(md.getDriverName());
        System.out.println(md.getDriverVersion());
        System.out.println(md.getExtraNameCharacters());
        System.out.println(md.getIdentifierQuoteString());

        //                System.out.println(md.getJDBCMajorVersion());
        //                System.out.println(md.getJDBCMinorVersion());
        System.out.println(md.getMaxBinaryLiteralLength());
        System.out.println(md.getMaxCatalogNameLength());
        System.out.println(md.getMaxColumnsInGroupBy());
        System.out.println(md.getMaxColumnsInIndex());
        System.out.println(md.getMaxColumnsInOrderBy());
        System.out.println(md.getMaxColumnsInSelect());
        System.out.println(md.getMaxColumnsInTable());
        System.out.println(md.getMaxConnections());
        System.out.println(md.getMaxCursorNameLength());
        System.out.println(md.getMaxIndexLength());
        System.out.println(md.getMaxProcedureNameLength());
        System.out.println(md.getMaxRowSize());
        System.out.println(md.getMaxSchemaNameLength());
        System.out.println(md.getMaxStatementLength());
        System.out.println(md.getMaxStatements());
        System.out.println(md.getMaxTableNameLength());
        System.out.println(md.getMaxUserNameLength());
        System.out.println(md.getNumericFunctions());
        System.out.println(md.getProcedureTerm());

        //                System.out.println(md.getResultSetHoldability());
        System.out.println(md.getSchemaTerm());
        System.out.println(md.getSearchStringEscape());
        System.out.println(md.getSQLKeywords());

        //                System.out.println(md.getSQLStateType());
        System.out.println(md.getStringFunctions());
        System.out.println(md.getSystemFunctions());
        System.out.println(md.getTimeDateFunctions());
        System.out.println(md.getURL());
        System.out.println(md.getUserName());
        System.out.println(md.importedKeyCascade);
        System.out.println(md.isCatalogAtStart());
        System.out.println(md.isReadOnly());

        ResultSet rs;

        rs = md.getPrimaryKeys(null, null, "USER");

        ResultSetMetaData rsmd = rs.getMetaData();
        String result0 = "";

        for (; rs.next(); ) {
          for (int i = 0; i < rsmd.getColumnCount(); i++) {
            result0 += rs.getString(i + 1) + ":";
          }

          result0 += "\n";
        }

        rs.close();
        System.out.println(result0);
      }

      {
        ResultSet rs;

        rs = md.getBestRowIdentifier(null, null, "USER", 0, true);

        ResultSetMetaData rsmd = rs.getMetaData();
        String result0 = "";

        for (; rs.next(); ) {
          for (int i = 0; i < rsmd.getColumnCount(); i++) {
            result0 += rs.getString(i + 1) + ":";
          }

          result0 += "\n";
        }

        rs.close();
        System.out.println(result0);
      }

      {
        ResultSet rs = md.getImportedKeys(null, null, "ADDRESSBOOK");
        ResultSetMetaData rsmd = rs.getMetaData();

        result1 = "";

        for (; rs.next(); ) {
          for (int i = 0; i < rsmd.getColumnCount(); i++) {
            result1 += rs.getString(i + 1) + ":";
          }

          result1 += "\n";
        }

        rs.close();
        System.out.println(result1);
      }

      {
        ResultSet rs =
            md.getCrossReference(null, null, "ADDRESSBOOK_CATEGORY", null, null, "ADDRESSBOOK");
        ResultSetMetaData rsmd = rs.getMetaData();

        result2 = "";

        for (; rs.next(); ) {
          for (int i = 0; i < rsmd.getColumnCount(); i++) {
            result2 += rs.getString(i + 1) + ":";
          }

          result2 += "\n";
        }

        rs.close();
        System.out.println(result2);
      }

      {
        ResultSet rs = md.getExportedKeys(null, null, "USER");
        ResultSetMetaData rsmd = rs.getMetaData();

        result3 = "";

        for (; rs.next(); ) {
          for (int i = 0; i < rsmd.getColumnCount(); i++) {
            result3 += rs.getString(i + 1) + ":";
          }

          result3 += "\n";
        }

        rs.close();
        System.out.println(result3);
      }

      {
        ResultSet rs = md.getCrossReference(null, null, "USER", null, null, "ADDRESSBOOK_CATEGORY");
        ResultSetMetaData rsmd = rs.getMetaData();

        result4 = "";

        for (; rs.next(); ) {
          for (int i = 0; i < rsmd.getColumnCount(); i++) {
            result4 += rs.getString(i + 1) + ":";
          }

          result4 += "\n";
        }

        rs.close();
        System.out.println(result4);
      }

      {
        stmnt.execute("DROP TABLE T IF EXISTS;");
        stmnt.executeQuery("CREATE TABLE T (I IDENTITY, A CHAR, B CHAR);");
        stmnt.executeQuery(
            "INSERT INTO T VALUES (NULL, 'get_column_name', '" + getColumnName + "');");

        ResultSet rs = stmnt.executeQuery("SELECT I, A, B, A \"aliasA\", B \"aliasB\" FROM T;");
        ResultSetMetaData rsmd = rs.getMetaData();

        result5 = "";

        for (; rs.next(); ) {
          for (int i = 0; i < rsmd.getColumnCount(); i++) {
            result5 += rsmd.getColumnName(i + 1) + ":" + rs.getString(i + 1) + ":";
          }

          result5 += "\n";
        }

        rs.close();

        rs = stmnt.executeQuery("SELECT I, A, B, A \"aliasA\", B \"aliasB\" FROM T;");
        ;
        rsmd = rs.getMetaData();

        for (; rs.next(); ) {
          for (int i = 0; i < rsmd.getColumnCount(); i++) {
            result5 += rsmd.getColumnLabel(i + 1) + ":" + rs.getString(i + 1) + ":";
          }

          result5 += "\n";
        }

        System.out.println(result5);
        System.out.println("first column identity: " + rsmd.isAutoIncrement(1));
        rsmd.isCaseSensitive(1);
        rsmd.isCurrency(1);
        rsmd.isDefinitelyWritable(1);
        rsmd.isNullable(1);
        rsmd.isReadOnly(1);
        rsmd.isSearchable(1);
        rsmd.isSigned(1);
        rsmd.isWritable(1);
        rs.close();

        // test identity with PreparedStatement
        pstmnt = connection.prepareStatement("INSERT INTO T VALUES (?,?,?)");

        pstmnt.setString(1, null);
        pstmnt.setString(2, "test");
        pstmnt.setString(3, "test2");
        pstmnt.executeUpdate();

        pstmnt = connection.prepareStatement("call identity()");

        ResultSet rsi = pstmnt.executeQuery();

        rsi.next();

        int identity = rsi.getInt(1);

        System.out.println("call identity(): " + identity);
        rsi.close();
      }
    } catch (SQLException e) {
      fail(e.getMessage());
    }

    System.out.println("testMetaData complete");

    // assert equality of exported and imported with xref
    assertEquals(result1, result2);
    assertEquals(result3, result4);
  }
  /**
   * Demonstration of a reported bug.
   *
   * <p>Because all values were turned into strings with toString before
   * PreparedStatement.executeQuery() was called, special values such as NaN were not accepted. In
   * 1.7.0 these values are inserted as nulls (fredt)<b>
   *
   * <p>This test can be extended to cover various conversions through JDBC
   */
  public void testDoubleNaN() {

    double value = 0;
    boolean wasEqual = false;
    String message = "DB operation completed";
    String ddl1 =
        "DROP TABLE t1 IF EXISTS;"
            + "CREATE TABLE t1 ( d DECIMAL, f DOUBLE, l BIGINT, i INTEGER, s SMALLINT, t TINYINT, "
            + "dt DATE DEFAULT CURRENT_DATE, ti TIME DEFAULT CURRENT_TIME, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP );";

    try {
      stmnt.execute(ddl1);

      PreparedStatement ps =
          connection.prepareStatement(
              "INSERT INTO t1 (d,f,l,i,s,t,dt,ti,ts) VALUES (?,?,?,?,?,?,?,?,?)");

      ps.setString(1, "0.2");
      ps.setDouble(2, 0.2);
      ps.setLong(3, java.lang.Long.MAX_VALUE);
      ps.setInt(4, Integer.MAX_VALUE);
      ps.setInt(5, Short.MAX_VALUE);
      ps.setInt(6, 0);
      ps.setDate(7, new java.sql.Date(System.currentTimeMillis()));
      ps.setTime(8, new java.sql.Time(System.currentTimeMillis()));
      ps.setTimestamp(9, new java.sql.Timestamp(System.currentTimeMillis()));
      ps.execute();
      ps.setInt(1, 0);
      ps.setDouble(2, java.lang.Double.NaN);
      ps.setLong(3, java.lang.Long.MIN_VALUE);
      ps.setInt(4, Integer.MIN_VALUE);
      ps.setInt(5, Short.MIN_VALUE);
      ps.setInt(6, 0);

      // allowed conversions
      ps.setTimestamp(7, new java.sql.Timestamp(System.currentTimeMillis() + 1));
      ps.setTime(8, new java.sql.Time(System.currentTimeMillis() + 1));
      ps.setDate(9, new java.sql.Date(System.currentTimeMillis() + 1));
      ps.execute();

      //
      ps.setInt(1, 0);
      ps.setDouble(2, java.lang.Double.POSITIVE_INFINITY);
      ps.setInt(4, Integer.MIN_VALUE);

      // test conversion
      ps.setObject(5, Boolean.TRUE);
      ps.setBoolean(5, true);
      ps.setObject(5, new Short((short) 2), Types.SMALLINT);
      ps.setObject(6, new Integer(2), Types.TINYINT);

      // allowed conversions
      ps.setObject(7, new java.sql.Date(System.currentTimeMillis() + 2));
      ps.setObject(8, new java.sql.Time(System.currentTimeMillis() + 2));
      ps.setObject(9, new java.sql.Timestamp(System.currentTimeMillis() + 2));
      ps.execute();

      ResultSet rs = stmnt.executeQuery("SELECT d, f, l, i, s*2, t FROM t1");
      boolean result = rs.next();

      value = rs.getDouble(2);

      //            int smallintValue = rs.getShort(3);
      int integerValue = rs.getInt(4);

      if (rs.next()) {
        value = rs.getDouble(2);
        wasEqual = Double.isNaN(value);
        integerValue = rs.getInt(4);

        // tests for conversion
        // getInt on DECIMAL
        integerValue = rs.getInt(1);
      }

      rs = stmnt.executeQuery("SELECT MAX(i) FROM t1");

      if (rs.next()) {
        int max = rs.getInt(1);

        System.out.println("Max value for i: " + max);
      }

      {
        stmnt.execute("drop table CDTYPE if exists");

        // test for the value MAX(column) in an empty table
        rs =
            stmnt.executeQuery(
                "CREATE TABLE cdType (ID INTEGER NOT NULL, name VARCHAR(50), PRIMARY KEY(ID))");
        rs = stmnt.executeQuery("SELECT MAX(ID) FROM cdType");

        if (rs.next()) {
          int max = rs.getInt(1);

          System.out.println("Max value for ID: " + max);
        } else {
          System.out.println("Max value for ID not returned");
        }

        stmnt.executeUpdate("INSERT INTO cdType VALUES (10,'Test String');");
        stmnt.executeQuery("CALL IDENTITY();");

        try {
          stmnt.executeUpdate("INSERT INTO cdType VALUES (10,'Test String');");
        } catch (SQLException e1) {
          stmnt.execute("ROLLBACK");
          connection.rollback();
        }
      }
    } catch (SQLException e) {
      fail(e.getMessage());
    }

    System.out.println("testDoubleNaN complete");

    // assert new behaviour
    assertEquals(true, wasEqual);
  }