Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dd7f35e

Browse files
author
Wagner Franchin
committed
first commit
0 parents commit dd7f35e

File tree

9 files changed

+586
-0
lines changed

9 files changed

+586
-0
lines changed

‎README.md‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Developing Java applications using MySQL Server Community and MySQL Connector/J
2+
Sample Java applications which show how to use MySQL Server Community (8.0.11) and MySQL Connector/J (8.0.11)
3+
4+
## Required software
5+
MySQL Community Server compressed TAR or ZIP version (http://dev.mysql.com/downloads/mysql/)
6+
7+
## Instructions
8+
1. Clone the project
9+
2. Import it as a new Eclipse project (or other IDE)
10+
3. Download MySQL Community Server
11+
4. Change variable "MYSQL_PATH" in src/mysqlserver/Utils.java to point to your MySQL Server path
12+
13+
## Aplications
14+
### Aplication 1
15+
The Application1 program implements an application that:
16+
- Initialize server instance (let's call it S1)
17+
- Start S1 in a separate thread
18+
- Connect to S1 (Tip: connect to the "mysql" database, the "test" database is no longer provided)
19+
- Fetch the server version by executing "SELECT VERSION()" and print it to stdout
20+
- Stop S1
21+
22+
### Aplication 2
23+
The Application2 program implements an application that:
24+
- Initialize a server instance (S1)
25+
- Start S1 in a separate thread as replication master, passing the required additional options
26+
- Initialize another server instance (S2)
27+
- Start S2 in a separate thread as replication slave, passing the required additional options
28+
- Set the master (S1) configuration in the slave (S2)
29+
- Start replication in the slave (S2)
30+
- Create a table in the master (S1)
31+
- Wait 2 seconds (for replication to occur)
32+
- Check that the table exists in the slave (S2)
33+
- Stop S2 and S1
34+
35+
## Outputs
36+
### Aplication 1
37+
```
38+
[/home/wfranchi/MySQL/mysql-8.0.11/bin/mysqld, --no-defaults, --initialize-insecure, --basedir=/home/wfranchi/MySQL/mysql-8.0.11, --datadir=/home/wfranchi/eclipse-oxygen-JEE/workspace/sample-java-mysql-replication/bdApp1/data]
39+
Server instance initialized!
40+
[/home/wfranchi/MySQL/mysql-8.0.11/bin/mysqladmin, --protocol=TCP, --port=3306, --user=root, ping]
41+
[/home/wfranchi/MySQL/mysql-8.0.11/bin/mysqld, --no-defaults, --port=3306, --basedir=/home/wfranchi/MySQL/mysql-8.0.11, --datadir=/home/wfranchi/eclipse-oxygen-JEE/workspace/sample-java-mysql-replication/bdApp1/data, --socket=socket, --log-error=/home/wfranchi/eclipse-oxygen-JEE/workspace/sample-java-mysql-replication/bdApp1/log.txt]
42+
wait...
43+
[/home/wfranchi/MySQL/mysql-8.0.11/bin/mysqladmin, --protocol=TCP, --port=3306, --user=root, ping]
44+
wait...
45+
[/home/wfranchi/MySQL/mysql-8.0.11/bin/mysqladmin, --protocol=TCP, --port=3306, --user=root, ping]
46+
*****************************
47+
MySQL Version: 8.0.11
48+
*****************************
49+
[/home/wfranchi/MySQL/mysql-8.0.11/bin/mysqladmin, --protocol=TCP, --port=3306, --user=root, shutdown]
50+
S1 stopped!
51+
```
52+
53+
### Aplication 2
54+
```
55+
[/home/wfranchi/MySQL/mysql-8.0.11/bin/mysqld, --no-defaults, --initialize-insecure, --basedir=/home/wfranchi/MySQL/mysql-8.0.11, --datadir=/home/wfranchi/eclipse-oxygen-JEE/workspace/sample-java-mysql-replication/bdApp2Master/data]
56+
Server instance initialized!
57+
[/home/wfranchi/MySQL/mysql-8.0.11/bin/mysqld, --no-defaults, --initialize-insecure, --basedir=/home/wfranchi/MySQL/mysql-8.0.11, --datadir=/home/wfranchi/eclipse-oxygen-JEE/workspace/sample-java-mysql-replication/bdApp2Slave/data]
58+
[/home/wfranchi/MySQL/mysql-8.0.11/bin/mysqld, --no-defaults, --port=3306, --basedir=/home/wfranchi/MySQL/mysql-8.0.11, --datadir=/home/wfranchi/eclipse-oxygen-JEE/workspace/sample-java-mysql-replication/bdApp2Master/data, --socket=socket, --log-error=/home/wfranchi/eclipse-oxygen-JEE/workspace/sample-java-mysql-replication/bdApp2Master/log.txt, --server-id=1, --log-bin=/home/wfranchi/eclipse-oxygen-JEE/workspace/sample-java-mysql-replication/bdApp2Master/mysql-bin]
59+
Server instance initialized!
60+
[/home/wfranchi/MySQL/mysql-8.0.11/bin/mysqladmin, --protocol=TCP, --port=3306, --user=root, ping]
61+
[/home/wfranchi/MySQL/mysql-8.0.11/bin/mysqld, --no-defaults, --port=3307, --basedir=/home/wfranchi/MySQL/mysql-8.0.11, --datadir=/home/wfranchi/eclipse-oxygen-JEE/workspace/sample-java-mysql-replication/bdApp2Slave/data, --socket=socket, --log-error=/home/wfranchi/eclipse-oxygen-JEE/workspace/sample-java-mysql-replication/bdApp2Slave/log.txt, --server-id=2]
62+
[/home/wfranchi/MySQL/mysql-8.0.11/bin/mysqladmin, --protocol=TCP, --port=3307, --user=root, ping]
63+
wait...
64+
[/home/wfranchi/MySQL/mysql-8.0.11/bin/mysqladmin, --protocol=TCP, --port=3306, --user=root, ping]
65+
[/home/wfranchi/MySQL/mysql-8.0.11/bin/mysqladmin, --protocol=TCP, --port=3307, --user=root, ping]
66+
wait...
67+
[/home/wfranchi/MySQL/mysql-8.0.11/bin/mysqladmin, --protocol=TCP, --port=3306, --user=root, ping]
68+
[/home/wfranchi/MySQL/mysql-8.0.11/bin/mysqladmin, --protocol=TCP, --port=3307, --user=root, ping]
69+
70+
TEST Replication
71+
72+
[Slave]# DESCRIBE PERSON;
73+
Table 'mysql.PERSON' doesn't exist
74+
[Master]# CREATE TABLE PERSON (ID INT NOT NULL, NAME VARCHAR(20), LASTNAME VARCHAR(20));
75+
76+
[Master]# DESCRIBE PERSON;
77+
ID int(11) NO
78+
NAME varchar(20) YES
79+
LASTNAME varchar(20) YES
80+
*****************************
81+
82+
[Slave]# DESCRIBE PERSON;
83+
ID int(11) NO
84+
NAME varchar(20) YES
85+
LASTNAME varchar(20) YES
86+
*****************************
87+
[/home/wfranchi/MySQL/mysql-8.0.11/bin/mysqladmin, --protocol=TCP, --port=3307, --user=root, shutdown]
88+
S2 stopped!
89+
[/home/wfranchi/MySQL/mysql-8.0.11/bin/mysqladmin, --protocol=TCP, --port=3306, --user=root, shutdown]
90+
S1 stopped!
91+
```

‎pom.xml‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.mysql.example</groupId>
4+
<artifactId>sample-java-mysql-connector</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<name>sample-java-mysql-connector</name>
7+
<description>sample-java-mysql-connector</description>
8+
9+
<dependencies>
10+
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
11+
<dependency>
12+
<groupId>mysql</groupId>
13+
<artifactId>mysql-connector-java</artifactId>
14+
<version>8.0.11</version>
15+
</dependency>
16+
</dependencies>
17+
18+
</project>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package applications;
2+
/**
3+
* The Application1 program implements an application that:
4+
- Initialize server instance (let's call it S1)
5+
- Start S1 in a separate thread
6+
- Connect to S1 (Tip: connect to the "mysql" database, the "test" database is no longer provided)
7+
- Fetch the server version by executing "SELECT VERSION()" and print it to stdout
8+
- Stop S1
9+
*
10+
* @author Wagner Jose Franchin
11+
* @version 1.0
12+
* @since 2015年03月28日
13+
*/
14+
15+
import java.io.File;
16+
import java.io.IOException;
17+
import java.sql.*;
18+
19+
import mysqlserver.MySQLServer;
20+
import mysqlserver.RunnableServer;
21+
import mysqlserver.Utils;
22+
23+
public class Application1 {
24+
25+
private static String connectionString;
26+
private static String username;
27+
private static String password;
28+
29+
private static Connection connection;
30+
private static Statement command;
31+
private static ResultSet data;
32+
33+
public static void main(String[] args) throws SQLException, IOException, InterruptedException {
34+
35+
File baseDir = new File(Utils.MYSQL_PATH);
36+
File installDir = new File("bdApp1");
37+
int port = 3306;
38+
39+
connectionString = "jdbc:mysql://localhost:" + port + "/mysql" + Utils.CONNECT_PROPS;
40+
username = "root";
41+
password = "";
42+
43+
final MySQLServer s1 = new MySQLServer(baseDir, installDir, port);
44+
45+
try {
46+
47+
//Server instance initialize
48+
if (s1.init() == 0) {
49+
System.out.println("Server instance initialized!");
50+
}
51+
52+
//Start S1 in a new thread
53+
new Thread(new RunnableServer(s1)).start();
54+
55+
//Test to verify if the server instance is done
56+
while (s1.ping() != 0) {
57+
System.out.println("wait...");
58+
Thread.sleep(500);
59+
}
60+
61+
//Create a DB connection
62+
connection = DriverManager.getConnection(connectionString, username, password);
63+
command = connection.createStatement();
64+
65+
//Create Query
66+
data = command.executeQuery("SELECT VERSION();");
67+
68+
//Print result
69+
if (data.next()) {
70+
System.out.println("*****************************");
71+
System.out.println("MySQL Version: " + data.getString(1));
72+
System.out.println("*****************************");
73+
}
74+
75+
} finally {
76+
77+
if (data != null)
78+
data.close();
79+
if (command != null)
80+
command.close();
81+
if (connection != null)
82+
connection.close();
83+
84+
//Stop S1
85+
s1.stop();
86+
System.out.println("S1 stopped!");
87+
}
88+
}
89+
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package applications;
2+
/**
3+
* The Application2 program implements an application that:
4+
- Initialize a server instance (S1)
5+
- Start S1 in a separate thread as replication master, passing the required additional options
6+
- Initialize another server instance (S2)
7+
- Start S2 in a separate thread as replication slave, passing the required additional options
8+
- Set the master (S1) configuration in the slave (S2)
9+
- Start replication in the slave (S2)
10+
- Create a table in the master (S1)
11+
- Wait 2 seconds (for replication to occur)
12+
- Check that the table exists in the slave (S2)
13+
- Stop S2 and S1
14+
*
15+
* @author Wagner Jose Franchin
16+
* @version 1.0
17+
* @since 2015年03月28日
18+
*/
19+
20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.sql.*;
23+
24+
import mysqlserver.*;
25+
26+
public class Application2 {
27+
28+
private static String connectionStringMaster;
29+
private static String connectionStringSlave;
30+
private static String username;
31+
private static String password;
32+
33+
private static Connection connectionMaster, connectionSlave;
34+
private static Statement commandMaster, commandSlave;
35+
private static ResultSet data;
36+
37+
private static final String USER_TABLE = "PERSON";
38+
39+
public static void main(String[] args) throws SQLException, InterruptedException, IOException {
40+
41+
// Change below path to point to your MySQL binaries
42+
File baseDir = new File(Utils.MYSQL_PATH);
43+
44+
File installDirMaster = new File("bdApp2Master");
45+
File installDirSlave = new File("bdApp2Slave");
46+
47+
int master_port = 3306;
48+
int slave_port = 3307;
49+
50+
connectionStringMaster = "jdbc:mysql://localhost:" + master_port + "/mysql" + Utils.CONNECT_PROPS;
51+
connectionStringSlave = "jdbc:mysql://localhost:" + slave_port + "/mysql" + Utils.CONNECT_PROPS;
52+
53+
username = "root";
54+
password = "";
55+
56+
final MySQLServer s1 = new MasterMySQLServer(baseDir, installDirMaster, master_port, 1, "mysql-bin");
57+
final MySQLServer s2 = new SlaveMySQLServer(baseDir, installDirSlave, slave_port, 2);
58+
59+
try {
60+
61+
/*Initialize a server instance (S1)
62+
Start S1 in a separate thread as replication master, passing the required additional options*/
63+
initAndStarServer(s1);
64+
65+
/*Initialize a server instance (S2)
66+
Start S2 in a separate thread as replication master, passing the required additional options*/
67+
initAndStarServer(s2);
68+
69+
//Test to verify if the server instances are ready
70+
while (s1.ping() != 0 || s2.ping() != 0) {
71+
System.out.println("wait...");
72+
Thread.sleep(500);
73+
}
74+
75+
//Create DB connections
76+
connectionMaster = DriverManager.getConnection(connectionStringMaster, username, password);
77+
connectionSlave = DriverManager.getConnection(connectionStringSlave, username, password);
78+
79+
commandMaster = connectionMaster.createStatement();
80+
commandSlave = connectionSlave.createStatement();
81+
82+
//Set the master (S1) configuration in the slave (S2)
83+
commandSlave.execute("CHANGE MASTER TO MASTER_HOST='localhost', MASTER_PORT=" + master_port + ", MASTER_USER='" + username + "';");
84+
85+
//Start replication in the slave (S2)
86+
commandSlave.execute("START SLAVE;");
87+
88+
testReplication();
89+
90+
}
91+
finally {
92+
93+
if (data != null)
94+
data.close();
95+
96+
if (commandMaster != null)
97+
commandMaster.close();
98+
if (commandSlave != null)
99+
commandSlave.close();
100+
101+
if (connectionMaster != null)
102+
connectionMaster.close();
103+
if (connectionSlave != null)
104+
connectionSlave.close();
105+
106+
//Stop S2
107+
s2.stop();
108+
System.out.println("S2 stopped!");
109+
110+
//Stop S1
111+
s1.stop();
112+
System.out.println("S1 stopped!");
113+
}
114+
}
115+
116+
private static void initAndStarServer(MySQLServer s) throws IOException, InterruptedException {
117+
118+
//Server instance initialize
119+
if (s.init() == 0) {
120+
System.out.println("Server instance initialized!");
121+
}
122+
123+
//Start S in a new thread
124+
new Thread(new RunnableServer(s)).start();
125+
}
126+
127+
/**
128+
* Method to run the replication test
129+
*
130+
* @throws SQLException
131+
* @throws InterruptedException
132+
*/
133+
private static void testReplication() throws SQLException, InterruptedException {
134+
135+
System.out.println("\nTEST Replication");
136+
137+
try {
138+
//Check that the table exists in the slave (S2)
139+
executeQuery(commandSlave, "DESCRIBE " + USER_TABLE + ";", Kind.SLAVE);
140+
}catch(Exception e) {
141+
System.err.println(e.getMessage());
142+
}
143+
144+
//Create a table in the master (S1)
145+
String command = "CREATE TABLE " + USER_TABLE + " (ID INT NOT NULL, NAME VARCHAR(20), LASTNAME VARCHAR(20));";
146+
commandMaster.execute(command);
147+
System.out.println("[" + Kind.MASTER.getName() + "]# " + command);
148+
149+
//Check that the table exists in the slave (S1)
150+
executeQuery(commandMaster, "DESCRIBE " + USER_TABLE + ";", Kind.MASTER);
151+
152+
//Wait 2 seconds (for replication)
153+
Thread.sleep(2000);
154+
155+
//Check that the table exists in the slave (S2)
156+
executeQuery(commandSlave, "DESCRIBE " + USER_TABLE + ";", Kind.SLAVE);
157+
}
158+
159+
/**
160+
* Method to execute queries
161+
*
162+
* @param command
163+
* @param script
164+
* @param kind
165+
* @throws SQLException
166+
*/
167+
private static void executeQuery(Statement command, String script, Kind kind) throws SQLException {
168+
System.out.println("\n[" + kind.getName() + "]# " + script);
169+
data = command.executeQuery(script);
170+
171+
while (data.next()) {
172+
System.out.println(data.getString(1) + " " + data.getString(2) + " " + data.getString(3));
173+
}
174+
System.out.println("*****************************");
175+
}
176+
177+
private enum Kind {
178+
MASTER("Master"), SLAVE("Slave");
179+
180+
private String name;
181+
private Kind(String name) {
182+
this.name = name;
183+
}
184+
public String getName() {
185+
return name;
186+
}
187+
}
188+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /