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