0

I'm trying to connect python to BigSQL.I have a java code which can connect python to BigSQL & retrieve data from BigSQL.Below is my sample code

 public class Hello {

  public static void main(String[] args) {
    try(Connection con = DriverManager.getConnection("connection details")) {

Class.forName("com.ibm.db2.jcc.DB2Driver");
    Statement stmt = con.createStatement();
    System.out.println("Connected to BigSQL");      
    ResultSet result = stmt.executeQuery("select *  from table limit 10");
    while (result.next()) {
      //Retrieve by com_name
      String com_name = result.getString(1);
      //Retrieve by family
      String family = result.getString(2); 
      //Retrieve by sci_name
      String sci_name = result.getString(3);
      //Retrieve by symbol
      String symbol = result.getString(4);
      //Retrieve by synonym
      String synonym = result.getString(5);
     System.out.println(com_name+":"+family+":"+sci_name+":"+symbol+":"+synonym);
    }

    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } 
  }

}  

I wrote a python code to invoke this java code using my python

import os
import os.path,subprocess
from subprocess import STDOUT,PIPE
path='Location where my .java file is'
os.chdir(path)
def compile_java(java_file):
    subprocess.check_call(['javac', java_file])


def execute_java(java_file):
    java_class,ext = os.path.splitext(java_file)
    cmd = ['java', java_class]

compile_java('Hello.java')      
execute_java("Hello")

My python code is running successfully but I'm not able to retrieve the java output mentioned in

System.out.println(com_name+":"+family+":"+sci_name+":"+symbol+":"+synonym);

Can you please help me?

Thanks in Advance

Python Learner
  • 437
  • 2
  • 11
  • 28

1 Answers1

0

You need to execute the call to java and use check_output to get the java output. check_call just returns the returncode:

def execute_java(java_file):
    java_class,ext = os.path.splitext(java_file)
    cmd = ['java', java_class]
    print(cmd)
    javaStdout =subprocess.check_output(['java', java_class])
    print ('>>', javaStdout)

execute_java("HelloWorld")

The returns:

['java', 'HelloWorld']
>> b'Hello World!\n'
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
  • Thank you @Maurice Meyer Using check_output, I'm getting output >> b'' for the java code mentioned in topmost post But I'm expecting 10 lines of output in format com_name+":"+family+":"+sci_name+":"+symbol+":"+synonym. Any idea why it is happening? When I'm running the java code using command prompt with java command then I'm getting 10 lines of output with com_name+":"+family+":"+sci_name+":"+symbol+":"+synonym format – Python Learner May 04 '17 at 18:50
  • I was using: `System.out.println("Hello World!");` and ran it from console. You could try shell=True and print stderr as definied in the [docs](https://docs.python.org/2/library/subprocess.html?highlight=check_output#subprocess.check_output) – Maurice Meyer May 04 '17 at 19:01
  • Thanks @Maurice Meyer. It helped – Python Learner May 05 '17 at 12:44