从sclipse到远程mysql服务器的Java ssh2连接

时间:2015-11-24 10:27:45

标签: java mysql eclipse

一直困扰着这个问题,参考这里的代码:http://www.journaldev.com/235/java-program-to-connect-to-remote-database-through-ssh-using-port-forwarding 一直在尝试,但仍然无法得到连接。这是我修改过的代码:

    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.Session;
    import java.sql.Connection;

    public class MySqlConnOverSSH {

    /**
    * Java Program to connect to remote database through SSH using port forwarding
    * @throws SQLException 
    */
    public static void main(String[] args) throws SQLException {

    int lport=5656;
    String rhost="fictitious@germany.com";
    String host="fictitious@germany.com";
    int rport=3306;
    String user="root";
    String password="root";
    String dbuserName = "root";
    String dbpassword = "root";
    String url = "jdbc:mysql://localhost:"+lport+"/test";
    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 assigned_port=session.setPortForwardingL(lport, rhost, rport);
        System.out.println("localhost:"+assigned_port+" -> "+rhost+":"+rport);
        System.out.println("Port Forwarded");

        //mysql database connectivity
        Class.forName(driverName).newInstance();
        conn = DriverManager.getConnection (url, dbuserName, dbpassword);
        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();
        }
    }
}

以下是我得到的错误:

    Connected
    localhost:5656 -> fictitious@germany.com:3306
    Port Forwarded
    java.sql.SQLInvalidAuthorizationSpecException: Could not connect: Access denied for user 'root'@'fictitious@germany.com' (using password: YES)
at org.mariadb.jdbc.internal.util.ExceptionMapper.get(ExceptionMapper.java:121)Closing SSH Connection

at org.mariadb.jdbc.internal.util.ExceptionMapper.throwException(ExceptionMapper.java:69)
at org.mariadb.jdbc.Driver.connect(Driver.java:110)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at myDB.MySqlConnOverSSH.main(MySqlConnOverSSH.java:50)
    Caused by: org.mariadb.jdbc.internal.util.dao.QueryException: Could not connect: Access denied for user 'root'@'fictitious@germany.com' (using password: YES)
at  org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.authentication(AbstractConnectProtocol.java:433)
at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.handleConnectionPhases(AbstractConnectProtocol.java:397)
at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connect(AbstractConnectProtocol.java:318)
at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:630)
at org.mariadb.jdbc.internal.util.Utils.retrieveProxy(Utils.java:580)
at org.mariadb.jdbc.Driver.connect(Driver.java:105)
... 3 more}

我在Java:初学者级别,并尝试探索不同的方式来实现此连接。顺便说一下,我可以使用SecureCRT上给定的用户名和密码连接到数据库,因此授权错误有点令人困惑。如果有人可以指导我做错的事情以及如何纠正它,那将会很棒。非常感谢![/ p>

0 个答案:

没有答案