0

I'm trying to execute the code below. First sql query gave me some needed 'IMSI' number which I store in 's' variable and than I used it to select second and third select query.

When I exclude second query everything went OK. Also when I replace "+s+" with '21901123456456' which is value of s variable, code finish successfully. But when I execute code as below I got error from subject.

import java.sql.*;
import java.util.Scanner;
class stanjeBroja{  
public static void main(String args[]){  
    Scanner reader = new Scanner(System.in);
    System.out.println("Unesi MSISDN");
    // get user input for min range
    String min=reader.nextLine();
    String s = "";
    try{    
        //step1 load the driver class  
        Class.forName("oracle.jdbc.driver.OracleDriver");  
        //step2 create  the connection object  
        Connection con=DriverManager.getConnection(  
        "jdbc:oracle:thin:@ip:port:sid","user","pass");  
        //step3 create the statement object  
        Statement stmt=con.createStatement();  
        //step4 execute query  
        ResultSet rs=stmt.executeQuery("SELECT 
SUBSCRIBERKEY,ALTERNATESUBSCRIBERKEY FROM 
TERTIOTEST.ALTERNATESUBSCRIBERKEY 
WHERE ALTERNATESUBSCRIBERKEY ="+min+"");  
        while(rs.next()){  
            System.out.println("IMSI: "+rs.getString(1)+"  MSISDN: 
"+rs.getString(2)+"  ");  
            s = rs.getString(1);
        }
        ResultSet rs1=stmt.executeQuery("SELECT servicename FROM 
TERTIOTEST.SUBSCRIBERSERVICE WHERE SUBSCRIBERKEY = "+s+"AND servicename 
IN 
('STD','VPNNUM','HLR','VPNUNR','STDHYB','VOXX','VPNFA')");  
        while(rs1.next()){  
            System.out.println("SERVISI: "+rs1.getString(1)+"  ");  
        }
        ResultSet rs2=stmt.executeQuery("SELECT PARAMETERVALUE FROM 
TERTIOTEST.SERVICEPARAMETER WHERE SUBSCRIBERKEY = "+s+"AND servicename = 
'TARIFF' AND PARAMETERNAME = 'tariff'");  
        while(rs2.next()){  
            System.out.println("TARIFA: "+rs2.getString(1)+"  ");  
        }
        //step5 close the connection object  
        con.close();   
    }
    catch(Exception e){
        System.out.println(e);
    }   
    }  
}  
  • 1
    [Please, please, please use bind variables instead of string concatenation](https://stackoverflow.com/q/37156169/3419894), which coincidentally would make the error plainly obvious to you. You're missing a whitespace before an `AND` in several places. – JonK Jun 13 '19 at 12:27

1 Answers1

0

you select string needs some improvements

  • space before AND
  • s is a string so you have to pass it a string in your select so add ' around. to avoid that, you should use prepared Statement!
"SELECT PARAMETERVALUE FROM 
    TERTIOTEST.SERVICEPARAMETER WHERE SUBSCRIBERKEY = '"+s+"' AND servicename = 
    'TARIFF' AND PARAMETERNAME = 'tariff'"

you should check both of your select statements

hotfix
  • 3,376
  • 20
  • 36
  • thank you very much. Of course single quote was the problem. I have spotted space before 'AND' earlier and it works without it. – Alen Serezlija Jun 13 '19 at 12:43