Java Code Examples for io.vertx.sqlclient.SqlConnection#prepare()

The following examples show how to use io.vertx.sqlclient.SqlConnection#prepare() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public void queries10(SqlConnection sqlConnection) {
  sqlConnection
    .prepare("SELECT * FROM users WHERE id=$1", ar -> {
      if (ar.succeeded()) {
        PreparedStatement preparedStatement = ar.result();
        preparedStatement.query()
          .execute(Tuple.of("julien"), ar2 -> {
            if (ar2.succeeded()) {
              RowSet<Row> rows = ar2.result();
              System.out.println("Got " + rows.size() + " rows ");
              preparedStatement.close();
            } else {
              System.out.println("Failure: " + ar2.cause().getMessage());
            }
          });
      } else {
        System.out.println("Failure: " + ar.cause().getMessage());
      }
    });
}
 
Example 2
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public void usingCursors03(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE age > ?", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement pq = ar1.result();

      // Fetch 50 rows at a time
      RowStream<Row> stream = pq.createStream(50, Tuple.of(18));

      // Use the stream
      stream.exceptionHandler(err -> {
        System.out.println("Error: " + err.getMessage());
      });
      stream.endHandler(v -> {
        System.out.println("End of stream");
      });
      stream.handler(row -> {
        System.out.println("User: " + row.getString("last_name"));
      });
    }
  });
}
 
Example 3
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public void usingCursors01(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE age > ?", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement pq = ar1.result();

      // Create a cursor
      Cursor cursor = pq.cursor(Tuple.of(18));

      // Read 50 rows
      cursor.read(50, ar2 -> {
        if (ar2.succeeded()) {
          RowSet<Row> rows = ar2.result();

          // Check for more ?
          if (cursor.hasMore()) {
            // Repeat the process...
          } else {
            // No more rows - close the cursor
            cursor.close();
          }
        }
      });
    }
  });
}
 
Example 4
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public void usingConnections03(SqlConnection connection) {
  connection.prepare("INSERT INTO USERS (id, name) VALUES (?, ?)", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement prepared = ar1.result();

      // Create a query : bind parameters
      List<Tuple> batch = new ArrayList();

      // Add commands to the createBatch
      batch.add(Tuple.of("julien", "Julien Viet"));
      batch.add(Tuple.of("emad", "Emad Alblueshi"));

      prepared.query().executeBatch(batch, res -> {
        if (res.succeeded()) {

          // Process rows
          RowSet<Row> rows = res.result();
        } else {
          System.out.println("Batch failed " + res.cause());
        }
      });
    }
  });
}
 
Example 5
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public void queries10(SqlConnection sqlConnection) {
  sqlConnection
    .prepare("SELECT * FROM users WHERE id = ?", ar -> {
      if (ar.succeeded()) {
        PreparedStatement preparedStatement = ar.result();
        preparedStatement.query()
          .execute(Tuple.of("julien"), ar2 -> {
            if (ar2.succeeded()) {
              RowSet<Row> rows = ar2.result();
              System.out.println("Got " + rows.size() + " rows ");
              preparedStatement.close();
            } else {
              System.out.println("Failure: " + ar2.cause().getMessage());
            }
          });
      } else {
        System.out.println("Failure: " + ar.cause().getMessage());
      }
    });
}
 
Example 6
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public void usingConnections03(SqlConnection connection) {
  connection.prepare("INSERT INTO USERS (id, name) VALUES ($1, $2)", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement prepared = ar1.result();

      // Create a query : bind parameters
      List<Tuple> batch = new ArrayList();

      // Add commands to the createBatch
      batch.add(Tuple.of("julien", "Julien Viet"));
      batch.add(Tuple.of("emad", "Emad Alblueshi"));

      prepared.query().executeBatch(batch, res -> {
        if (res.succeeded()) {

          // Process rows
          RowSet<Row> rows = res.result();
        } else {
          System.out.println("Batch failed " + res.cause());
        }
      });
    }
  });
}
 
Example 7
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public void queries10(SqlConnection sqlConnection) {
  sqlConnection
    .prepare("SELECT * FROM users WHERE id = $1", ar -> {
      if (ar.succeeded()) {
        PreparedStatement preparedStatement = ar.result();
        preparedStatement.query()
          .execute(Tuple.of("julien"), ar2 -> {
            if (ar2.succeeded()) {
              RowSet<Row> rows = ar2.result();
              System.out.println("Got " + rows.size() + " rows ");
              preparedStatement.close();
            } else {
              System.out.println("Failure: " + ar2.cause().getMessage());
            }
          });
      } else {
        System.out.println("Failure: " + ar.cause().getMessage());
      }
    });
}
 
Example 8
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public void usingConnections03(SqlConnection connection) {
  connection.prepare("INSERT INTO USERS (id, name) VALUES ($1, $2)", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement prepared = ar1.result();

      // Create a query : bind parameters
      List<Tuple> batch = new ArrayList();

      // Add commands to the createBatch
      batch.add(Tuple.of("julien", "Julien Viet"));
      batch.add(Tuple.of("emad", "Emad Alblueshi"));
      batch.add(Tuple.of("andy", "Andy Guibert"));

      prepared.query().executeBatch(batch, res -> {
        if (res.succeeded()) {

          // Process rows
          RowSet<Row> rows = res.result();
        } else {
          System.out.println("Batch failed " + res.cause());
        }
      });
    }
  });
}
 
Example 9
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public void queries10(SqlConnection sqlConnection) {
  sqlConnection
    .prepare("SELECT * FROM users WHERE id= ?", ar -> {
      if (ar.succeeded()) {
        PreparedStatement preparedStatement = ar.result();
        preparedStatement.query()
          .execute(Tuple.of("julien"), ar2 -> {
            if (ar2.succeeded()) {
              RowSet<Row> rows = ar2.result();
              System.out.println("Got " + rows.size() + " rows ");
              preparedStatement.close();
            } else {
              System.out.println("Failure: " + ar2.cause().getMessage());
            }
          });
      } else {
        System.out.println("Failure: " + ar.cause().getMessage());
      }
    });
}
 
Example 10
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public void usingConnections03(SqlConnection connection) {
  connection.prepare("INSERT INTO USERS (id, name) VALUES ($1, $2)", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement prepared = ar1.result();

      // Create a query : bind parameters
      List<Tuple> batch = new ArrayList();

      // Add commands to the createBatch
      batch.add(Tuple.of("julien", "Julien Viet"));
      batch.add(Tuple.of("emad", "Emad Alblueshi"));

      prepared.query().executeBatch(batch, res -> {
        if (res.succeeded()) {

          // Process rows
          RowSet<Row> rows = res.result();
        } else {
          System.out.println("Batch failed " + res.cause());
        }
      });
    }
  });
}
 
Example 11
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public void usingCursors01(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar0 -> {
    if (ar0.succeeded()) {
      PreparedStatement pq = ar0.result();

      // Cursors require to run within a transaction
      connection.begin(ar1 -> {
        if (ar1.succeeded()) {
          Transaction tx = ar1.result();

          // Create a cursor
          Cursor cursor = pq.cursor(Tuple.of("julien"));

          // Read 50 rows
          cursor.read(50, ar2 -> {
            if (ar2.succeeded()) {
              RowSet<Row> rows = ar2.result();

              // Check for more ?
              if (cursor.hasMore()) {
                // Repeat the process...
              } else {
                // No more rows - commit the transaction
                tx.commit();
              }
            }
          });
        }
      });
    }
  });
}
 
Example 12
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public void usingCursors03(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar0 -> {
    if (ar0.succeeded()) {
      PreparedStatement pq = ar0.result();

      // Streams require to run within a transaction
      connection.begin(ar1 -> {
        if (ar1.succeeded()) {
          Transaction tx = ar1.result();

          // Fetch 50 rows at a time
          RowStream<Row> stream = pq.createStream(50, Tuple.of("julien"));

          // Use the stream
          stream.exceptionHandler(err -> {
            System.out.println("Error: " + err.getMessage());
          });
          stream.endHandler(v -> {
            tx.commit();
            System.out.println("End of stream");
          });
          stream.handler(row -> {
            System.out.println("User: " + row.getString("last_name"));
          });
        }
      });
    }
  });
}
 
Example 13
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public void usingConnections02(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement pq = ar1.result();
      pq.query().execute(Tuple.of("andy"), ar2 -> {
        if (ar2.succeeded()) {
          // All rows
          RowSet<Row> rows = ar2.result();
        }
      });
    }
  });
}
 
Example 14
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public void usingConnections02(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement pq = ar1.result();
      pq.query().execute(Tuple.of("julien"), ar2 -> {
        if (ar2.succeeded()) {
          // All rows
          RowSet<Row> rows = ar2.result();
        }
      });
    }
  });
}
 
Example 15
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public void usingCursors01(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar0 -> {
    if (ar0.succeeded()) {
      PreparedStatement pq = ar0.result();

      // Cursors require to run within a transaction
      connection.begin(ar1 -> {
        if (ar1.succeeded()) {
          Transaction tx = ar1.result();

          // Create a cursor
          Cursor cursor = pq.cursor(Tuple.of("julien"));

          // Read 50 rows
          cursor.read(50, ar2 -> {
            if (ar2.succeeded()) {
              RowSet<Row> rows = ar2.result();

              // Check for more ?
              if (cursor.hasMore()) {
                // Repeat the process...
              } else {
                // No more rows - commit the transaction
                tx.commit();
              }
            }
          });
        }
      });
    }
  });
}
 
Example 16
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public void usingCursors03(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar0 -> {
    if (ar0.succeeded()) {
      PreparedStatement pq = ar0.result();

      // Streams require to run within a transaction
      connection.begin(ar1 -> {
        if (ar1.succeeded()) {
          Transaction tx = ar1.result();

          // Fetch 50 rows at a time
          RowStream<Row> stream = pq.createStream(50, Tuple.of("julien"));

          // Use the stream
          stream.exceptionHandler(err -> {
            System.out.println("Error: " + err.getMessage());
          });
          stream.endHandler(v -> {
            tx.commit();
            System.out.println("End of stream");
          });
          stream.handler(row -> {
            System.out.println("User: " + row.getString("last_name"));
          });
        }
      });
    }
  });
}
 
Example 17
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public void usingCursors03(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar0 -> {
    if (ar0.succeeded()) {
      PreparedStatement pq = ar0.result();

      // Streams require to run within a transaction
      connection.begin(ar1 -> {
        if (ar1.succeeded()) {
          Transaction tx = ar1.result();

          // Fetch 50 rows at a time
          RowStream<Row> stream = pq.createStream(50, Tuple.of("julien"));

          // Use the stream
          stream.exceptionHandler(err -> {
            System.out.println("Error: " + err.getMessage());
          });
          stream.endHandler(v -> {
            tx.commit();
            System.out.println("End of stream");
          });
          stream.handler(row -> {
            System.out.println("User: " + row.getString("last_name"));
          });
        }
      });
    }
  });
}
 
Example 18
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public void usingConnections02(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE ?", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement prepared = ar1.result();
      prepared.query().execute(Tuple.of("julien"), ar2 -> {
        if (ar2.succeeded()) {
          // All rows
          RowSet<Row> rows = ar2.result();
        }
      });
    }
  });
}
 
Example 19
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public void usingCursors01(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar0 -> {
    if (ar0.succeeded()) {
      PreparedStatement pq = ar0.result();

      // Cursors require to run within a transaction
      connection.begin(ar1 -> {
        if (ar1.succeeded()) {
          Transaction tx = ar1.result();

          // Create a cursor
          Cursor cursor = pq.cursor(Tuple.of("julien"));

          // Read 50 rows
          cursor.read(50, ar2 -> {
            if (ar2.succeeded()) {
              RowSet<Row> rows = ar2.result();

              // Check for more ?
              if (cursor.hasMore()) {
                // Repeat the process...
              } else {
                // No more rows - commit the transaction
                tx.commit();
              }
            }
          });
        }
      });
    }
  });
}
 
Example 20
Source File: SqlClientExamples.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public void usingConnections02(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement pq = ar1.result();
      pq.query().execute(Tuple.of("julien"), ar2 -> {
        if (ar2.succeeded()) {
          // All rows
          RowSet<Row> rows = ar2.result();
        }
      });
    }
  });
}