Skip to content

Commit f6a0eee

Browse files
committed
https://jira.baeldung.com/browse/BAEL-8292
1 parent 48395d7 commit f6a0eee

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-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,72 @@
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.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import java.sql.Connection;
9+
import java.sql.DriverManager;
10+
import java.sql.SQLException;
11+
import java.sql.Statement;
12+
import java.sql.ResultSet;
13+
14+
public class RemoteMysqlConnection {
15+
16+
private static final String HOST = "HOST";
17+
private static final String USER = "USERNAME";
18+
private static final String PRIVATE_KEY = "PATH_TO_KEY";
19+
private static final int PORT = 22;
20+
21+
private static final String DATABASE_HOST = "DATABASE_HOST";
22+
private static final int DATABASE_PORT = 3306;
23+
private static final String DATABASE_USERNAME = "DATABASE_USERNAME";
24+
private static final String DATABASE_PASSWORD = "DATABASE_PASSWORD";
25+
26+
public static JSch setUpJsch() throws JSchException {
27+
JSch jsch = new JSch();
28+
jsch.addIdentity(PRIVATE_KEY);
29+
return jsch;
30+
}
31+
32+
public static Session createSession(JSch jsch) throws JSchException {
33+
Session session = jsch.getSession(USER, HOST, PORT);
34+
session.setConfig("StrictHostKeyChecking", "no");
35+
session.connect();
36+
return session;
37+
}
38+
39+
public static int tunnelNetwork(Session session) throws JSchException {
40+
int portForwarding = session.setPortForwardingL(0, DATABASE_HOST, DATABASE_PORT);
41+
return portForwarding;
42+
}
43+
44+
public static Connection databaseConnection(int port) throws SQLException {
45+
String databaseUrl = "jdbc:mysql://" + DATABASE_HOST + ":" + port + "/baeldung";
46+
Connection connection = DriverManager.getConnection(databaseUrl, DATABASE_USERNAME, DATABASE_PASSWORD);
47+
return connection;
48+
}
49+
50+
public static void createTable(Connection connection, String tableName) throws SQLException {
51+
String createTableSQL = "CREATE TABLE " + tableName + " (id INT, data VARCHAR(255))";
52+
Statement statement = connection.createStatement();
53+
statement.execute(createTableSQL);
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+
Statement statement = connection.createStatement();
59+
statement.execute(insertDataSQL);
60+
}
61+
62+
public static boolean isTableExists(Connection connection, String tableName) throws SQLException {
63+
Statement statement = connection.createStatement();
64+
ResultSet resultSet = statement.executeQuery("SHOW TABLES LIKE '" + tableName + "'");
65+
return resultSet.next();
66+
}
67+
68+
public static void disconnect(Session session, Connection connection) throws SQLException {
69+
session.disconnect();
70+
connection.close();
71+
}
72+
}
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_whenConnectingToRemoteDatabase_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
@@ -136,6 +136,7 @@
136136
<module>spring-boot-persistence-5</module>
137137
<module>hibernate-annotations-2</module>
138138
<module>hibernate-reactive</module>
139+
<module>my-sql</module>
139140
</modules>
140141

141142
<properties>

0 commit comments

Comments
 (0)