Search code examples
javasqlsql-serveroracle-databasesql-query-store

SELECT query with ResultSet arguments in JAVA


The server code of my Oracle database contains two files (classes) namely: Main.javaand DBManager.java. DBManager.java contains SQLPRocessing and SQLProcess methods (functions) that perform executeUpdate and executeQuery operations with the query statement, respectively. The SQL queries for dumping and fetching the data are written at the Main.java class. My database contains two tables T1and T2. My code has two queries for inserting the data as:

public void SQLProcessing(String ID, String X, String Y, String XE, String YE){
    db.SQLProcess("INSERT INTO T1 VALUES("+Double.parseDouble(ID)+ "," +Double.parseDouble(X)+ "," +Double.parseDouble(Y)+")");
    db.SQLProcessing("INSERT INTO T2 VALUES("+Double.parseDouble(ID)+ "," +Double.parseDouble(XE)+ "," +Double.parseDouble(YE)+")");                    
}

Similarly, for getting the data back from the database, I use single SELECT query as:

public ResultSet SQLProcess(String msg1, String msg2, String msg3, String msg4, String msg5){
    ResultSet rs = db.SQLProcess("SELECT * FROM T1, T2 WHERE T1.ID = T2.ID");
    return rs;
}

While executing the code, I get an error:

Exception in thread "Thread-1" java.lang.NullPointerException at Main.sendTo(Main.java:132) at Main$ServerReceiver.run(Main.java:79)

The line 132 of Main.java contains while(rs.next()) and line 79 contains sendTo(socket.getInetAddress()); . However, when I see the database, data is sent and stored in the database. So, I think the problem is with the SELECT query. I tried many possible SELECT queries from different threads, I could not solve the problem. Is my guess correct? Could anybody provide the real SELECT query for my case? Or is the error is triggered from another source?

The problem is with the cursor. It is null and I added an if statement as:

if(rs!=null) {
        while(rs.next()){//code} 

Then it does not generate any error. However, the client device does not get any data from the server. How can I fix it?


Solution

  • Have you checked if (T1.ID = T2.ID) there are IDs which equal each other? Otherwise your queryresponse is empty because your where case declines a result.

    Sometimes there is a extra column at first place. So maybe your data is not inserted correctly?