'Java Sqlite Inner Join returns something but I cannot print it
I have database which has 3 tables, customers, products and Orders. Orders table has 2 foreign keys from the other tables. I want to operate a join operation with these tables. First, let me show you my try on Sqlite:
SELECT c.id, c.name, c.surname, o.productid, o.Datee
FROM customers c INNER JOIN Orders o
ON c.id = o.customerid;
And the result is here
So I used this code in Java as:
package autoparkautomation2;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class test {
public static void main(String[] args) throws SQLException {
Connection con = DriverManager.getConnection("jdbc:sqlite:" + DatabaseInfo.filePath);
String query = "SELECT c.id, c.name, c.surname, o.productid, o.Datee FROM customer c"
+ "INNER JOIN Orders o ON c.id = o.customerid";
PreparedStatement pstmt = con.prepareStatement(query);
ResultSet rs = pstmt.executeQuery();
System.out.println(rs == null); // the statement that checks if rs is null
int i = 0;
while(rs.next()) {
++i;
System.out.println(rs.getString("name") + " " + rs.getString("surname") +
rs.getString("productid") + " " + rs.getString("Datee"));
}
System.out.println(i);
con.close();
}
}
But the while loop isn't executed, and value of the variable i isn't changing. So I thought that rs value is null because nothing returns from the query. However, the statement that checks if rs is null returns false, meaning that query returns something. Furthermore, I tried to print the values of rs just before the while loop without a loop, only a System.out.Println method, and program throws an error as
Exception in thread "main" java.sql.SQLException: ResultSet closed at org.sqlite.core.CoreResultSet.checkOpen(CoreResultSet.java:76) at org.sqlite.jdbc3.JDBC3ResultSet.findColumn(JDBC3ResultSet.java:39) at org.sqlite.jdbc3.JDBC3ResultSet.getString(JDBC3ResultSet.java:446) What's wrong with my code? I scanned a lot of solutions, but many of them were about Android whose codes seemed a little bit confusing and scary. I am newbie to JDBC and Sqlite. Thx in advance for your replies.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
