I am having issues connecting to mySQL server using ConnectJ driver for JDBC. I wrote a simple program just to establish connectivity as below:
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://[host]:1521/OBIEE11G";
private static final String DB_USER = "OBADW";
private static final String DB_PASSWORD = "OBADW";
public static Connection getConnection() {
Connection dbConnection = null;
try {
// DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
try {
// DriverManager.registerDriver(new com.mysql.jdbc.Driver());
System.out.println("before get connection ***********");
System.out.println("Java version: " + System.getProperty("java.version"));
Driver driver = (Driver) Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("JDBC driver: " + driver.getMajorVersion() + "." + driver.getMinorVersion());
dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
System.out.println("Connection: " + dbConnection.getClass().getName());
System.out.println("after get connection ***********");
return dbConnection;
} catch (SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
return dbConnection;
}
public static void main(String[] args) {
Connection dbConnection = getConnection();
if (dbConnection != null) {
System.out.println("Connected");
} else {
System.out.println("Failed");
}
I am getting the following error:
before get connection ***********
Java version: 1.7.0
JDBC driver: 5.1
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:85)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:57)
at java.lang.reflect.Constructor.newInstance(Constructor.java:541)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:983)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:628)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1014)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2254)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2285)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2084)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:85)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:57)
at java.lang.reflect.Constructor.newInstance(Constructor.java:541)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:226)
at JDBCConnection.getConnection(JDBCConnection.java:32)
at JDBCConnection.main(JDBCConnection.java:45)
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:2949)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:560)
... 17 more
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. Failed
I am using connectJ : mysql-connector-java-5.1.37-bin.jar Java 1.7, JDBC driver 5.1
I am able to ping the host, and connect it using mysql developer. Please suggest.
From your log its clear about "Communications link failure"
听起来很愚蠢,但是您是否在项目属性中正确配置了MySQL Connector.jar文件?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.