0

I have this small code :

import groovy.sql.Sql
class GroovySqlExample1{
  static void main(String[] args) {
    def sql = Sql.newInstance("jdbc:mysql://localhost/jdb", "root",
           "root", "com.mysql.jdbc.Driver")
    sql.eachRow("select * from user"){ row ->
       //other code . . . . 
    }
  }
}

When I run the following code with the :

 groovy -cp mysql-connector-java-5.1.18-bin.jar database.groovy

I get this big error:

Caught: 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.
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 com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2332)
    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 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 GroovySqlExample1.main(database.groovy:4)
Caused by: java.net.ConnectException: Connection refused
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:294)
    ... 9 more

Note that, I can log into mysql as :

 mysql -u root -proot -hlocalhost

Everything seems to be fine. But still I get those errors. I'm using Ubuntu 12.04. Where I'm making the mistake?

batman
  • 4,728
  • 8
  • 39
  • 45

1 Answers1

0

You should specify port number for your MySQL DB as JDBC never knows which port number is used for each types of DBs. For MySQL, I believe default port number should be set as 3306

Yuki Tanaka
  • 215
  • 2
  • 9