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 7 of 7

Thread: Connect to MySQL database on remote IP/port through JDBC

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Connect to MySQL database on remote IP/port through JDBC

    Hi guys,

    I've seen a lot about a similar issue posted and solved but I have a small variation on the common question. The remote server I need to connect to an IP/port combination, i.e: xx.xx.xx.xx : xxxx. The IP is for the company and the port (e.g: 9090) redirects to a specific PC on the work network. The usual way of connecting to a database locally is by using this "jdbc:mysql://localhost:3306/" as the url string. But how do I handle there being a port attached to the remote IP?

    Thanks


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Connect to MySQL database on remote IP/port through JDBC

    Can you be more specific? Your database is on server? Or another maching connected with the server?
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connect to MySQL database on remote IP/port through JDBC

    Thanks for the reply.

    The database is on my server (which can be accessed through the IP/port that I need to include in the database url string). The java that needs to connect to it will be deployed on another machine's server.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Connect to MySQL database on remote IP/port through JDBC

    Then why don't you directly access the server on which your database is?
    Well this would be something like
    HOST_NAME:PORT/YourDatabase
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connect to MySQL database on remote IP/port through JDBC

    I tried that but I think there has to be some more config changes or something like that because of the default use of port 3306 with mysql. If the only thing I do is change "jdbc:mysql://localhost:3306/" to "jdbc:mysql://myhost:myport/" then it simply does not connect.

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Connect to MySQL database on remote IP/port through JDBC

    Now you are coming to the actual thing. So, can you post the stacktrace here along with the part of your code, you are trying to connect?
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  7. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connect to MySQL database on remote IP/port through JDBC

    Exception Message:
    "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."

    StackTrace:
    "com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: 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.newInstance0(Native Method)
     	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
     	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
     	at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
     	at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
     	at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
     	at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:696)
     	at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1102)
     	at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336)
     	at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
     	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
     	at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
     	at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
     	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
     	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
     	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
     	at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
     	at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
     	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
     	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
     	at java.sql.DriverManager.getConnection(DriverManager.java:579)
     	at java.sql.DriverManager.getConnection(DriverManager.java:221)
     	at MobileMoneyPack.Account.<init>(Account.java:38)
     	at MobileMoneyPack.LoginServlet.processRequest(LoginServlet.java:42)
     	at MobileMoneyPack.LoginServlet.doPost(LoginServlet.java:118)
     	at javax.servlet.http.HttpServlet.service(HttpServlet.java:688)
     	at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
     	at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
     	at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:809)
     	at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:671)
     	at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:505)
     	at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:476)
     	at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:355)
     	at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:305)
     	at org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:785)
     	at org.apache.jsp.LoginPageBean_jsp._jspService(LoginPageBean_jsp.java:86)
     	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
     	at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
     	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
     	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
     	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
     	at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
     	at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
     	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
     	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
     	at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
     	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
     	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
     	at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
     	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
     	at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
     	at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
     	at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
     	at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
     	at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
     	at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
     	at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
     	at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
     	at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
     	at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
     	at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
     	at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
     	at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
     	at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
     	at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
     	at java.lang.Thread.run(Thread.java:722)
     
     Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
     	at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2552)
     	at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:620)
     	... 59 more
     "

    My connection code:
        Connection conn = null;
        String driver = "com.mysql.jdbc.Driver";
     
        try {
                Class.forName(driver).newInstance();
                conn = DriverManager.getConnection("jdbc:mysql://myIP:myPort/myDB?zeroDateTimeBehavior=convertToNull", "user", "password");
             }

    The "caused by" part makes me note that going to http://myIP:myport/ in a web browser brings up the server main page but pinging myIP alone gives "Request timed out."

    ETA: Another note is that when I call one of the methods in the class via URL using myIP:myPort, the connection is established normally and I can access the database just fine. URL example that works:
    http://myIP:myPort/myProjectName/myLoginHttpServlet?loginID=xx&password=yy
    where myLoginHttpServlet starts a connection to the mySQL database and calls a java method which queries the database.
    Last edited by javauser; September 7th, 2012 at 02:22 AM.

Similar Threads

  1. Java Code to connect remote server
    By idealguy in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 3rd, 2012, 04:59 AM
  2. connect to mysql database
    By aueddonline in forum JDBC & Databases
    Replies: 5
    Last Post: December 17th, 2011, 02:44 PM
  3. JDBC Problem - com.mysql.jdbc.Driver
    By icu222much in forum Java Servlet
    Replies: 2
    Last Post: November 21st, 2011, 11:54 PM
  4. How to connect to a remote database?
    By ace84 in forum JDBC & Databases
    Replies: 4
    Last Post: February 24th, 2011, 02:09 AM
  5. Relay data from remote port to local port
    By chegers in forum Java Networking
    Replies: 0
    Last Post: November 7th, 2010, 12:46 PM

Tags for this Thread