Results 1 to 4 of 4

Thread: Database

  1. #1
    MarioBrawler is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Database

    I'm sorry if this is in the wrong section but I'm new with getting a java applet to connect to a mysql database. This is my first time attempting to do so.

    I've researched a lot and have seen lots of tutorials, but I'm still facing problems.
    Since my java applet goes to a browser, I did sign it.
    I've also been using jdbc and the jar file has been imported properly to my library.

    I've also been using 000webhost.com and have been trying to connect to the database from both the IDE and the browser.
    I also got two of my friends to help me. One of them had to go early and the other don't know where I went wrong.

    Here is my codes:
    Java Code:
    import java.sql.*;
    
    public class DBconnect {
    	private Connection con;
    	private Statement st;
    	ResultSet rs;
    
    	public DBconnect() {
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    		
    			con = DriverManager.getConnection("jdbc:mysql://mysql2.000webhost.com:3306/" +
    				"MySQL_Database","MySQL_Username","MySQL_Pass");
    			
    		}catch (Exception ex){
    			ex.printStackTrace();
            }
    	}
    }
    I've come to the conclusion that the DriverManager.getConnection(...) is whats giving me problems.


    The error reads...
    The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    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:1117)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:350)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(Connecti onImpl.java:2408)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(Co nnectionImpl.java:2445)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(Connecti onImpl.java:2230)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImp l.java:813)
    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:399)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonReg isteringDriver.java:334)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at DBconnect.<init>(DBconnect.java:14)
    at Lobby.init(Lobby.java:66)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Un known Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress( Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(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:259)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
    ... 18 more
    I have also tried looking up the cause but still no luck.

    Is there anything I can do to fix this problem? I'm curious of what this cause mean and why I'm having trouble.
    Thanks in advanced

    Edit: Sorry, I forgot to make a better title

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Database

    I can only quote from another forum:
    "If you get a SQLException: Connection refused or Connection timed out or a MySQL specific CommunicationsException: Communications link failure, then it means that the DB isn't reachable at all. This can have one or more of the following causes:

    IP address or hostname in JDBC URL is wrong.
    Hostname in JDBC URL is not recognized by local DNS server.
    Port number is missing or wrong in JDBC URL.
    DB server is down.
    DB server doesn't accept TCP/IP connections.
    DB server has run out of connections.
    Something in between Java and DB is blocking connections, e.g. a firewall or proxy.

    To solve the one or the other, follow the following advices:

    Verify and test them with ping.
    Refresh DNS or use IP address in JDBC URL instead.
    Verify it based on my.cnf of MySQL DB.
    Start the DB.
    Verify if mysqld is started without the --skip-networking option.
    Restart the DB and fix your code accordingly that it closes connections in finally.
    Disable firewall and/or configure firewall/proxy to allow/forward the port."
    I think some of the causes may be ruled out and what is left should point you to a direction. I would guess maybe firewall or similar outside issues maybe?
    Last edited by Sierra; 01-06-2013 at 10:48 AM.
    I like likes!

  3. #3
    MarioBrawler is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: Database

    I've just learned something new about the database. Could this be the cause?
    Database-74a87f5fd4114e049bad988as.png

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Database

    I guess it could, you need to make sure you can establish connections. You could try a free client software to test that maybe.
    I like likes!

Similar Threads

  1. Create registration form | insert into database | call the database
    By FamilyGuy in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-23-2012, 11:08 AM
  2. Embedded Database : SQLite or Derby Database
    By vishnubrett in forum NetBeans
    Replies: 3
    Last Post: 03-26-2012, 10:50 AM
  3. Client-Database vs Client-Server-Database Setup
    By xxSLRxx in forum New To Java
    Replies: 5
    Last Post: 01-06-2012, 09:53 AM
  4. Relational Database or Object Database?
    By mattlindsay in forum New To Java
    Replies: 8
    Last Post: 09-24-2011, 06:44 PM
  5. Replies: 2
    Last Post: 02-11-2009, 04:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •