doutao1282 2013-11-15 17:47
浏览 25
已采纳

从php调用外部java类[关闭]

I'm starting a website project that requires to communicate with external oracle databases. The oracle databases are managed by a system written in java. I was wondering if it is possible for me to use those java classes from within a website using php.

As an example i'm trying to achieve something like : shell_exec("path/java GetSomething") but with the "path/java" stored on a remote server.

Am I giving this the right approach? will this cause security issues?

  • 写回答

2条回答 默认 最新

  • douge3830 2013-11-15 18:26
    关注

    One way to do it would be to make a JSP web service consumable by php. This is a very basic start, but the idea is, you have a jsp which lives on the java server, and it can be called over the web. It takes parameters, then hits the database with those parameters, and returns it. PHP makes a call to this service and reads the data it returns.

    DatabaseService.jsp

    String query = Request.getParameter('query');
    ResultSet rs = MyDbClass.executeQuery(query);
    
    
    ResultSetMetaData rsmd = rs.getMetaData();
    int colCount           = rsmd.getColumnCount();
    
    while (rs.next())
    {
        for (int i = 1; i <= colCount; i++)
        {
            String columnName = rsmd.getColumnName(i);
            Object value      = rs.getObject(i);
            out.println(columnName+":"+value.toString()+'\t');
        }
        out.println("
    ");
    }
    

    oracleConnect.php

    $query=urlencode("SELECT * FROM DB.table");
    $results=file_get_contents("http://remoteServer.com/DatabaseService.jsp?query=$query");
    

    This is highly simplified, you will need to output the data some way consumable by php like xml, as well as sending other parameters, an api key, using CURL instead of file_get_contents, any number of things, but this basic model should get you started in the right direction.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部