Skip to content

Commit 3f91ff3

Browse files
authored
Merge pull request #17816 from Michaelin007/mysql-ssh
https://jira.baeldung.com/browse/BAEL-8292
2 parents 560a5c9 + 7cd54ff commit 3f91ff3

File tree

5 files changed

+164
-0
lines changed

5 files changed

+164
-0
lines changed

persistence-modules/my-sql/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## Relevant Articles
2+

persistence-modules/my-sql/pom.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.baeldung.my-sql</groupId>
6+
<artifactId>my-sql</artifactId>
7+
<version>0.1.0-SNAPSHOT</version>
8+
<name>my-sql</name>
9+
<packaging>jar</packaging>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>persistence-modules</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.github.mwiede</groupId>
20+
<artifactId>jsch</artifactId>
21+
<version>${jsch.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.mysql</groupId>
25+
<artifactId>mysql-connector-j</artifactId>
26+
<version>${mysql-connector-java.version}</version>
27+
</dependency>
28+
</dependencies>
29+
30+
<properties>
31+
<mysql-connector-java.version>8.0.32</mysql-connector-java.version>
32+
<jsch.version>0.2.20</jsch.version>
33+
</properties>
34+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.baeldung.connectingtoremotemysqlssh;
2+
3+
import com.jcraft.jsch.JSch;
4+
import com.jcraft.jsch.JSchException;
5+
import com.jcraft.jsch.Session;
6+
7+
import java.sql.Connection;
8+
import java.sql.DriverManager;
9+
import java.sql.SQLException;
10+
import java.sql.Statement;
11+
import java.sql.ResultSet;
12+
13+
public class RemoteMysqlConnection {
14+
15+
private static final String HOST = "HOST";
16+
private static final String USER = "USERNAME";
17+
private static final String PRIVATE_KEY = "PATH_TO_KEY";
18+
private static final int PORT = 22;
19+
20+
private static final String DATABASE_HOST = "DATABASE_HOST";
21+
private static final int DATABASE_PORT = 3306;
22+
private static final String DATABASE_USERNAME = "DATABASE_USERNAME";
23+
private static final String DATABASE_PASSWORD = "DATABASE_PASSWORD";
24+
25+
public static JSch setUpJsch() throws JSchException {
26+
JSch jsch = new JSch();
27+
jsch.addIdentity(PRIVATE_KEY);
28+
return jsch;
29+
}
30+
31+
public static Session createSession(JSch jsch) throws JSchException {
32+
Session session = jsch.getSession(USER, HOST, PORT);
33+
session.setConfig("StrictHostKeyChecking", "no");
34+
session.connect();
35+
return session;
36+
}
37+
38+
public static int tunnelNetwork(Session session) throws JSchException {
39+
int portForwarding = session.setPortForwardingL(0, DATABASE_HOST, DATABASE_PORT);
40+
return portForwarding;
41+
}
42+
43+
public static Connection databaseConnection(int port) throws SQLException {
44+
String databaseUrl = "jdbc:mysql://" + DATABASE_HOST + ":" + port + "/baeldung";
45+
Connection connection = DriverManager.getConnection(databaseUrl, DATABASE_USERNAME, DATABASE_PASSWORD);
46+
return connection;
47+
}
48+
49+
public static void createTable(Connection connection, String tableName) throws SQLException {
50+
String createTableSQL = "CREATE TABLE " + tableName + " (id INT, data VARCHAR(255))";
51+
try (Statement statement = connection.createStatement()) {
52+
statement.execute(createTableSQL);
53+
}
54+
}
55+
56+
public static void insertData(Connection connection, String tableName) throws SQLException {
57+
String insertDataSQL = "INSERT INTO " + tableName + " (id, data) VALUES (1, 'test data')";
58+
try (Statement statement = connection.createStatement()) {
59+
statement.execute(insertDataSQL);
60+
}
61+
}
62+
63+
public static boolean isTableExists(Connection connection, String tableName) throws SQLException {
64+
try (Statement statement = connection.createStatement()) {
65+
ResultSet resultSet = statement.executeQuery("SHOW TABLES LIKE '" + tableName + "'");
66+
return resultSet.next();
67+
}
68+
}
69+
70+
public static void disconnect(Session session, Connection connection) throws SQLException {
71+
session.disconnect();
72+
connection.close();
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.connectingtoremotemysqlssh;
2+
3+
import com.jcraft.jsch.JSch;
4+
import com.jcraft.jsch.JSchException;
5+
import com.jcraft.jsch.Session;
6+
import org.junit.jupiter.api.Test;
7+
import java.sql.Connection;
8+
import java.sql.SQLException;
9+
import static com.baeldung.connectingtoremotemysqlssh.RemoteMysqlConnection.setUpJsch;
10+
import static com.baeldung.connectingtoremotemysqlssh.RemoteMysqlConnection.createSession;
11+
import static com.baeldung.connectingtoremotemysqlssh.RemoteMysqlConnection.disconnect;
12+
import static com.baeldung.connectingtoremotemysqlssh.RemoteMysqlConnection.createTable;
13+
import static com.baeldung.connectingtoremotemysqlssh.RemoteMysqlConnection.insertData;
14+
import static com.baeldung.connectingtoremotemysqlssh.RemoteMysqlConnection.isTableExists;
15+
import static org.junit.jupiter.api.Assertions.assertNotNull;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
17+
18+
class RemoteMysqlConnectionLiveTest {
19+
20+
@Test
21+
public void givenJschSessionAndMySqlServer_whenConnectingToRemoteMySqlDatabase_thenSuccess() throws JSchException, SQLException {
22+
JSch jsch = setUpJsch();
23+
Session session = createSession(jsch);
24+
int port = RemoteMysqlConnection.tunnelNetwork(session);
25+
Connection connection = RemoteMysqlConnection.databaseConnection(port);
26+
assertNotNull(connection);
27+
disconnect(session, connection);
28+
}
29+
30+
@Test
31+
public void givenJschSessionAndMySqlServer_whenCreatingTableAndAddingRecordToTheTable_thenSuccess() throws JSchException, SQLException {
32+
JSch jsch = setUpJsch();
33+
Session session = createSession(jsch);
34+
int port = RemoteMysqlConnection.tunnelNetwork(session);
35+
Connection connection = RemoteMysqlConnection.databaseConnection(port);
36+
String newTableName = "test_table";
37+
createTable(connection, newTableName);
38+
insertData(connection, newTableName);
39+
disconnect(session, connection);
40+
}
41+
42+
@Test
43+
public void givenJschSessionAndMySqlServer_whenVerifyingTable_thenTableExists() throws JSchException, SQLException {
44+
JSch jsch = setUpJsch();
45+
Session session = createSession(jsch);
46+
int port = RemoteMysqlConnection.tunnelNetwork(session);
47+
Connection connection = RemoteMysqlConnection.databaseConnection(port);
48+
String newTableName = "test_table";
49+
assertTrue(isTableExists(connection, newTableName));
50+
disconnect(session, connection);
51+
}
52+
53+
}

persistence-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
<!--<module>spring-boot-persistence-5</module>--> <!-- failing after spring boot upgrade to 3.3.2 -->
138138
<module>hibernate-annotations-2</module>
139139
<module>hibernate-reactive</module>
140+
<module>my-sql</module>
140141
</modules>
141142

142143
<properties>

0 commit comments

Comments
 (0)