我目前有一个运行 Centos 5.2-i386-LAMP 服务器的 VPS。我已经设法完成所有设置并创建了一个 MySQL 数据库。我正在创建一个 Android 应用程序,我需要访问该服务器。另外,我正在使用 Java 进行开发。
这是我一直在尝试的:
package com.example.jsch;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import com.jcraft.jsch.JSch;
public class JSchUtility {
private JSch jsch = new JSch();
private String SSHHostname = "123.45.678.91";
private String SSHUsername = "root";
private String SSHPassword = "password";
private int SSHPort = 22;
private String MySQLHostname = "127.0.0.1";
private String MySQLUsername = "username";
private String MySQLPassword = "password";
private int hostPort = 3306;
private int dbHostPort = 3306;
private String MySQLSchemaName = "sqldb";
private Connection connection = null;
public void sessionConnect() {
try {
com.jcraft.jsch.Session session = jsch.getSession(SSHUsername, SSHHostname, SSHPort);
session.setPassword(SSHPassword);
final Properties config = new Properties();
config.put( "StrictHostKeyChecking", "no" );
session.setConfig(config);
session.connect();
// This is where the error comes. PortForwardingL: local port 127.0.0.1:3306 cannot be bound.
session.setPortForwardingL(hostPort, MySQLHostname, dbHostPort);
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + MySQLSchemaName, MySQLUsername, MySQLPassword);
} catch(Exception e) {
e.printStackTrace();
}
}
public void sessionDestroy() {
try {
if(connection != null) {
connection.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
我开始相信也许 Java 不是最适合这种类型的东西?如果有更好的语言可以将您的应用程序连接到实时服务器,我想知道。