472,958 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Trying to marry php, java, and mysql

I am trying to use php as a kind of servlet to act as a middle man
between a java applet and mysql. I know java has jdbc but it's flakey
and painful. php access to mysql is much nicer. So I have:
1. An html page that holds the applet.
2. a php page that accepts data submitted to it by the applet via the
$_POST array and writes it to the mysql database. This page never
makes it to the browser window.
3. a simple Thank you page that shows up after the applet sends the
data.
Trouble is, nothing makes it into the mysql database. I've tested each
component seperately. I know they work. I know the applet data is
posted cuz I tested it with the usual query.php. But when I put them
together nothing shows up in the database.
What did I do wrong???
Heres the java applet. The page that holds it has nothing but this
applet.

import java.awt.*;
import java.net.*;
import java.io.*;
import java.applet.*;

public class CGISQLPost extends Applet
{
public void init()
{
try
{
URL url = new URL("http://localhost/tmp/CGISQLPost.php");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
PrintWriter out = new
PrintWriter(connection.getOutputStream());
out.print("id="+URLEncoder.encode("4","UTF-8")+"&name="+URLEncoder.encode("Apchar","UTF-8")+"&title="+URLEncoder.encode("Chieftan","UTF-8")+"\n");
out.close();
} catch(IOException e)
{
System.out.println("Oops"+e); return;
}
repaint();
try
{
getAppletContext().showDocument(new
URL("http://localhost/tmp/thanks.html"));
} catch (MalformedURLException e)
{
System.out.println("Oops"+e); return;
}

}

public void paint(Graphics g)
{
g.drawString("Sending",1,14);
}
}

Here is the php page that processes the data.

<html>
<head>
<title>CGISQLPost</title>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
</head>
<body>
<?php
$id = $_POST[id];
$name = $_POST[name];
$title = $_POST[title];
$conn = mysql_connect("localhost","web","webster");
mysql_select_db("test", $conn);
$str = "insert into mytest values ($id,'$name','$title')";
mysql_query($str, $conn) or die(mysql_error());
mysql_close($conn);
?>
</body>
</html>
Jul 17 '05 #1
5 3309
Hi,

this is quite an interesting setup you got there. Personally, I have
never tried this using an Java Applet but did create environments
where JDBC, PHP and
a database were involved.

Have you tried creating an ODBC Trace in order to see how far the
applet gets when accessing the database ?

Also, have you tried this data entry using non-unicode data ?
MySQL only recently started supporting Unicode so I assume you
are using the latest Version and it might be worth running a test
using non-unicode data (and a non-unicode DB).

Also, which Version of PHP are you working with at this stage ?

You said that you tested all components seperately, so I would imagine
that
you can successfully enter data into MySQL using PHP without the
applet as
a front-end. If this is the case the problem must be inbetween the
applet
and PHP itself. Either way, an ODBC Trace and doublechecking the PHP
make
should shed more light on the cause of this problem. Regarding the
PHP-ODBC
interface you might want to have a look at http://www.iodbc.org.

I hope you will find this to be of help.

Kind Regards,

Jan Csisko
Professional Services Consultant
OpenLink Software Web: http://www.openlinksw.com
Product Weblogs: Virtuoso:
http://www.openlinksw.com/weblogs/virtuoso
UDA: http://www.openlinksw.com/weblogs/uda
Universal Data Access & Virtual Database Technology Providers

ap****@yahoo.com (apchar) wrote in message news:<29*************************@posting.google.c om>...
I am trying to use php as a kind of servlet to act as a middle man
between a java applet and mysql. I know java has jdbc but it's flakey
and painful. php access to mysql is much nicer. So I have:
1. An html page that holds the applet.
2. a php page that accepts data submitted to it by the applet via the
$_POST array and writes it to the mysql database. This page never
makes it to the browser window.
3. a simple Thank you page that shows up after the applet sends the
data.
Trouble is, nothing makes it into the mysql database. I've tested each
component seperately. I know they work. I know the applet data is
posted cuz I tested it with the usual query.php. But when I put them
together nothing shows up in the database.
What did I do wrong???
Heres the java applet. The page that holds it has nothing but this
applet.

import java.awt.*;
import java.net.*;
import java.io.*;
import java.applet.*;

public class CGISQLPost extends Applet
{
public void init()
{
try
{
URL url = new URL("http://localhost/tmp/CGISQLPost.php");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
PrintWriter out = new
PrintWriter(connection.getOutputStream());
out.print("id="+URLEncoder.encode("4","UTF-8")+"&name="+URLEncoder.encode("Apchar","UTF-8")+"&title="+URLEncoder.encode("Chieftan","UTF-8")+"\n");
out.close();
} catch(IOException e)
{
System.out.println("Oops"+e); return;
}
repaint();
try
{
getAppletContext().showDocument(new
URL("http://localhost/tmp/thanks.html"));
} catch (MalformedURLException e)
{
System.out.println("Oops"+e); return;
}

}

public void paint(Graphics g)
{
g.drawString("Sending",1,14);
}
}

Here is the php page that processes the data.

<html>
<head>
<title>CGISQLPost</title>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
</head>
<body>
<?php
$id = $_POST[id];
$name = $_POST[name];
$title = $_POST[title];
$conn = mysql_connect("localhost","web","webster");
mysql_select_db("test", $conn);
$str = "insert into mytest values ($id,'$name','$title')";
mysql_query($str, $conn) or die(mysql_error());
mysql_close($conn);
?>
</body>
</html>

Jul 17 '05 #2
On 2004-02-27, apchar <ap****@yahoo.com> wrote:

With the code you provided, you will always try to call a php script,
and then set the appletContext to thanks.html. I suggest that you set
the appletContext to the CGISQLPost.php page.

In your CGISQLPost.php you process the data, and then output an
appropriate page...

[snip applet code]

public void init() {
try {
String location = "http://localhost/tmp/CGISQLPost.php");
location.append("?id="+URLEncoder.encode("4","UTF-8")+"&name="+URLEncoder.encode("Apchar","UTF-8")+"&title="+URLEncoder.encode("Chieftan","UTF-8")+"\n");
URL url = new URL(location);
getAppletContext().showDocument(url);
} catch (MalformedURLException e) {
System.out.println("Oops"+e);
}
}

[snip php code]

<?php
$id = $_POST['id'];
$name = $_POST['name'];
$title = $_POST['title'];
$conn = mysql_connect("localhost","web","webster");
mysql_select_db("test", $conn);
$str = "insert into mytest values ($id,'$name','$title')";
mysql_query($str, $conn) or die(mysql_error());
mysql_close($conn);

$fp = fopen('thanks.html');
while ($line = fgets($fp,4096)) == TRUE) {
echo $line;
}
fclose(fp);
?>
--
http://home.mysth.be/~timvw
Jul 17 '05 #3
I just installed the odbc stuff for linux but I have no idea howw to
do a trace. I didn't find much except advertising on the iodbc site.
How do I do a trace? Where does the output go?
I tried it without UTF-8 encoding. Same story. BTW, why do the values
require encoding while the names of the variables passed dont??
I am using PHP v4.3.3 MySQL v4.0.15.
I did indeed test the php file without the applet with a simple html
form. It worked fine. It's only when data is passed via an applet.
Even stranger, I know the values I'm passing are getting to the php
file because I can echo them back to the java applet and display them.
I just cant feed them to mysql.
Thanks.
Art.

jc*****@openlinksw.co.uk (Jan Csisko) wrote in message news:<99**************************@posting.google. com>...
Hi,

this is quite an interesting setup you got there. Personally, I have
never tried this using an Java Applet but did create environments
where JDBC, PHP and
a database were involved.

Have you tried creating an ODBC Trace in order to see how far the
applet gets when accessing the database ?

Also, have you tried this data entry using non-unicode data ?
MySQL only recently started supporting Unicode so I assume you
are using the latest Version and it might be worth running a test
using non-unicode data (and a non-unicode DB).

Also, which Version of PHP are you working with at this stage ?

You said that you tested all components seperately, so I would imagine
that
you can successfully enter data into MySQL using PHP without the
applet as
a front-end. If this is the case the problem must be inbetween the
applet
and PHP itself. Either way, an ODBC Trace and doublechecking the PHP
make
should shed more light on the cause of this problem. Regarding the
PHP-ODBC
interface you might want to have a look at http://www.iodbc.org.

I hope you will find this to be of help.

Kind Regards,

Jan Csisko
Professional Services Consultant
OpenLink Software Web: http://www.openlinksw.com
Product Weblogs: Virtuoso:
http://www.openlinksw.com/weblogs/virtuoso
UDA: http://www.openlinksw.com/weblogs/uda
Universal Data Access & Virtual Database Technology Providers

ap****@yahoo.com (apchar) wrote in message news:<29*************************@posting.google.c om>...
I am trying to use php as a kind of servlet to act as a middle man
between a java applet and mysql. I know java has jdbc but it's flakey
and painful. php access to mysql is much nicer. So I have:
1. An html page that holds the applet.
2. a php page that accepts data submitted to it by the applet via the
$_POST array and writes it to the mysql database. This page never
makes it to the browser window.
3. a simple Thank you page that shows up after the applet sends the
data.
Trouble is, nothing makes it into the mysql database. I've tested each
component seperately. I know they work. I know the applet data is
posted cuz I tested it with the usual query.php. But when I put them
together nothing shows up in the database.
What did I do wrong???
Heres the java applet. The page that holds it has nothing but this
applet.

import java.awt.*;
import java.net.*;
import java.io.*;
import java.applet.*;

public class CGISQLPost extends Applet
{
public void init()
{
try
{
URL url = new URL("http://localhost/tmp/CGISQLPost.php");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
PrintWriter out = new
PrintWriter(connection.getOutputStream());
out.print("id="+URLEncoder.encode("4","UTF-8")+"&name="+URLEncoder.encode("Apchar","UTF-8")+"&title="+URLEncoder.encode("Chieftan","UTF-8")+"\n");
out.close();
} catch(IOException e)
{
System.out.println("Oops"+e); return;
}
repaint();
try
{
getAppletContext().showDocument(new
URL("http://localhost/tmp/thanks.html"));
} catch (MalformedURLException e)
{
System.out.println("Oops"+e); return;
}

}

public void paint(Graphics g)
{
g.drawString("Sending",1,14);
}
}

Here is the php page that processes the data.

<html>
<head>
<title>CGISQLPost</title>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
</head>
<body>
<?php
$id = $_POST[id];
$name = $_POST[name];
$title = $_POST[title];
$conn = mysql_connect("localhost","web","webster");
mysql_select_db("test", $conn);
$str = "insert into mytest values ($id,'$name','$title')";
mysql_query($str, $conn) or die(mysql_error());
mysql_close($conn);
?>
</body>
</html>

Jul 17 '05 #4
I thought data passed in the url was only passed to the $_GET array. I
cant use GET to post the data since some of the values are way too
big. GET is limited to I dont remember how many bytes. Note that what
I've shown here is just a skeleton of the actual code.
Do you think the problem is that I never display the php page?
Thanks,

Tim Van Wassenhove <eu**@pi.be> wrote in message news:<c1*************@ID-188825.news.uni-berlin.de>...
On 2004-02-27, apchar <ap****@yahoo.com> wrote:

With the code you provided, you will always try to call a php script,
and then set the appletContext to thanks.html. I suggest that you set
the appletContext to the CGISQLPost.php page.

In your CGISQLPost.php you process the data, and then output an
appropriate page...

[snip applet code]

public void init() {
try {
String location = "http://localhost/tmp/CGISQLPost.php");
location.append("?id="+URLEncoder.encode("4","UTF-8")+"&name="+URLEncoder.encode("Apchar","UTF-8")+"&title="+URLEncoder.encode("Chieftan","UTF-8")+"\n");
URL url = new URL(location);
getAppletContext().showDocument(url);
} catch (MalformedURLException e) {
System.out.println("Oops"+e);
}
}

[snip php code]

<?php
$id = $_POST['id'];
$name = $_POST['name'];
$title = $_POST['title'];
$conn = mysql_connect("localhost","web","webster");
mysql_select_db("test", $conn);
$str = "insert into mytest values ($id,'$name','$title')";
mysql_query($str, $conn) or die(mysql_error());
mysql_close($conn);

$fp = fopen('thanks.html');
while ($line = fgets($fp,4096)) == TRUE) {
echo $line;
}
fclose(fp);
?>

Jul 17 '05 #5
Just to complete the thread for the archives in case anyone else runs
into this problem, the solution is to not only write the data to the
php page but to read back the response. Apparently PHP wont do
anything at all unless it can spit back something to whatever called
it. I didn't even have to display the response, I just had to read it
in. Below is the updated java and php code. Thanks to all who replied!

-----------------------------------java code ------------------
import java.awt.*;
import java.net.*;
import java.io.*;
import java.applet.*;

public class CGISQLPost extends Applet
{
int numLines=0;
String[] lines = new String[60];

public void init()
{
try
{
URL url = new URL("http://localhost/tmp/CGISQLPost.php");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.print("id=13&name="+URLEncoder.encode("ralph
malph","UTF-8")+"&title="+URLEncoder.encode("goof
ball","UTF-8")+"goofball\n");
out.flush();
out.close();

// read response
BufferedReader in = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
while ((lines[numLines] = in.readLine()) != null)
{
lines[numLines] = lines[numLines].trim();
if ((lines[numLines].length()) > 0) numLines++;
}
in.close();
} catch(IOException e)
{
System.out.println("Oops"+e); return;
}
repaint();
try
{
getAppletContext().showDocument(new
URL("http://localhost/tmp/thanks.html"));
} catch (MalformedURLException e)
{
System.out.println("Oops"+e); return;
}

}
public void paint(Graphics g)
{
g.drawString("Sending",1,14);
}
}

-----------------------------php code -----------------------
<html>
<head>
<title>CGISQLPost</title>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
</head>
<body>
<?php
$id = (integer) $_POST[id];
$name = $_POST[name];
$title = rtrim($_POST[title]);
print "id=$id<br>name=$name<br>title=$title<br>\n";
$conn = mysql_connect("localhost","blah","blahblah");
mysql_select_db("test", $conn);
$str = "insert into mytest values ($id,'$name','$title')";
print $str;
mysql_query($str, $conn) or die(mysql_error());
mysql_close($conn);
?>
</body>
</html>
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: cwho.work | last post by:
Hi! We are using apache ibatis with our MySQL 5.0 database (using innodb tables), in our web application running on Tomcat 5. Recently we started getting a number of errors relating to...
0
oll3i
by: oll3i | last post by:
package library.common; import java.sql.ResultSet; public interface LibraryInterface { public ResultSet getBookByAuthor(String author); public ResultSet getBookByName(String name);
1
by: Arif Mohammed | last post by:
Hi, iam using MySql 4.0.1 and jboss-4.0.4.GA Iam getting the following exception when there are more concurrent requests more than 50 in a second Caused by: org.xyz.MyClass: SQL...
1
by: nickyeng | last post by:
imageValue=null SEVERE:--> com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Column 'Gout_msgtext' cannot be null at...
5
by: Ananthu | last post by:
Hi I have done all the codings part for connecting mysql server with java application but when i try to compile,the compilation is successful and during execution i get the following message, ...
8
by: Ananthu | last post by:
Hi I have done all the codings part for connecting mysql with java in eclipse environment. Coding Part: import java.sql.Connection; import java.sql.DriverManager; public class...
66
by: flarosa | last post by:
Hi, I'm wondering if I can get a reasonably civil (without starting any huge wars) opinion on how server-side PHP compares to server-side Java. I've been strictly a Java developer for almost...
1
by: swethak | last post by:
hi, when i run a java program for to store data and retrive using mysql datatabse i got the following errors.I think in that one of error is due to set the class path.I placed my...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.