admin管理员组文章数量:1355529
I am working on a project using GridDB's Java API and I am facing an issue with parameterized queries. I created a container with two columns (an integer column id and a string column value), inserted some sample data, and then attempted to retrieve records using a parameterized query. However, when I bind a parameter (for example, to fetch all records where id >= 5), the query unexpectedly returns an empty result set—even though I have matching records in the container.
My code:
import com.toshiba.mwcloud.gs.*;
import java.util.*;
public class GridDBExample {
public static void main(String[] args) {
try {
// Establish connection to GridDB
GridStore store = GridStoreFactory.getInstance().getStore(
"notificationAddress=239.0.0.1;notificationPort=31999;clusterName=defaultCluster;user=admin;password=admin;"
);
// Define container schema and create container
ContainerInfo containerInfo = new ContainerInfo(
"sample_container",
ContainerType.COLLECTION,
new String[]{"id", "value"},
new Type[]{Type.INTEGER, Type.STRING}
);
Collection con = store.putContainer(containerInfo);
// Insert sample data
for (int i = 0; i < 10; i++) {
con.put(new Object[]{i, "value" + i});
}
// Parameterized query: expecting to retrieve rows where id >= 5
Query<Row> query = con.query("SELECT * WHERE id >= ?");
query.bind(5);
RowSet<Row> rs = query.fetch();
// Iterate and print results
while (rs.hasNext()) {
Row row = rs.next();
System.out.println("id: " + row.getInteger(0) + ", value: " + row.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
I am using the latest version of the GridDB Java API (if there's a specific version, please mention it in your comments).
I am working on a project using GridDB's Java API and I am facing an issue with parameterized queries. I created a container with two columns (an integer column id and a string column value), inserted some sample data, and then attempted to retrieve records using a parameterized query. However, when I bind a parameter (for example, to fetch all records where id >= 5), the query unexpectedly returns an empty result set—even though I have matching records in the container.
My code:
import com.toshiba.mwcloud.gs.*;
import java.util.*;
public class GridDBExample {
public static void main(String[] args) {
try {
// Establish connection to GridDB
GridStore store = GridStoreFactory.getInstance().getStore(
"notificationAddress=239.0.0.1;notificationPort=31999;clusterName=defaultCluster;user=admin;password=admin;"
);
// Define container schema and create container
ContainerInfo containerInfo = new ContainerInfo(
"sample_container",
ContainerType.COLLECTION,
new String[]{"id", "value"},
new Type[]{Type.INTEGER, Type.STRING}
);
Collection con = store.putContainer(containerInfo);
// Insert sample data
for (int i = 0; i < 10; i++) {
con.put(new Object[]{i, "value" + i});
}
// Parameterized query: expecting to retrieve rows where id >= 5
Query<Row> query = con.query("SELECT * WHERE id >= ?");
query.bind(5);
RowSet<Row> rs = query.fetch();
// Iterate and print results
while (rs.hasNext()) {
Row row = rs.next();
System.out.println("id: " + row.getInteger(0) + ", value: " + row.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
I am using the latest version of the GridDB Java API (if there's a specific version, please mention it in your comments).
Share Improve this question edited Mar 31 at 16:13 TylerH 21.1k78 gold badges79 silver badges114 bronze badges asked Mar 29 at 23:59 Zaigham Ali AnjumZaigham Ali Anjum 511 silver badge6 bronze badges 1 |1 Answer
Reset to default 0To fix these errors, I updated My code as follows:
Missing Import for Date Class
The Date
class is not recognized because it is not imported. (import java.util.Date;)
Undefined TimeSeries
Class
The TimeSeries
class is missing an import statement. (import com.toshiba.mwcloud.gs.TimeSeries;)
package gsSample;
import java.util.Arrays;
import java.util.Properties;
import java.util.Date; // Import Date class
import com.toshiba.mwcloud.gs.Collection;
import com.toshiba.mwcloud.gs.GSException;
import com.toshiba.mwcloud.gs.GridStore;
import com.toshiba.mwcloud.gs.GridStoreFactory;
import com.toshiba.mwcloud.gs.Query;
import com.toshiba.mwcloud.gs.RowKey;
import com.toshiba.mwcloud.gs.RowSet;
import com.toshiba.mwcloud.gs.TimeSeries; // Import TimeSeries class
public class MyApp2 {
// Container schema
static class Person {
@RowKey String name;
int age;
}
static class HeartRate {
@RowKey Date ts;
int heartRate; // Correct field name
String activity;
}
public static void main(String[] args) throws GSException {
// Create a Properties instance to connect to GridDB
Properties props = new Properties();
props.setProperty("notificationAddress", "239.0.0.1");
props.setProperty("notificationPort", "31999");
props.setProperty("clusterName", "defaultCluster");
props.setProperty("user", "administrator");
props.setProperty("password", "password");
GridStore store = GridStoreFactory.getInstance().getGridStore(props);
// Create a person object (fix person1 issue)
Person person1 = new Person();
person1.name = "JohnDoe"; // Assign a valid name
person1.age = 30;
// Get the container to write records
Collection<String, Person> people = store.putCollection("PEOPLE", Person.class);
people.put(person1); // Add a person record
// Write a record
HeartRate hr = new HeartRate();
hr.ts = new Date(); // Fix Date issue
hr.heartRate = 60; // Fix case-sensitive issue
hr.activity = "resting";
// Create TimeSeries and insert heart rate data
TimeSeries<HeartRate> heartRate = store.putTimeSeries("HR_" + person1.name, HeartRate.class);
heartRate.put(hr);
}
}
本文标签: Parameter binding issue in GridDB Java API query returns empty result setStack Overflow
版权声明:本文标题:Parameter binding issue in GridDB Java API query returns empty result set - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744000880a2573835.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
FROM
clause? – Paul T. Commented Mar 30 at 0:03