473,404 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 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 3356
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.