== Using connections

When you need to execute sequential queries (without a transaction), you can create a new connection
or borrow one from the pool:

[source,groovy]
----
Code not translatable
----

Prepared queries can be created:

[source,groovy]
----
connection.prepare("SELECT * FROM users WHERE first_name LIKE ?", { ar1 ->
  if (ar1.succeeded()) {
    def pq = ar1.result()
    pq.execute(Tuple.of("julien"), { ar2 ->
      if (ar2.succeeded()) {
        // All rows
        def rows = ar2.result()
      }
    })
  }
})

----

NOTE: prepared query caching depends on the `link:../../apidocs/io/vertx/sqlclient/SqlConnectOptions.html#setCachePreparedStatements-boolean-[setCachePreparedStatements]` and
does not depend on whether you are creating prepared queries or use `link:../../apidocs/io/vertx/sqlclient/SqlClient.html#preparedQuery-java.lang.String-io.vertx.core.Handler-[direct prepared queries]`

`link:../../apidocs/io/vertx/sqlclient/PreparedQuery.html[PreparedQuery]` can perform efficient batching:

[source,groovy]
----
connection.prepare("INSERT INTO USERS (id, name) VALUES (?, ?)", { ar1 ->
  if (ar1.succeeded()) {
    def prepared = ar1.result()

    // Create a query : bind parameters
    def batch = []

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

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

        // Process rows
        def rows = res.result()
      } else {
        println("Batch failed ${res.cause()}")
      }
    })
  }
})

----