首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法连接到Groovy中的MySQLA数据库

问无法连接到Groovy中的MySQLA数据库
EN

Stack Overflow用户
提问于 2012-08-11 21:19:10
回答 1查看 887关注 0票数 0

我有一小段代码:

代码语言:javascript
复制
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 . . . . 
    }
  }
}

运行以下代码时:

代码语言:javascript
复制
 groovy -cp mysql-connector-java-5.1.18-bin.jar database.groovy

我得到一个很大的错误:

代码语言:javascript
复制
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

请注意,我可以以以下身份登录mysql:

代码语言:javascript
复制
 mysql -u root -proot -hlocalhost

一切似乎都很好。但我仍然会遇到这些错误。我使用的是Ubuntu 12.04。我在哪里犯了错?

EN

回答 1

Stack Overflow用户

发布于 2015-03-26 13:54:05

您应该为JDBC DB指定端口号,因为MySQL永远不知道每种类型的DB使用哪个端口号。对于MySQL,我认为默认端口号应该设置为3306

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11915050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档