Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: mysql in java applets

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default mysql in java applets

    hello I got this code working fine

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
     
    public class connect
    {
    	public static void main(String[] args)
    	{
    		Connection conn=null;
    		System.out.println("Start.");
    		try
    		{
    			Class.forName("com.mysql.jdbc.Driver");
    			System.out.println("So far so good.");
    			conn=DriverManager.getConnection("jdbc:mysql://a.b.c.d/db","user","password");
    			System.out.println("connected.");
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    			System.out.println("Bad.");
    		}
    		finally
    		{
    			if(conn!=null)
    			{
    				try
    				{
    					conn.close();
    					System.out.println("close database connection.");
    				}
    				catch(Exception e){/*ignore this exception*/}
    			}
    		}
    	}
    }

    but when I put it in an applet like this it stops working for me.

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    //import java.sql.*;
     
    //import sun.jdbc.odbc.JdbcOdbcDriver
    import com.mysql.jdbc.Driver;
     
    import java.applet.Applet;
    import java.awt.Label;
    import java.awt.Button;
    import java.awt.TextField;
    import java.awt.TextArea;
     
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    import javax.swing.JOptionPane;
     
    public class guestbook extends Applet
    {
            Connection conn=null;
     
    	public void init()
    	{
    		//establich connection
    		try
    		{
    			Class.forName("com.mysql.jdbc.Driver");
    			JOptionPane.showMessageDialog(null,"so far so good");
    			conn=DriverManager.getConnection("jdbc:mysql://a.b.c.d/db","user","password");
    			JOptionPane.showMessageDialog(null,"connected");
    		}
    		catch(Exception e)
    		{
    			JOptionPane.showMessageDialog(null,"error");
    		}
     
    	public void destroy()
    	{
    		// close database connection
    		if(conn!=null)
    		{
    			try
    			{
    				conn.close();
    			}
    			catch(Exception e){/*ignore this exception*/}
    		}
    	}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: mysql in java applets

    but when I put it in an applet like this it stops working for me.
    Define 'it stops working'. Does it compile? Are there Exceptions?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: mysql in java applets

    Look in the browser's java console for error messages. Applets often need permission to do most things.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: mysql in java applets

    As Norm said, an applet, by default, would not be able to do this - it doesn't have permission to open a socket to anywhere except for the server where it was served from.

    I realize that you're likely learning so let me push you in the right direction - this is a terrible design. A real system would never, ever, ever allow direct database connections from the outside world. Additionally, your applet would have to contain authorization parameters giving everyone who downloaded the applet the ability to rumage through your database at will.

    Again, for a learning exercise this is ok though totally pointless. But if you are ever asked to do the same thing in a professional software environment run, do not walk, to the nearest exit.
    Need Java help? Check out the HotJoe Java Help forums!

  5. The Following User Says Thank You to stdunbar For This Useful Post:

    copeg (June 8th, 2012)

  6. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: mysql in java applets

    Quote Originally Posted by copeg View Post
    Define 'it stops working'. Does it compile? Are there Exceptions?
    In the console program I can connect to the database but not in the applet. I test it on the same computer I program on so I have the CLASSPATH set up, I dont even run a web server (yet), a.b.c.d is my IP-adress.

  7. #6
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: mysql in java applets

    As Norm said, an applet, by default, would not be able to do this - it doesn't have permission to open a socket to anywhere except for the server where it was served from.
    So if the web and mySQL-server is the same computer it should work then?

    I realize that you're likely learning so let me push you in the right direction - this is a terrible design. A real system would never, ever, ever allow direct database connections from the outside world. Additionally, your applet would have to contain authorization parameters giving everyone who downloaded the applet the ability to rumage through your database at will.

    Again, for a learning exercise this is ok though totally pointless. But if you are ever asked to do the same thing in a professional software environment run, do not walk, to the nearest exit.
    I understand the potential hazzard of letting people access a database from the internet but what if the user "user" have read only priviliges on the databade "db" then what is the possible harm if the database is meant to be for public display?

  8. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: mysql in java applets

    Quote Originally Posted by micro kid View Post
    In the console program I can connect to the database but not in the applet. I test it on the same computer I program on so I have the CLASSPATH set up, I dont even run a web server (yet), a.b.c.d is my IP-adress.
    I forgot to say that I think have nave narrowed it down to the driver not working when I run the applet which is a mystery to me because the driver works fine for the console program.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: mysql in java applets

    Have you looked in the browser's java console for error messages?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How do embedded java applets send and recv data?
    By MRnobody in forum Java Networking
    Replies: 1
    Last Post: March 1st, 2012, 05:36 AM
  2. How do I debug my Java applets to see what's going wrong?
    By diyaots in forum AWT / Java Swing
    Replies: 2
    Last Post: November 10th, 2011, 09:37 AM
  3. CyberPet Project - Using Java and GUI (NO APPLETS)
    By Princess90 in forum Member Introductions
    Replies: 1
    Last Post: May 16th, 2011, 09:38 AM
  4. Java.jar and Mysql.jar
    By kurt-hardy in forum Java Theory & Questions
    Replies: 1
    Last Post: January 27th, 2011, 09:48 AM
  5. Learn applets in java
    By mohsendeveloper in forum Java Applets
    Replies: 2
    Last Post: June 25th, 2009, 02:50 PM