Using Java to connect to local host

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gerrybytes
    New Member
    • Apr 2008
    • 11

    Using Java to connect to local host

    Hi all,

    I created a Java application which allows me to send information to my mysql database using the jdbc connector via a JFrame. However when i modified the file to be a JApplet the info will not sent and i cannot connect to the database? Any particular reason for this?
    I need it to be a JApplet so i can add it to a browser window.

    [code=java]
    private void save ()
    {
    Connection conn = null;

    try
    {
    Statement stmt;
    String userName = "root";
    String password = "PASSWORD";
    String url = "jdbc:mysql ://localhost:3306/DATABASE";
    Class.forName ("com.mysql.jdb c.Driver").newI nstance ();
    conn = DriverManager.g etConnection (url, userName, password);
    System.out.prin tln ("Database connection established");

    //Get a statement object
    stmt = conn.createStat ement();

    stmt.executeUpd ate( "INSERT INTO TABLE(Info i know to put in) VALUES('Values i know to put in')");
    }
    catch (Exception e)
    {
    System.err.prin tln ("Cannot connect to database server");
    }
    finally
    {if (conn != null)
    {try
    {conn.close ();
    System.out.prin tln ("Database connection terminated");
    }
    catch (Exception e) { /* ignore close errors*/ }
    }
    }
    }
    [/code]

    As i said above it works as a JFrame but not as a JApplet. Any help would be great.

    Thanks G.
  • nomad
    Recognized Expert Contributor
    • Mar 2007
    • 664

    #2
    Originally posted by gerrybytes
    Hi all,

    I created a Java application which allows me to send information to my mysql database using the jdbc connector via a JFrame. However when i modified the file to be a JApplet the info will not sent and i cannot connect to the database? Any particular reason for this?
    I need it to be a JApplet so i can add it to a browser window.

    [code=java]
    private void save ()
    {
    Connection conn = null;

    try
    {
    Statement stmt;
    String userName = "root";
    String password = "PASSWORD";
    String url = "jdbc:mysql ://localhost:3306/DATABASE";
    Class.forName ("com.mysql.jdb c.Driver").newI nstance ();
    conn = DriverManager.g etConnection (url, userName, password);
    System.out.prin tln ("Database connection established");

    //Get a statement object
    stmt = conn.createStat ement();

    stmt.executeUpd ate( "INSERT INTO TABLE(Info i know to put in) VALUES('Values i know to put in')");
    }
    catch (Exception e)
    {
    System.err.prin tln ("Cannot connect to database server");
    }
    finally
    {if (conn != null)
    {try
    {conn.close ();
    System.out.prin tln ("Database connection terminated");
    }
    catch (Exception e) { /* ignore close errors*/ }
    }
    }
    }
    [/code]

    As i said above it works as a JFrame but not as a JApplet. Any help would be great.

    Thanks G.
    do a google search on this one JApplet and Database. You will find a couple solutions that will help you. One is called Signed

    nomad

    Comment

    • gerrybytes
      New Member
      • Apr 2008
      • 11

      #3
      Hi Nomad,

      I know about signing applets. I've done this ok and i get no error messages returned.

      Could it be, because i'm connecting directly to the local host or should i use the ip address?

      G

      Comment

      • ajos
        Contributor
        • Aug 2007
        • 283

        #4
        Originally posted by gerrybytes
        Hi Nomad,

        I know about signing applets. I've done this ok and i get no error messages returned.

        Could it be, because i'm connecting directly to the local host or should i use the ip address?

        G
        How about printing the stacktrace instead of leaving it empty.ip is not required here. check the mysql docs to connect to a java application/applet.

        Comment

        Working...