Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connecting to PHP Databases with Java

Status
Not open for further replies.

ACRRHODES

Programmer
May 16, 2000
33
CA
Whoa! I had my hands full trying to do this, but I finally found a way. It ain't purdy, but here it is:

Step 1: Make the following into a PHP file
[tt]
<?php
mysql_connect(&quot;localhost&quot;,username,password) or die (&quot;could not connect to database&quot;);
mysql_select_db(your_database) or die (&quot;could not select database&quot;);
$q = &quot;select $query from $database;&quot;;
$result = mysql_query(&quot;$q&quot;) or die (&quot;Could not query database&quot;.mysql_error());
while ($row = mysql_fetch_array($result))
{
$data = eval(&quot;return \$row[$query];&quot;);
echo &quot;$data\n&quot;;
}
?>
[/tt]
Note that you must change &quot;username&quot;, &quot;password&quot; and &quot;your database&quot; (and possibly &quot;localhost&quot;).


Step 2: Add this method to your Java program
[tt]
private static String[] getValues(String database,String query)
{
String[] ret = new String[max_amount_of_data];
try
{
URL PHP = new URL(&quot; URLConnection a = PHP.openConnection();
a.connect();
InputStreamReader b = new InputStreamReader(a.getInputStream());
BufferedReader data = new BufferedReader(b);
int counter;
String reply = &quot;&quot;;
for(counter=0;reply!=null;counter++)
{
reply = data.readLine();
ret[counter] = reply;
}
}
catch (IOException e)
{
System.out.println(&quot;IO Error&quot; + e.getMessage());
}
return ret;
}
[/tt]
Note that in this one you must change max_amount_of_data and PHP_name_and_location.

Then merely use getValues to query your database:
i.e.
String[] stuentnamelist = getValues(&quot;students&quot;,&quot;fullname&quot;)

Enjoy![pc3] ***
Dammit Jim, I'm a programmer, not a doctor.
 
PHP is not a database. MySQL is the database that is most often used with PHP Applications and is the one used in your example. A much more efficient, easier, and powerful solution to this problem is to use a JDBC Driver for MySQL (there are a few).
 
The database I was trying to connect to was identified as a PHPMySQL Database. That's what I meant. I couldn't find a driver that worked specifically with this database, and this method works just fine. ***
Dammit Jim, I'm a programmer, not a doctor.
 
Trust me, this is not the way to connect to MySQL in Java. To write a simple Client-Server Application you would need a dedicated Web Browser just to handle the database connections. How is this good? The performance is going to be terrible and you definitely won't be able to take advantage of key features in JDBC like PreparedStatements and CallableStatements. How would you pass a byte array to store in the database as a BLOB?

Again, this database is MySQL. It has nothing to do with PHP other than being commonly used for the database backend of most PHP sites. Here is a link to a good MySQL JDBC driver:
Cheers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top