我使用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)