Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / April 2004

Tip: Looking for answers? Try searching our database.

Establishment of connection rejected (MySQL & Applet)

Thread view: 
circuit_breaker - 24 Apr 2004 22:25 GMT
Hi,

The following code works as an application but not as an applet on the
localhost.
The error is: "java.sql.SQLException: Data source rejected
establishment of connection, message from server......Host
192.168.0.72 is not allowed to connect to this MySQL server".

I'm trying to run it from the server (localhost) and from another
machine on the network but the error is the same (the IP address
change of course).  Is the problem a Java security issue or is it on
the MySQL server?

Here's the code:

Application version (works)
---------------------------
public class connectmysql
{
    public static void main( String[] args )
    {
        ResultSet rs;
        Connection conn = null;
        String url = "jdbc:mysql://localhost/Inventory";
        String userName = "iuser";
        String password = "mypass";

        try
        {
            Class.forName ("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection (url, userName, password );
            System.out.println ("Connected" );

            Statement s = conn.createStatement();
            String sql = "SELECT * FROM categories";

            rs = s.executeQuery( sql );
            while (rs.next() )
            {
                System.out.println( rs.getString( "category_name" ) );
            }
...
------------------------------

Applet version:
--------------

public class sqlapplet extends JApplet
{
    public JFrame frame;
    public void init()
    {
        frame = new myFrame();
        frame.setSize( 200, 200 );
        frame.setTitle( "Applet Test on Linux with MySql");
        frame.show();
//        new DatabaseConnection();
    }
}

class DatabaseConnection
{
    static String driver = "com.mysql.jdbc.Driver";
    DatabaseConnection()
    {
        try
        {
            Class.forName( driver );
        }
        catch (Exception e)
        {
            return;
        }
    }
}

class myFrame extends JFrame
{
    private JPanel myPanel;
    private JTextField jtf;

    static String driver = "com.mysql.jdbc.Driver";
    static String url = "jdbc:mysql://localhost/Inventory";
    //Connection conn = null;

    static String user = "iuser";
    static String password = "ipass";

    private ResultSet rs;

    myFrame()
    {
        myPanel = new JPanel();

        Container contentPane = getContentPane();
//        contentPane.setLayout( new( BorderLayout() ) );

        myPanel.add( jtf );
        contentPane.add( myPanel );

        try
        {
            Class.forName( driver ).newInstance();
            jtf.setText("Driver loaded" );
            try{

            Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost/Inventory?user=iuser&password=mypass");
            }

            catch( SQLException e)
            {
                jtf.setText( e.toString() );
                return;
            }
            jtf.setText("Connected" );
Bjorn Abelli - 25 Apr 2004 04:08 GMT
"circuit_breaker" wrote...

> The following code works as an application but
> not as an applet on the localhost.
[quoted text clipped - 9 lines]
> Is the problem a Java security issue or is it on
> the MySQL server?

I think it's a bit of both.

The Java security issue strikes as soon as you try to execute an applet
through a browser. In AppletViewer there shouldn't be any problem in that
respect.

With just ordinary JDBC-drivers in most cases you cannot connect to a
database that resides on another server than the webserver.

You've got another Java issue in the following line:

> String url = "jdbc:mysql://localhost/Inventory";

Remember that the applet is downloaded to the browser, so with that url,
it's trying to connect to "localhost" from the *browsers* point of view.

It seems like the IP-address for "localhost" is altered when you run as an
applet (probably depending on the browser) as localhost usually maps to
127.0.0.1.

As regarding to IP-addresses, there's also the question of MySQL.

AFAIK the authorization/authentication in MySQL is default set to access
*only* from the own computer (127.0.0.1), so you'll probably have to change
that as well, to accept connections from other addresses.

.02

// Bjorn A
circuit_breaker - 25 Apr 2004 12:25 GMT
From the webserver the applet works fine using AppletViewer but not in
Mozilla.  I've installed the latest plug-in & JVM as well...  Should I
sign the applet to be able to run it on the web server itself?

Thanks

> "circuit_breaker" wrote...
> I think it's a bit of both.
[quoted text clipped - 26 lines]
>
> // Bjorn A
Bjorn Abelli - 25 Apr 2004 13:13 GMT
"circuit_breaker" wrote...

> From the webserver the applet works fine using
> AppletViewer but not in Mozilla.  I've installed the
> latest plug-in & JVM as well...  Should I sign the
> applet to be able to run it on the web server itself?

If the database resides on the webserver, that wouldn't be necessary, but if
they are on different machines, yes.

> > > String url = "jdbc:mysql://localhost/Inventory";

1. Remember to change the url to another
  address than localhost.

> > AFAIK the authorization/authentication in MySQL is
> > default set to access *only* from the own computer
> > (127.0.0.1)...

2. Remember to set the privileges in MySQL
  so the database is accessible from "outside".

3. Consider another approach completely. There can
  be other security issues of using an applet to
  access the database, as the communication to get
  the connection can be "sniffed", and then you're
  wide open...

A common approach is to delegate the actual database connection to a
servlet, and to let the applet communicate with the servlet instead.

Or to implement the whole shabang in servlets instead...

// Bjorn A


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.