简体   繁体   中英

SQLite query returns only one row JDBC JAVA database

  • I have read the previous questions but none of them seem to match my problem although they might seem similar at first. :/_

So, I am working on a local database on Java(JDBC). When I press a button I should be getting the result of a "SELECT" query. So far so good, but for some reason which my beginner brain does not understand I keep getting only one row from the query. I have even run the same exact query on "DB Browser for SQLite" and it returns the correct result (1+ rows) . So this is the method I am using to get the result of the query:

public ResultSet returnBill(int no) throws SQLException{
    String sql = "SELECT * FROM billList WHERE no = " + no + " ;";
    ResultSet thisSet =  stmt.executeQuery(sql); // stmt is a 'Statement' type variable 
    return thisSet;
}

The method does not crash but it only returns the very first row of a query which should return more than 2 ( while (thisSet.next()) RUNS ONCE). I run other "SELECT" queries on the program which are supposed to return more than one rows and they all work fine so it's not a matter of not being able to start/close the connection etc.

Below is the method being used:

int number  =  table.getModel().getValueAt(rows, 0);                            
ResultSet thisSet =  db.returnBill(number);
while (thisSet.next()){
        String name = thisSet.getString("name");
        int quantity = thisSet.getInt("quantity");
        // do something with the returned data 
}

So I get this magical number from a table (of course I made sure it's not 0, -1 etc.) and I run a query using that number. You could think of the structure of the table consisting of columns :

number | name | quantity |

where 'number' is nonzero. I understand that probably using this method to run a query on a DB might not be safe or might post security threats but it's not the case right now. I have been working on this project for quite a long time already and I have been through many silly mistakes and I think this is yet one of them. Any help is APPRECIATED ! :D

So yes, it was a silly mistake as I expected. So I had previously initiated a variable

Database db = new Database();  

which opened the database for 2 queries (the SELECT query and an UPDATE query on another table as shown) which would then be closed at the end of the following code.

When I removed this UPDATE query however the loop executed the correct amount of times. So it seems like the SQLite JDBC is somehow prone to running a SELECT and UPDATE query on the same Statement (as far as my super mega* brain perceives it.)

So I created 2 connections at the very beginning and closed them at the end using one of them for the SELECT and the other one for the UPDATE query:

Database db = new Database(); // open database
Database db2 = new Database(); // open another creepy one :/ 

int number  =  table.getModel().getValueAt(rows, 0);                            
ResultSet thisSet =  db.returnBill(number);
while (thisSet.next()){
        String name = thisSet.getString("name");
        int quantity = thisSet.getInt("quantity");
        // do something with the returned data 
        // --------> STUPIDO <----------
        //** Now executing an UPDATE query on db2 :
        // ex.: UPDATE anotherTable SET amount = (current+ "+ quantity+") WHERE name= '" + name+ "' ;";
}
db.closeConn(); // close db ++ 
db2.closeConn(); // closes db 2

I don't know if this is the best approach but it solved my problem, so I'm leaving it so probably it could help. Any suggestions though would be welcomed :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM