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:
Code java:
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:
Quote:
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.
Re: Connecting to Server Database From Applet
Quote:
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
Re: Connecting to Server Database From Applet
Quote:
Does it only allow access to from localhost?
I attempted to use this:
Code java:
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?
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.
Re: Connecting to Server Database From Applet
Quote:
Originally Posted by
aussiemcgr
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
Re: Connecting to Server Database From Applet
Quote:
Originally Posted by
cafeteria84
Making a database connection to a remote server from an applet is a very bad practice. Should be avoided.
Care to elaborate?
Re: Connecting to Server Database From Applet
Quote:
Originally Posted by
copeg
Care to elaborate?
Performance, Security and Maintainability.
Re: Connecting to Server Database From Applet
Quote:
Originally Posted by
cafeteria84
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.