Querying the Database

Writing the data

Other Usage

Misc

Query Concept

The Concept before start

The most straightforward expression that produces results from the DB is a SQL query.To issue a query with a SqlMan Handle, we have to, at least:

We’ll now look at each of the points above.

Create the query

We assume that the query is ready to be executed, it's pure SQL statement. Of course, the connection is already established. Then we create a new Sql instance, pass conn to the constructor and SQL statement to input() method.

Map<String, Object> result = new Sql(conn).input("SELECT * FROM shop_address").query(); // fetch the first one

It's important to note that input() method accepts SQL statement and its parameters. The query is not executed until the query() method is called.

Currently we didn't pass any parameters to the query. We'll talk about how to pass parameters in the next section.

Here is a question about how to represent the result(s), it can be divded into two parts:

SQlMan abstracts away from the JDBC ResultSet, which has a quite cumbersome API.

Therefore, it offers several possibilities to access the columns resulting from a query or some other statement that returns a result.

From the return structure of value(s), we can identify several types:

These types correspond to the following methods in SqlMan:

Here are complete examples:

Map<String, Object> result = new Sql(conn).input("SELECT * FROM shop_address").query(); // fetch the first one
int result = new Sql(conn).input("SELECT COUNT(*) AS total FROM shop_address").queryOne(int.class); // fetch the first one
List<Map<String, Object>> result = new Sql(conn).input("SELECT * FROM shop_address").queryList();

Return Format

There is the last question: Which format to represent the result(s)?

---- We’ll now see the simplest ones: Map, we’ll see the more complex ones in the next section.