qq_28037587 2017-06-09 04:17 采纳率: 50%
浏览 2614
已采纳

jdbc连接数据库的com.mysql.jdbc.CommunicationsException问题

    我使用jdbc连接数据库,连接不上,上网搜了很多,有人说是数据库请求超时问题,也有说是数据库url,账号或者密码配置错误,我检查了都没有问题。还有一点奇怪的是,我用jdbc-odbc桥连接又可以连上数据库,用可视化工具也可以连上,但是用jdbc方式连接不上。。。我现在想着应该是哪里的配置有问题,请教各位大神,看看有谁知道或者遇到过,谢谢大家了


    这是我的程序
 public static void main(String[] args) {
        Connection ct = null;
        Statement sm = null;
        ResultSet rs = null;
        try {
            //1、加载驱动(作用是把需要的驱动程序加入内存)
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            //2、得到连接(指定连接到哪个数据源)
            //如果配置数据源选择的是Windows nt验证方式,则不需要用户名和密码
            ct = DriverManager.getConnection("jdbc:odbc:MyTest", "root", "root");
            //3、创建Statement或者PreparedStatement[区别]
            //Statement用处是,用于发送sql语句
            sm = ct.createStatement();
            //4、执行(crud、备份数据库、删除数据库......)
            //查询,显示所有部门信息
            //ResultSet结果集,可以理解成一个表行的结果集
            rs = sm.executeQuery("select * from dept");

            //因为rs指向结果集的第一行的前一行,
            //循环取出
            while (rs.next()) {
                int deptno = rs.getInt(1);
                String dname = rs.getString(2);
                String loc = rs.getString(3);
                System.out.println(deptno+" "+dname+" "+loc);
            }
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            //关闭顺序是谁后创建谁先关闭
            try {
                //为了程序健壮
                if (rs != null) {
                    rs.close();
                }
                if (sm != null) {
                    sm.close();
                }
                if (ct != null) {
                    ct.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }

这些是报的异常

 com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.SocketException
MESSAGE: Permission denied: recv failed

STACKTRACE:

java.net.SocketException: Permission denied: recv failed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:152)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:105)
    at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:148)
    at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:176)
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1899)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:481)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:964)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2544)
    at com.mysql.jdbc.Connection.<init>(Connection.java:1474)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:264)
    at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:146)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:195)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:184)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:200)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1086)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1073)
    at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:44)
    at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1810)
    at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:648)


** END NESTED EXCEPTION **



Last packet sent to the server was 0 ms ago.
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:552)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:964)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2544)
    at com.mysql.jdbc.Connection.<init>(Connection.java:1474)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:264)
    at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:146)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:195)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:184)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:200)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1086)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1073)
    at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:44)
    at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1810)
    at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:648)
  • 写回答

5条回答 默认 最新

  • Jackie 仔 2017-06-09 04:39
    关注

    连接mysql方式,必须先添加mysql驱动jar包:
    public Connection getConnection() throws SQLException {
    if (connection == null) {
    String url = "jdbc:mysql://localhost:3306/mysql";
    Properties props = new Properties();
    props.setProperty("user", user);
    props.setProperty("password", password);
    try {
    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection(url, props);
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    }
    return connection;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?