我尝试验证前端和数据库结果之间的总评论。因为数据库只能通过VPN访问,所以它让我卡住了。我尝试使用下面的代码,但结果是空的,控制台中唯一连接的ssh
public class ConnectDB {
private static Connection connection = null;
private static Session session = null;
private static void connectToServer(String dataBaseName) throws SQLException {
connectSSH();
connectToDataBase(dataBaseName);
}
public static void connectSSH() throws SQLException {
String sshHost = "my ssh host";
String sshuser = "my ssh user";
String dbuserName = "username db";
String dbpassword = "pass db";
String SshKeyFilepath = "/Users/mac/.ssh/id_rsa";
int localPort = 8740; // any free port can be used
String remoteHost = "ip db";
int remotePort = 3306;
String localSSHUrl = "localhost";
/***************/
String driverName = "com.mysql.jdbc.Driver";
try {
java.util.Properties config = new java.util.Properties();
JSch jsch = new JSch();
session = jsch.getSession(sshuser, sshHost, 22);
jsch.addIdentity(SshKeyFilepath);
config.put("StrictHostKeyChecking", "no");
config.put("ConnectionAttempts", "3");
session.setConfig(config);
session.connect();
System.out.println("SSH Connected");
Class.forName(driverName).newInstance();
int assinged_port = session.setPortForwardingL(localPort, remoteHost, remotePort);
System.out.println("localhost" + assinged_port + " -> " + remoteHost + ":" + remotePort);
System.out.println("Port Forwarded");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void connectToDataBase(String dataBaseName) throws SQLException {
String dbuserName = "username db";
String dbpassword = "pass db";
int localPort = 8740; // any free port can be used
String localSSHUrl = "ip db"; //since ssh connected i guess to put ip db
try {
//mysql database connectivity
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setServerName(localSSHUrl);
//dataSource.setPortNumber(localPort);
dataSource.setUser(dbuserName);
dataSource.setAllowMultiQueries(true);
dataSource.setPassword(dbpassword);
dataSource.setDatabaseName(dataBaseName);
connection = dataSource.getConnection();
System.out.print("Connection to server successful!:" + connection + "\n\n");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void closeConnections() {
CloseDataBaseConnection();
CloseSSHConnection();
}
public static void CloseDataBaseConnection() {
try {
if (connection != null && !connection.isClosed()) {
System.out.println("Closing Database Connection");
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void CloseSSHConnection() {
if (session != null && session.isConnected()) {
System.out.println("Closing SSH Connection");
session.disconnect();
}
}
// works ONLY FOR single query (one SELECT or one DELETE etc)
public static ResultSet executeMyQuery(String query, String dataBaseName) {
ResultSet resultSet = null;
try {
connectToServer(dataBaseName);
Statement stmt = (Statement) connection.createStatement();
resultSet = stmt.executeQuery(query);
System.out.println("Database connection success");
} catch (SQLException e) {
e.printStackTrace();
}
return resultSet;
}
public static void DeleteOrganisationReferencesFromDB(String organisationsLike) {
try {
connectToServer("ServerName");
Statement stmt = (Statement) connection.createStatement();
ResultSet resultSet = stmt.executeQuery("select * from DB1");
String organisationsToDelete = "";
List<String> organisationsIds = new ArrayList<String>();
// create string with id`s values to delete organisations references
while (resultSet.next()) {
String actualValue = resultSet.getString("id");
organisationsIds.add(actualValue);
}
for (int i = 0; i < organisationsIds.size(); i++) {
organisationsToDelete = " " + organisationsToDelete + organisationsIds.get(i);
if (i != organisationsIds.size() - 1) {
organisationsToDelete = organisationsToDelete + ", ";
}
}
stmt.executeUpdate(" DELETE FROM `DB1`.`table1` WHERE `DB1`.`table1`.`organisation_id` in ( " + organisationsToDelete + " );");
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeConnections();
}
}
public static List<String> getOrganisationsDBNamesBySubdomain(String organisationsLike) {
List<String> organisationDbNames = new ArrayList<String>();
ResultSet resultSet = executeMyQuery("select `DB`.organisation.dbname from `DB1`.organisation where subdomain like '" + organisationsLike + "%'", "DB1");
try {
while (resultSet.next()) {
String actualValue = resultSet.getString("dbname");
organisationDbNames.add(actualValue);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeConnections();
}
return organisationDbNames;
}
public static List<String> getAllDBNames() {
// get all live db names incentral DB
List<String> organisationDbNames = new ArrayList<String>();
ResultSet resultSet = executeMyQuery("show databases", "DB1");
try {
while (resultSet.next()) {
String actualValue = resultSet.getString("Database");
organisationDbNames.add(actualValue);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeConnections();
}
return organisationDbNames;
}
public static void deleteDataBasesByName(List<String> DataBasesNamesList) {
try {
connectSSH();
int dataBasesAmount = DataBasesNamesList.size();
for (int i = 0; i < dataBasesAmount; i++) {
connectToDataBase(DataBasesNamesList.get(i));
Statement stmt = (Statement) connection.createStatement();
stmt.executeUpdate("DROP database `" + DataBasesNamesList.get(i) + "`");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
CloseDataBaseConnection();
closeConnections();
}
}
但是在控制台中的结果,没有迹象表明执行了查询 *查询在另一个文件
conn.connectSSH();
conn.executeMyQuery("select shade_item FROM nubr_product_shade WHERE shade_id=1", "database_name");
SSH已连接 localhost8740 - &gt; 172.xx.x.xx:3306 港口转发
答案 0 :(得分:0)
为什么不用操作系统工具来做到这一点?
例如(MySQL数据库):
ssh -R 3306:127.0.0.1:3306 username@server.com -NnT
现在jdbc连接到端口3306的localhost。