java.io.EOFException,SSH端口转发

时间:2015-01-13 05:10:57

标签: mysql ssh

package com.soul.db;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.sql.Connection;
public class JdbcSSHConnection {

/**
 * Java Program to connect to remote database through SSH using port forwarding

 * @throws SQLException 
 */
public static void main(String[] args) throws SQLException {

    int lport=443;
    String rhost="10.99.69.10";
    String host="10.105.27.201";
    int rport=3306;
    String user="root";
    String password="SysPassword1";
    String dbuserName = "root";
    String dbpassword = "DBPassword1";

    String url = "jdbc:mysql://localhost:"+lport+"/proj_db";//"jdbc:mysql://localhost:3306", "root", "mysql"
    String driverName="com.mysql.jdbc.Driver";
    Connection conn = null;
    Session session= null;
    try{
        //Set StrictHostKeyChecking property to no to avoid UnknownHostKey issue
        java.util.Properties config = new java.util.Properties(); 
        config.put("StrictHostKeyChecking", "no");
        JSch jsch = new JSch();
        session=jsch.getSession(user, host, 22);
        session.setPassword(password);
        session.setConfig(config);
        session.connect();
        System.out.println("Connected");
        int assinged_port=session.setPortForwardingL(lport,rhost, rport);
        System.out.println("localhost:"+assinged_port+" -> "+rhost+":"+rport);
        System.out.println("Port Forwarded");

        //mysql database connectivity
        Class.forName(driverName).newInstance(); 
        Class.forName("com.mysql.jdbc.Driver");

        conn = DriverManager.getConnection (url, "root", "DBPassword1");

        // step3 create the statement object
        Statement stmt = conn.createStatement();
        System.out.println ("Database connection established");
        // step4 execute query
        ResultSet rs = stmt.executeQuery("select DISTINCT STUD_NAME from STUD_DETAILS;");
        while (rs.next())
        System.out.println(rs.getString(1));

        // step5 close the connection object
        conn.close();

        //System.out.println ("Database connection established");
       // System.out.println("DONE");

    }catch(Exception e){
        e.printStackTrace();
    }finally{
        if(conn != null && !conn.isClosed()){
            System.out.println("Closing Database Connection");
            conn.close();
        }
        if(session !=null && session.isConnected()){
            System.out.println("Closing SSH Connection");
            session.disconnect();
        }
    }
}

}


我在我的windows机器上安装了SSH安全shell客户端。我正在尝试连接linux机器上安装的mysql服务器。

现在我低于错误:

连 localhost:443 - > 10.99.69.10:3306 端口转发 com.mysql.jdbc.CommunicationsException:由于基础异常导致通信链接失败:

**开始没有异常**

com.mysql.jdbc.CommunicationsException 消息:由于潜在的异常导致通信链路故障:

**开始没有异常**

java.io.EOFException的 消息:无法读取服务器的响应。预计读取4个字节,在连接意外丢失之前读取0个字节。

堆栈跟踪:

java.io.EOFException:无法读取服务器的响应。预计读取4个字节,在连接意外丢失之前读取0个字节。     在com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1997)     在com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:573)     在com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1044)     在com.mysql.jdbc.Connection.createNewIO(Connection.java:2775)     在com.mysql.jdbc.Connection。(Connection.java:1555)     在com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)     at java.sql.DriverManager.getConnection(Unknown Source)     at java.sql.DriverManager.getConnection(Unknown Source)     在com.soul.db.JdbcSSHConnection.main(JdbcSSHConnection.java:54)

**结束了例外**

发送到服务器的最后一个数据包是0毫秒前。

堆栈跟踪:

com.mysql.jdbc.CommunicationsException:由于基础异常导致通信链接失败:

**开始没有异常**

java.io.EOFException的 消息:无法读取服务器的响应。预计读取4个字节,在连接意外丢失之前读取0个字节。

堆栈跟踪:

java.io.EOFException:无法读取服务器的响应。预计读取4个字节,在连接意外丢失之前读取0个字节。     在com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1997)     在com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:573)     在com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1044)     在com.mysql.jdbc.Connection.createNewIO(Connection.java:2775)     在com.mysql.jdbc.Connection。(Connection.java:1555)     在com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)     at java.sql.DriverManager.getConnection(Unknown Source)     at java.sql.DriverManager.getConnection(Unknown Source)     在com.soul.db.JdbcSSHConnection.main(JdbcSSHConnection.java:54)

**结束了例外**

发送到服务器的最后一个数据包是0毫秒前。     在com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:641)     在com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1044)     在com.mysql.jdbc.Connection.createNewIO(Connection.java:2775)     在com.mysql.jdbc.Connection。(Connection.java:1555)     在com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)     at java.sql.DriverManager.getConnection(Unknown Source)     at java.sql.DriverManager.getConnection(Unknown Source)     在com.soul.db.JdbcSSHConnection.main(JdbcSSHConnection.java:54)

**结束了例外**

发送到服务器的最后一个数据包是0毫秒前。     在com.mysql.jdbc.Connection.createNewIO(Connection.java:2847) 关闭SSH连接     在com.mysql.jdbc.Connection。(Connection.java:1555)     在com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)     at java.sql.DriverManager.getConnection(Unknown Source)     at java.sql.DriverManager.getConnection(Unknown Source)     在com.soul.db.JdbcSSHConnection.main(JdbcSSHConnection.java:54)

0 个答案:

没有答案