tags:

views:

138

answers:

7

I want to access a MySQL database from Java, but remote connection is disabled by the host. So I will send the data to PHP and then PHP will locally access the database. The data is pretty big (about 2~4kb) I've never done this before. What should I do?

A: 

Sounds like your only option is to create a Web Service. You can use Apache's HttpClient in Java to create requests and have your PHP return a response that you can easily parse. Depending on what you are trying to do, an XML response might be overkill. I've used JSON in the past if I know exactly what I am getting back.

Chris J
Saying that XML is "overkill" is misleading. Json is more concise, so it will consume less bandwith, however it's not as human readable as XML. If the goal is to reduce the amount of data being passed between the systems and you don't care about human readability, consider devising your own protocol (perhaps starting with csv and adding whatever additional metadata you require).
Todd R
A: 

Are you saying that you are going to run java from php, so that php has the handle to the mysql database and you will pass the data to java?

check out java php bridge

Kris.Mitchell
+2  A: 

Probably the best way would be to write a web service in PHP that you call from Java.

Another somewhat easier way would be to use the HttpURLConnection class to simply do a POST of the data to the PHP page.

If the PHP page is publicly accessible, you'd want to make sure you have some kind of authentication mechanism in place and use HTTPS as well.

Eric Petroelje
+1  A: 

Http web-services

this. __curious_geek
A: 

I assume the Java program will be the client. I think the simplest would be to just create a simple PHP page, POST any possible parameters to that page from Java, and return the data as a JSON structure.

There are a number of JSON tools for Java and PHP listed on JSON page: http://json.org/

fish
A: 

You might want to have a look to Thrift:

http://incubator.apache.org/thrift/

Roberto Aloi
A: 

I would be very curious to see benchmarks between this PHP / Java bridge and Thrift...

Alex