LEEMINJOO / WRITE-ME

오늘의 뉴스 키워드를 뽑아 매일 글감을 주는 웹블로그
1 stars 3 forks source link

자바로 파이썬 실행 #7

Closed LEEMINJOO closed 4 years ago

LEEMINJOO commented 4 years ago
LEEMINJOO commented 4 years ago

방법 1. 자바에서 command로 python 호출 -> python db 바로 접근

자바에서 command로 python 호출 https://m.blog.naver.com/PostView.nhn?blogId=jhnyang&logNo=221407227360&proxyReferer=https%3A%2F%2Fwww.google.com%2F

python db 바로 접근 http://pythonstudy.xyz/python/article/203-MySQL-DML

LEEMINJOO commented 4 years ago
// PythonExample.java
import java.io.*;

public class PythonExample{
    public static void main(String args[]){
        String s = null;
        try{
            System.out.println("This is java");
            Process process = Runtime.getRuntime().exec("python test.py");
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((s = stdInput.readLine()) != null){
                System.out.println(s);
            }
            System.out.println("Success execute python in java!");
            System.exit(0);
        }
        catch (IOException e){
            e.printStackTrace();
            System.exit(-1);
        }
    }
}
# test.py
print('Hi, I am python')
스크린샷 2020-04-09 오후 2 27 17
LEEMINJOO commented 4 years ago

python db 바로 접근 http://pythonstudy.xyz/python/article/203-MySQL-DML

pip install PyMySQL mysqlclient==1.3.13