Our Java program is fetching data randomly from postgreSQL. how can it be resolved? What should be done in order to get data in a sequential order?
We have a table named grocery in our postgresql, which has entries from 0 to 99. the java code is fetching this entire table but in a random order.
connection = DriverManager.getConnection(
"jdbc:postgresql://localhost/test", "postgres",
"manafara");
connection.setAutoCommit(false);
Statement st = connection.createStatement();
String sql = "Select * from grocery";
ResultSet rs = st.executeQuery(sql);
Statement st1 = connection.createStatement();
ResultSet rs1 = st1.executeQuery("Select COUNT(*) AS TOTAL from grocery");
int n = 0;
while (rs1.next()) {
n = rs1.getInt("TOTAL");
}
System.out.println("Count: " + n);
int a = 0;
Double db[][] = new Double[n][2];
while (rs.next()) {
db[a][0] = (double) rs.getInt(4);
db[a][1] = rs.getDouble(6);
a++;
}
order byso the database is free to choose any order it likes. Rows in a relational database are NOT sorted.