Reputation: 843
I am working with java Vert.x project and connected to the MySQL using the Reactive MySQL Client using the guide of vert.x official Documentation. When I tried to retrieve data from SELECT
query it always gives empty result even there are data in the DB. The method that was implemented to retrieve data is given below.
client.getConnection(conRes -> {
if(conRes.succeeded()) {
logger.info("Connected to DB !!!");
SqlConnection conn = conRes.result();
String query = "SELECT * FROM table where id ="+id;
conn.query(query, res-> {
logger.info("Query Executed for retrieve data, id: "+id);
if(res.succeeded()) {
RowSet<Row> result = res.result();
if(result.size() > 0) { //Also tried with result.rowCount()
Row rowRes = result.iterator().next();
stringPromise.complete(rowRes.getString("reference_number"));
} else {
logger.error("No data reference number for id: "+id); // Always endup with here
stringPromise.fail("No data reference number for id: "+id);
}
conn.close();
} else {
logger.error("Query executed failed due to "+res.cause().getMessage());
res.cause().printStackTrace();
stringPromise.fail(res.cause());
conn.close();
}
});
}
});
Connection properties for the above method are,
MySQLConnectOptions connectOptions = new MySQLConnectOptions()
.setPort(3306)
.setHost(host_ip)
.setDatabase(db_name)
.setUser(username)
.setPassword(password);
// Pool options
PoolOptions poolOptions = new PoolOptions()
.setMaxSize(5);
client = MySQLPool.pool(vertx, connectOptions, poolOptions);
The method shows no errors, but always gives o result and shows logger.error("No data reference number for id: "+id);
. What is the issue and how can I fix this issue.
Upvotes: 0
Views: 342
Reputation: 96
Can you try to make a reproducer by either creating a small project or adding a test to the project? The code you put here seems normal to me.
Upvotes: 1
Reputation: 3040
Is your id a string? Because then you have to put it between apostrophes:
String query = String.format("SELECT * FROM table where id='%s'", id);
Upvotes: 1