使用java JSch进行ssh隧道 - Kerberos用户名[my-station-user-name]

时间:2014-07-28 11:12:29

标签: java mysql ssh

我在我的服务器上有一个mysql数据库,我想从其他工作站的java应用程序访问它,我成功通过mysql工作台访问它,但是当我尝试通过java连接时,使用下面的代码,我不能访问数据库,控制台显示:

  

[Ljava.lang.String; @ 466cb33cKerberos用户名[my-station-user-name]:

和代码(我在网上发现):

 public void connect() {

        // 
        int assigned_port = 0;   
        final int local_port=3306;

        // Remote host and port
        final int remote_port=3306;
        final String remote_host="server's-ip";

        try {
            JSch jsch = new JSch(); 

            // Create SSH session.  Port 22 is your SSH port which
            // is open in your firewall setup.
            Session session = jsch.getSession("server's username", remote_host, 22);
            session.setPassword("server's password");

            // Additional SSH options.  See your ssh_config manual for
            // more options.  Set options according to your requirements.
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            config.put("Compression", "yes");
            config.put("ConnectionAttempts","2");

            session.setConfig(config);

            // Connect
            session.connect();            

            // Create the tunnel through port forwarding.  
            // This is basically instructing jsch session to send 
            // data received from local_port in the local machine to 
            // remote_port of the remote_host
            // assigned_port is the port assigned by jsch for use,
            // it may not always be the same as
            // local_port.

            assigned_port = session.setPortForwardingL(local_port, 
                    remote_host, remote_port);

        } catch (JSchException e) {            
          //  LOGGER.log(Level.SEVERE, e.getMessage()); return;
        }

        if (assigned_port == 0) {
           //.log(Level.SEVERE, "Port forwarding failed !"); 
            return;
        }

        // Database access credintials.  Make sure this user has
        // "connect" access to this database;

        // these may be initialized somewhere else in your code.
        final String database_user="db-user";
        final String database_password="db-password";
        final String database = "db-name";

        // Build the  database connection URL.
        StringBuilder url =
                new StringBuilder("jdbc:mysql://localhost:");

        // use assigned_port to establish database connection
        url.append(assigned_port).append ("/").append(database).append ("?user=").
                append(database_user).append ("&password=").
                append (database_password);

        try {
            Class.forName(
                    "com.mysql.jdbc.Driver").newInstance();
            java.sql.Connection connection =
                    java.sql.DriverManager.getConnection(url.toString());

            java.sql.DatabaseMetaData metadata = connection.getMetaData();

            this.connection = connection;
            this.statement = this.connection.createStatement();


        } catch (ClassNotFoundException |
                IllegalAccessException |
                InstantiationException |
                java.sql.SQLException e) {
          //  LOGGER.log(Level.SEVERE, e.getMessage());
        }

    }

0 个答案:

没有答案