Skip to content

Commit 1b46d29

Browse files
authored
Create DBContainerTesting
1 parent 5b30d5f commit 1b46d29

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

Docker/DBContainerTesting

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
==> Create mysql container
2+
3+
docker run --name mysql57 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=1234 -d mysql/mysql-server:5.7
4+
5+
Container will be created successfully
6+
7+
Create a data base now:
8+
9+
# docker exec -it mysql57 bash
10+
11+
This command access the mysql container and allow to execute commands on database.
12+
13+
Inside a container, type:
14+
15+
# mysql -h localhost -u root -p
16+
17+
Remember, the password is 1234. First, we need to create a user for out-of-container access because root access is not allowed:
18+
19+
20+
CREATE USER 'demo_java' IDENTIFIED BY 'java';
21+
grant all on *.* to 'demo_java'@'%' identified by '1234';
22+
FLUSH PRIVILEGES;
23+
24+
To create a database, paste DDL bellow:
25+
26+
CREATE DATABASE hello_java CHARACTER SET utf8 COLLATE utf8_general_ci;
27+
28+
show databases;
29+
30+
you can see hello_java db
31+
32+
use hello_java
33+
34+
Create tables and insert rows in the DB
35+
36+
CREATE TABLE Customers (CustomerName varchar(20),ContactName varchar(255),Address varchar(255),City varchar(255),PostalCode varchar(255),Country varchar(255)
37+
);
38+
39+
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
40+
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
41+
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
42+
VALUES ('John', 'Tom B', 'Skagen', 'Stava', '40', 'USA');
43+
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
44+
VALUES ('CardinalJohn', 'Erichsen', 'S21', 'Stech', '4045', 'Belgium');
45+
46+
47+
select * from Customers;
48+
49+
50+
OR
51+
52+
Another way to start a database and automatic create a hello_java database and demo_java user:
53+
54+
docker run --name mysql57 -p 3306:3306 \
55+
-e MYSQL_ROOT_PASSWORD=1234 \
56+
-e MYSQL_USER=demo_java \
57+
-e MYSQL_PASSWORD=1234 \
58+
-e MYSQL_DATABASE=hello_java \
59+
-d mysql/mysql-server:5.7
60+
61+
And repeat above steps for table and row creation.
62+
63+
64+
Now steps to access db from eclipse
65+
66+
67+
Create a maven project and add dependency in the pom.xml file
68+
69+
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
70+
<dependency>
71+
<groupId>mysql</groupId>
72+
<artifactId>mysql-connector-java</artifactId>
73+
<version>6.0.6</version>
74+
</dependency>
75+
76+
Create a class
77+
78+
package com.qa.dbTest;
79+
80+
import java.sql.Connection;
81+
import java.sql.DriverManager;
82+
import java.sql.SQLException;
83+
84+
public class DbtestDemo {
85+
86+
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
87+
// TODO Auto-generated method stub
88+
89+
Connection con = DriverManager.getConnection("jdbc:mysql://34.71.212.58:3306/hello_java","demo_java", "1234");
90+
91+
/* String driver="com.mysql.cj.jdbc.Driver";
92+
93+
String userName="demo_java";
94+
String password="1234";
95+
Class.forName(driver).newInstance();
96+
97+
con=DriverManager.getConnection(dburl, userName, password);*/
98+
System.out.println("connection established successfully");
99+
100+
Statement st= con.createStatement();
101+
102+
ResultSet rs= st.executeQuery("select * from Customers");
103+
104+
while(rs.next())
105+
{
106+
System.out.println("CustomerName: " + rs.getString("CustomerName"));
107+
}
108+
rs.close();
109+
110+
}
111+
112+
}
113+
114+
Execute the class.. Connection to you DB will be successfull.
115+
116+
package com.qa.dbTest;
117+
118+
import java.sql.Connection;
119+
import java.sql.DriverManager;
120+
import java.sql.PreparedStatement;
121+
import java.sql.SQLException;
122+
123+
public class InsertData {
124+
125+
public static void main(String[] args) throws SQLException {
126+
// TODO Auto-generated method stub
127+
Connection con = DriverManager.getConnection("jdbc:mysql://34.123.119.97:3306/hello_java","demo_java", "1234");
128+
129+
System.out.println("connection established successfully");
130+
131+
PreparedStatement ps = con.prepareStatement("INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)\r\n" +
132+
"VALUES ('Sonal2', 'Vishal2', 'S21245', 'Stecsdfsdh', '4045', 'Gurgaon')");
133+
ps.executeUpdate();
134+
con.close();
135+
}
136+
137+
}
138+
139+
140+

0 commit comments

Comments
 (0)