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: Connecting to Server Database From Applet

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Connecting to Server Database From Applet

    Hi everyone, it's been awhile since I was last around here.

    So I am attempting to get an applet to make a connection with a database on the same server as the applet, with little success thus far. I'm looking for some guidance. I am using mysql-connector-java-5.1.16-bin.

    The closest I've gotten to success was with the following code:
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    //Database, Username, and Password have been removed in the below line for security reasons
    connect = DriverManager.getConnection("jdbc:mysql://airlinegame.orgfree.com/[database]","[username]","[password]");

    This code returned the following error:
    com.mysql.jdbc.exceptions.jdbc4.CommunicationsExce ption: Communications link failure

    The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:41 1)
    at com.mysql.jdbc.SQLError.createCommunicationsExcept ion(SQLError.java:1116)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(Connecti onImpl.java:2333)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(Co nnectionImpl.java:2370)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(Connecti onImpl.java:2154)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImp l.java:792)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connect ion.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:41 1)
    at com.mysql.jdbc.ConnectionImpl.getInstance(Connecti onImpl.java:381)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonReg isteringDriver.java:305)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    ...
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at com.mysql.jdbc.StandardSocketFactory.connect(Stand ardSocketFactory.java:257)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:294)
    ... 17 more
    Any suggestions? I know I'm doing something wrong, just not sure what.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


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

    Default Re: Connecting to Server Database From Applet

    Caused by: java.net.ConnectException: Connection refused: connect
    Are you sure you can remote access the database on that server, for instance is the database behind a firewall? Does it only allow access to from localhost? An easy way to test is to try a remote connection using a MySQL command line utility

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Connecting to Server Database From Applet

    Does it only allow access to from localhost?
    I attempted to use this:
    connect = DriverManager.getConnection("jdbc:mysql://localhost/[database]","[username]","[password]");

    And received the same error. According to my host, "Many people want to only use database, but their site is hosted elsewhere. We provide free database for websites hosted with us. For that reason, external access is blocked without exception."
    So I guess my question comes back to the actual dynamics of how an applet works. Would an embedded applet be considered "external" access?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

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

    Default Re: Connecting to Server Database From Applet

    Applets run client side, specifying localhost means you are trying to access a database on the machine the applet is running. If you try to connect to your host database using the host IP or URL address, this would be classified as external access - it will fail based upon the description of access you posted.

    You can solve this in many ways. 1) get a host that allows external access 2) create web scripts on the server which perform the database IO and spits out data to a webpage that can be parsed by your application.

  5. #5
    Member
    Join Date
    Nov 2011
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Connecting to Server Database From Applet

    Quote Originally Posted by aussiemcgr View Post
    Hi everyone, it's been awhile since I was last around here.

    So I am attempting to get an applet to make a connection with a database on the same server as the applet, with little success thus far. I'm looking for some guidance. I am using mysql-connector-java-5.1.16-bin.
    Making a database connection to a remote server from an applet is a very bad practice. Should be avoided.

    Spring 3
    Last edited by cafeteria84; January 22nd, 2012 at 04:04 PM.

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

    Default Re: Connecting to Server Database From Applet

    Quote Originally Posted by cafeteria84 View Post
    Making a database connection to a remote server from an applet is a very bad practice. Should be avoided.
    Care to elaborate?

  7. #7
    Member
    Join Date
    Nov 2011
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Connecting to Server Database From Applet

    Quote Originally Posted by copeg View Post
    Care to elaborate?
    Performance, Security and Maintainability.

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

    Default Re: Connecting to Server Database From Applet

    Quote Originally Posted by cafeteria84 View Post
    Performance, Security and Maintainability.
    Thanks...I was hoping for a bit more than that. I acknowledge and understand the reasons why, but would encourage you to provide a bit more detail - perhaps even real world examples. I oftentimes see quick responses online when doing research myself, and am always left wanting to see a bit more reasoning.

Similar Threads

  1. Replies: 1
    Last Post: December 15th, 2011, 08:29 AM
  2. Error connecting to database
    By bczm8703 in forum JDBC & Databases
    Replies: 9
    Last Post: December 8th, 2011, 01:06 AM
  3. Problem With Applet Connecting With Microsoft Access Database
    By Saurabh Mittal in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 29th, 2011, 10:43 AM
  4. Looking at Exploring Database Connecting
    By aussiemcgr in forum JDBC & Databases
    Replies: 2
    Last Post: August 12th, 2010, 03:40 PM
  5. Connecting to a database
    By fwashington in forum JDBC & Databases
    Replies: 5
    Last Post: March 15th, 2010, 01:37 PM