|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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: http://forums.devshed.com/showthread.php?threadid=44040 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. ![]()
__________________
K1 |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
|||
|
|||
|
Quote:
Correct me if I'm wrong, but this has to be the *clients* classpath, right?
__________________
-james |
|
#5
|
||||
|
||||
|
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. |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
||||
|
||||
|
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. |
|
#8
|
|||
|
|||
|
Shocka, could the applet be using ODBC? I think the jdbc:odbc bridge comes with the JVM.
|
|
#9
|
||||
|
||||
|
Quote:
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Using applets and mysql (w/php) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|