Example #1
0
  public void testXmlGeneratedKeys() throws SQLException {

    if (this.checkConnected(dbName) == false) {
      return;
    }

    QueryRunnerService runner = null;
    Map<String, Object> values = new HashMap<String, Object>();

    final QueryStructure defaultStructure = DBUpdateQueryStructure.updateXmlGeneratedKeysDS(values);

    QueryStructure structure =
        new QueryStructure(values) {

          @Override
          public void create(QueryRunnerService runner) throws SQLException {
            this.values.put(
                "createUpdatedCount",
                (Integer) runner.update(DBConstants.CREATE_STUDENT_TABLE_MSSQL));
          }

          @Override
          public void execute(QueryRunnerService runner) throws SQLException {
            defaultStructure.execute(runner);
          }

          @Override
          public void drop(QueryRunnerService runner) throws SQLException {
            defaultStructure.drop(runner);
          }
        };

    runner = MjdbcFactory.getQueryRunner(this.dataSource);

    DBUpdate.updateGeneratedKeysDS(structure, runner);

    runner = MjdbcFactory.getQueryRunner(this.conn);

    DBUpdate.updateGeneratedKeysDS(structure, runner);
  }
  public static void main(String[] args) throws SQLException {
    Connection conn = DerbyParameters.createConnection();

    QueryRunnerService runner = MjdbcFactory.getQueryRunner(conn);

    try {
      runner.update(
          "CREATE TABLE students ("
              + "id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),"
              + "name VARCHAR(24) NOT NULL,"
              + "address VARCHAR(1024),"
              + "CONSTRAINT primary_key PRIMARY KEY (id))");

      runner.update(
          "INSERT INTO students (name, address) VALUES ('Not me', 'unknown')",
          new RowCountOutputHandler<Integer>(),
          new Object[0]);

      final HashMap<String, Object> tableParams = new HashMap<String, Object>();
      tableParams.put("id", 1);

      final HashMap<String, Object> studentParams = new HashMap<String, Object>();
      studentParams.put("address", "unknown");

      HashMap<String, Map<String, Object>> paramsList = new HashMap<String, Map<String, Object>>();
      paramsList.put("table", tableParams);
      paramsList.put("student", studentParams);

      MapListInputHandler input =
          new MapListInputHandler(
              "SELECT name FROM students WHERE id = :table.id AND address = :student.address",
              paramsList);

      Map<String, Object> result = runner.query(input, new MapOutputHandler());

      System.out.println("Query output: " + result);

    } catch (SQLException ex) {
      ex.printStackTrace();
    } finally {
      runner.update("DROP TABLE students");
    }
  }