Trying to marry php, java, and mysql

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • apchar

    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.openConnect ion();
    connection.setD oOutput(true);
    PrintWriter out = new
    PrintWriter(con nection.getOutp utStream());
    out.print("id=" +URLEncoder.enc ode("4","UTF-8")+"&name="+UR LEncoder.encode ("Apchar","U TF-8")+"&title="+U RLEncoder.encod e("Chieftan","U TF-8")+"\n");
    out.close();
    } catch(IOExcepti on e)
    {
    System.out.prin tln("Oops"+e); return;
    }
    repaint();
    try
    {
    getAppletContex t().showDocumen t(new
    URL("http://localhost/tmp/thanks.html"));
    } catch (MalformedURLEx ception e)
    {
    System.out.prin tln("Oops"+e); return;
    }

    }

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

    Here is the php page that processes the data.

    <html>
    <head>
    <title>CGISQLPo st</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($st r, $conn) or die(mysql_error ());
    mysql_close($co nn);
    ?>
    </body>
    </html>
  • Jan Csisko

    #2
    Re: Trying to marry php, java, and mysql

    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:

    UDA: http://www.openlinksw.com/weblogs/uda
    Universal Data Access & Virtual Database Technology Providers



    apchar@yahoo.co m (apchar) wrote in message news:<295c858.0 402261912.75844 959@posting.goo gle.com>...[color=blue]
    > 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.openConnect ion();
    > connection.setD oOutput(true);
    > PrintWriter out = new
    > PrintWriter(con nection.getOutp utStream());
    > out.print("id=" +URLEncoder.enc ode("4","UTF-8")+"&name="+UR LEncoder.encode ("Apchar","U TF-8")+"&title="+U RLEncoder.encod e("Chieftan","U TF-8")+"\n");
    > out.close();
    > } catch(IOExcepti on e)
    > {
    > System.out.prin tln("Oops"+e); return;
    > }
    > repaint();
    > try
    > {
    > getAppletContex t().showDocumen t(new
    > URL("http://localhost/tmp/thanks.html"));
    > } catch (MalformedURLEx ception e)
    > {
    > System.out.prin tln("Oops"+e); return;
    > }
    >
    > }
    >
    > public void paint(Graphics g)
    > {
    > g.drawString("S ending",1,14);
    > }
    > }
    >
    > Here is the php page that processes the data.
    >
    > <html>
    > <head>
    > <title>CGISQLPo st</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($st r, $conn) or die(mysql_error ());
    > mysql_close($co nn);
    > ?>
    > </body>
    > </html>[/color]

    Comment

    • Tim Van Wassenhove

      #3
      Re: Trying to marry php, java, and mysql

      On 2004-02-27, apchar <apchar@yahoo.c om> 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="+URLEnco der.encode("4", "UTF-8")+"&name="+UR LEncoder.encode ("Apchar","U TF-8")+"&title="+U RLEncoder.encod e("Chieftan","U TF-8")+"\n");
      URL url = new URL(location);
      getAppletContex t().showDocumen t(url);
      } catch (MalformedURLEx ception e) {
      System.out.prin tln("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($st r, $conn) or die(mysql_error ());
      mysql_close($co nn);

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


      --

      Comment

      • apchar

        #4
        Re: Trying to marry php, java, and mysql

        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.

        jcsisko@openlin ksw.co.uk (Jan Csisko) wrote in message news:<998aac6a. 0402270340.6cec ba2b@posting.go ogle.com>...[color=blue]
        > 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
        >
        >
        >
        > apchar@yahoo.co m (apchar) wrote in message news:<295c858.0 402261912.75844 959@posting.goo gle.com>...[color=green]
        > > 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.openConnect ion();
        > > connection.setD oOutput(true);
        > > PrintWriter out = new
        > > PrintWriter(con nection.getOutp utStream());
        > > out.print("id=" +URLEncoder.enc ode("4","UTF-8")+"&name="+UR LEncoder.encode ("Apchar","U TF-8")+"&title="+U RLEncoder.encod e("Chieftan","U TF-8")+"\n");
        > > out.close();
        > > } catch(IOExcepti on e)
        > > {
        > > System.out.prin tln("Oops"+e); return;
        > > }
        > > repaint();
        > > try
        > > {
        > > getAppletContex t().showDocumen t(new
        > > URL("http://localhost/tmp/thanks.html"));
        > > } catch (MalformedURLEx ception e)
        > > {
        > > System.out.prin tln("Oops"+e); return;
        > > }
        > >
        > > }
        > >
        > > public void paint(Graphics g)
        > > {
        > > g.drawString("S ending",1,14);
        > > }
        > > }
        > >
        > > Here is the php page that processes the data.
        > >
        > > <html>
        > > <head>
        > > <title>CGISQLPo st</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($st r, $conn) or die(mysql_error ());
        > > mysql_close($co nn);
        > > ?>
        > > </body>
        > > </html>[/color][/color]

        Comment

        • apchar

          #5
          Re: Trying to marry php, java, and mysql

          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 <euki@pi.be> wrote in message news:<c1ncqo$1k 4df0$1@ID-188825.news.uni-berlin.de>...[color=blue]
          > On 2004-02-27, apchar <apchar@yahoo.c om> 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="+URLEnco der.encode("4", "UTF-8")+"&name="+UR LEncoder.encode ("Apchar","U TF-8")+"&title="+U RLEncoder.encod e("Chieftan","U TF-8")+"\n");
          > URL url = new URL(location);
          > getAppletContex t().showDocumen t(url);
          > } catch (MalformedURLEx ception e) {
          > System.out.prin tln("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($st r, $conn) or die(mysql_error ());
          > mysql_close($co nn);
          >
          > $fp = fopen('thanks.h tml');
          > while ($line = fgets($fp,4096) ) == TRUE) {
          > echo $line;
          > }
          > fclose(fp);
          > ?>[/color]

          Comment

          • apchar

            #6
            Re: Trying to marry php, java, and mysql

            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.openConnect ion();
            connection.setD oOutput(true);
            PrintWriter out = new PrintWriter(con nection.getOutp utStream());
            out.print("id=1 3&name="+URLEnc oder.encode("ra lph
            malph","UTF-8")+"&title="+U RLEncoder.encod e("goof
            ball","UTF-8")+"goofball\n ");
            out.flush();
            out.close();

            // read response
            BufferedReader in = new BufferedReader( new
            InputStreamRead er(connection.g etInputStream() ));
            while ((lines[numLines] = in.readLine()) != null)
            {
            lines[numLines] = lines[numLines].trim();
            if ((lines[numLines].length()) > 0) numLines++;
            }
            in.close();
            } catch(IOExcepti on e)
            {
            System.out.prin tln("Oops"+e); return;
            }
            repaint();
            try
            {
            getAppletContex t().showDocumen t(new
            URL("http://localhost/tmp/thanks.html"));
            } catch (MalformedURLEx ception e)
            {
            System.out.prin tln("Oops"+e); return;
            }

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

            -----------------------------php code -----------------------
            <html>
            <head>
            <title>CGISQLPo st</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","bla h","blahblah ");
            mysql_select_db ("test", $conn);
            $str = "insert into mytest values ($id,'$name','$ title')";
            print $str;
            mysql_query($st r, $conn) or die(mysql_error ());
            mysql_close($co nn);
            ?>
            </body>
            </html>

            Comment

            Working...