#1
  1. No Profile Picture
    aHVoPw==
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2001
    Posts
    1,058
    Rep Power
    15

    Using applets and mysql (w/php)


    I want to be able to create an applet and be able to use it to access data from mysql.

    I tried doing searches in this forum and the best thing I found was from this thread:
    Birthday Applet
    down at the post with the php coding by alexgreg

    He says to "point" your Java applet to the php file. What does that mean? (how do you do that with code?) I do know some of Java, but not that great with file manipulation.
    Also, what does that code do? I see that the php file prints data gathered from mysql. How would the applet accept this?

    Thanks for any help.
    Visit my new IIS Tutorial Series!
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Well, applets run client side, so you cannot access mysql directly. What you can do is have your applet contact something that can, namely a php script.

    What you have to do in your applet is use a URL object to open a connection to the php script. In effect, it is acting as its own little browser by opening another connection. This way you can send any info you want to the php script and have it access mysql, then return whatever info to the applet for display.

    Hope that helps.
  4. #3
  5. dont click here
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    409
    Rep Power
    13
    Connect to a MYSQL DB using JDBC, like any other Application/Servlet anything.

    here is a simple Java Class, that shows how to conect to a MySQL DB. If you want to dl a file to test it. http://www.risrani.com/testr/mysqlJDBC.txt


    or here is the code. You just have to change few things pretty self explanatory

    Code:
    import java.sql.*;
    
    //Goto mysql.com and download the mm.mySQL Drivers.
    //Unzip and copy mysql-connector-j-2.0.14-bin.jar to you CLASSPATH
    //or into the same directory where all you .class files will lie.
    //Here is a same code you should be able to figure it out.
    
    
    public class Connector {
      public static void main(String args[]){
        try {
          Class.forName("com.mysql.jdbc.Driver");
          Connection con =
              DriverManager.getConnection(
              "jdbc:mysql://localhost/dbname", "user", "password");
          String query  = "Select * from TABLE";
          Statement s = con.createStatement();
          ResultSet rs = s.executeQuery(query);
          while(rs.next()){
            System.out.println(rs.getString(1));
          }
          con.close();
        }
        catch (Exception ex) {
          System.out.println(ex.getMessage());
        }
      }
    }
    perhaps u can extend this class to have a method to query or update the DB and such and simply call this class everything.
    Last edited by Shocka; December 22nd, 2002 at 09:43 PM.
  6. #4
  7. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Unzip and copy mysql-connector-j-2.0.14-bin.jar to you CLASSPATH
    Correct me if I'm wrong, but this has to be the *clients* classpath, right?
    -james
  8. #5
  9. dont click here
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    409
    Rep Power
    13
    good question.. i havnt tried this yet.. but i assume u can put it in the same path as the class file on the server. but i dont stand by that answer..


    does anyone have an MySQL server i can use to test this out.. i actually want to find out the answer.
  10. #6
  11. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    You are very correct that the Applet should be able to connect to mysql directly, as long as the mysql server and the web server (where the applet came from) are the same box (applets cannot connect to anything other than the server they originated from). However, the mysql driver jar would have to be downloaded to the client with the applet jar. You can do this by putting both jar files in the "archive" attribute of the applet/object tag.

    Example:
    Code:
    <applet width="100" height="100" 
      code="mypackage.myclass"
      codebase="."
      archive="myApplet.jar, mysqlDriver.jar">
    </applet>
    Then your code *should* work. Unfortunately, the mysql driver is 435k, which is a rather large download for a modem user. Also, mysql has to be set up to allow non-localhost connections. If you do not have admin rights to mysql this may be a problem.
  12. #7
  13. dont click here
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    409
    Rep Power
    13
    well i know for a fact you do not have to specify the driver in the archive list,

    there is an example of an applet that connects to a Sybase DB on my schools site. But i am unable to figure out where they have put the Driver archive. And the HTML pointing to the point is pointing to 1 class no archives at all.


    As for the mysql settting like nemi said if you have no admin power and u are set to localhost this wont work at all.
  14. #8
  15. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Shocka, could the applet be using ODBC? I think the jdbc:odbc bridge comes with the JVM.
    -james
  16. #9
  17. dont click here
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    409
    Rep Power
    13
    Originally posted by bricker42
    Shocka, could the applet be using ODBC? I think the jdbcdbc bridge comes with the JVM.
    it might be... but at school they stress the use of the Sybase drivers to connect to the DB.. but they dont always practice what they preach

IMN logo majestic logo threadwatch logo seochat tools logo