Skip to content

Commit 2fa5d30

Browse files
rathr1rathr1
rathr1
authored and
rathr1
committed
Added the data base helper
1 parent c060f6d commit 2fa5d30

File tree

7 files changed

+322
-0
lines changed

7 files changed

+322
-0
lines changed

src/com/db/DbConnection.java

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.db;
2+
3+
import java.sql.Connection;
4+
import java.sql.DriverManager;
5+
import java.sql.ResultSet;
6+
import java.sql.ResultSetMetaData;
7+
import java.sql.SQLException;
8+
import java.sql.Statement;
9+
import java.util.LinkedHashMap;
10+
11+
import com.db.dbhelper.DataBaseHelper;
12+
import com.db.dbhelper.SqlDbHelper;
13+
14+
15+
public class DbConnection {
16+
17+
public static void main(String[] args) throws SQLException {
18+
//Step 1 : Getting the Driver - jar
19+
//Setp 2 : Load the driver into the memory
20+
//Step 3 : Get the Connection
21+
//Step 4 : Create a object which will execute query
22+
23+
24+
25+
String connectionString = "jdbc:sqlserver://localhost:1433;databaseName=SqlData;user=sa;password=admin@1234#;integratedSecurity=false;";
26+
Connection connection = null;
27+
Statement statement = null;
28+
ResultSet result;
29+
ResultSetMetaData data;
30+
31+
DataBaseHelper db = new SqlDbHelper();
32+
db.setConnectionString(connectionString);
33+
try {
34+
LinkedHashMap<Integer, LinkedHashMap<String, String>> data1 = db.executeQuery("select * from Table_1 where id = 1");
35+
System.out.println(data1.toString());
36+
} catch (ClassNotFoundException e1) {
37+
e1.printStackTrace();
38+
}
39+
40+
String deleteQuery = "delete from Table_1 where id = 2";
41+
String updateQuery = "update Table_1 set code=105 where Name like 'Name%'";
42+
String insertQuery = "insert into Table_1 (Name,Address,Code,id) VALUES ('Name Four','Address Four','107',1)";
43+
44+
45+
// First apporach for loading the driver in memory
46+
try {
47+
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
48+
connection = DriverManager.getConnection(connectionString);
49+
statement = connection.createStatement();
50+
int count = statement.executeUpdate(insertQuery);
51+
System.out.println("Row affected : "+count);
52+
//result = statement.executeQuery("Select * from Table_1");
53+
//data = result.getMetaData();
54+
55+
/*String [] columnName = new String[data.getColumnCount()];
56+
57+
for(int i = 1; i <= data.getColumnCount(); i++){
58+
columnName[i - 1] = data.getColumnName(i);
59+
}
60+
*/
61+
62+
/*while(result.next()){
63+
System.out.println(String.format("%s : %s : %d : %d", result.getString(1),result.getString(2),result.getInt(3),result.getInt(4)));
64+
}
65+
*/
66+
} catch (ClassNotFoundException | SQLException e) {
67+
e.printStackTrace();
68+
}finally {
69+
statement.close();
70+
connection.close();
71+
}
72+
// Second Apporach
73+
74+
/*try {
75+
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
76+
} catch (SQLException e) {
77+
e.printStackTrace();
78+
}*/
79+
80+
}
81+
82+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.db.dbhelper;
2+
3+
import java.sql.SQLException;
4+
import java.util.LinkedHashMap;
5+
6+
public interface DataBaseHelper {
7+
8+
public DataBaseHelper setConnectionString(String connString);
9+
10+
public int executeUpdate(String sqlQuery) throws ClassNotFoundException, SQLException;
11+
12+
//Outer map object will have data as <row no,MapObject>, inner map object will have the data as <Column Name,Column value>
13+
public LinkedHashMap<Integer, LinkedHashMap<String, String>> executeQuery(String sqlQuery) throws ClassNotFoundException, SQLException ;
14+
15+
}
16+
17+
18+
// Sql data -> Sql helper class implement this DataBaseHelper and provide the implementation of 3 methods
19+
// Oracle data -> Oracle helper class implement this DataBaseHelper and provide the implementation of 3 methods

src/com/db/dbhelper/OracleHelper.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.db.dbhelper;
2+
3+
import java.sql.SQLException;
4+
import java.util.LinkedHashMap;
5+
6+
public class OracleHelper implements DataBaseHelper {
7+
8+
@Override
9+
public DataBaseHelper setConnectionString(String connString) {
10+
// TODO Auto-generated method stub
11+
return null;
12+
}
13+
14+
@Override
15+
public int executeUpdate(String sqlQuery) throws ClassNotFoundException, SQLException {
16+
// TODO Auto-generated method stub
17+
return 0;
18+
}
19+
20+
@Override
21+
public LinkedHashMap<Integer, LinkedHashMap<String, String>> executeQuery(String sqlQuery)
22+
throws ClassNotFoundException, SQLException {
23+
// TODO Auto-generated method stub
24+
return null;
25+
}
26+
27+
}

src/com/db/dbhelper/SqlDbHelper.java

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.db.dbhelper;
2+
3+
import java.io.Closeable;
4+
import java.io.IOException;
5+
import java.sql.Connection;
6+
import java.sql.DriverManager;
7+
import java.sql.ResultSet;
8+
import java.sql.ResultSetMetaData;
9+
import java.sql.SQLException;
10+
import java.sql.Statement;
11+
import java.sql.Types;
12+
import java.util.LinkedHashMap;
13+
14+
public class SqlDbHelper implements DataBaseHelper,Closeable {
15+
16+
private String connString;
17+
18+
private Connection getConnection() throws ClassNotFoundException, SQLException{
19+
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
20+
return DriverManager.getConnection(connString);
21+
}
22+
23+
private Statement getStatement() throws ClassNotFoundException, SQLException{
24+
return getConnection().createStatement();
25+
}
26+
27+
@Override
28+
public DataBaseHelper setConnectionString(String connString) {
29+
this.connString = connString;
30+
return this;
31+
}
32+
33+
@Override
34+
public int executeUpdate(String sqlQuery) throws ClassNotFoundException, SQLException {
35+
return getStatement().executeUpdate(sqlQuery);
36+
}
37+
38+
//Outer map object will have data as <row no,MapObject>, inner map object will have the data as <Column Name,Column value>
39+
40+
@Override
41+
public LinkedHashMap<Integer, LinkedHashMap<String, String>> executeQuery(String sqlQuery) throws ClassNotFoundException, SQLException {
42+
ResultSet result = getStatement().executeQuery(sqlQuery);
43+
44+
String[] columnName = getColumnName(result);
45+
46+
LinkedHashMap<Integer, LinkedHashMap<String, String>> dbData = new LinkedHashMap<>();
47+
int couter = 1; // row no 1
48+
while(result.next()){
49+
dbData.put(couter, getDbData(columnName,result));
50+
couter++;
51+
}
52+
return dbData;
53+
}
54+
55+
private LinkedHashMap<String, String> getDbData(String[] columnName, ResultSet result) throws SQLException {
56+
LinkedHashMap<String, String> columnData = new LinkedHashMap<>();
57+
58+
for(int i = 0; i < columnName.length; i++){
59+
columnData.put(columnName[i], getColumnData(i,result));
60+
}
61+
62+
return columnData;
63+
64+
}
65+
66+
private String getColumnData(int i, ResultSet result) throws SQLException {
67+
int type = result.getMetaData().getColumnType(i + 1);
68+
69+
switch (type) {
70+
case Types.VARCHAR:
71+
return result.getString(i + 1);
72+
case Types.NUMERIC:
73+
return result.getInt(i+1) + "";
74+
}
75+
return null;
76+
}
77+
78+
private String[] getColumnName(ResultSet result) throws SQLException {
79+
ResultSetMetaData data = result.getMetaData();
80+
String[] columnName = new String[data.getColumnCount()];
81+
82+
for(int i = 1; i <= data.getColumnCount(); i++){
83+
columnName[i-1] = data.getColumnName(i);
84+
}
85+
return columnName;
86+
}
87+
88+
@Override
89+
public void close() throws IOException {
90+
// Logic for managing the connection
91+
92+
}
93+
94+
95+
96+
}

src/helper/GenericHelper.java

+4
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,8 @@ public static void UploadFile(String []aImages,String aLocWithFileName) throws F
173173
src.click(imgPatter[2]);
174174
}
175175

176+
public static boolean isElementPresent(By xpath) {
177+
return driver.findElements(xpath).size() >= 1;
178+
}
179+
176180
}

src/helper/GridHelper.java

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package helper;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.openqa.selenium.By;
7+
import org.openqa.selenium.WebElement;
8+
9+
public class GridHelper extends StartWebDriver {
10+
11+
12+
static String getTableXpath(String locator,int row,int col){
13+
return locator + "//tbody//tr[" + row + "]//td[" + col + "]";
14+
}
15+
16+
17+
private static WebElement getGridElement(String locator,int row,int col){
18+
19+
if (GenericHelper.isElementPresent(By.xpath(getTableXpath(locator, row, col) + "//a"))) {
20+
return driver.findElement(By.xpath(getTableXpath(locator, row, col) + "//a"));
21+
}
22+
else if (GenericHelper.isElementPresent(By.xpath(getTableXpath(locator, row, col) + "//input"))) {
23+
return driver.findElement(By.xpath(getTableXpath(locator, row, col) + "//a"));
24+
}else {
25+
return driver.findElement(By.xpath(getTableXpath(locator, row, col)));
26+
}
27+
}
28+
29+
30+
public static String getColumnValue(String locator,int row,int col) {
31+
32+
return getGridElement(locator, row, col).getText();
33+
}
34+
35+
public static List<String> getAllValues(String locator) {
36+
37+
ArrayList<String> list = new ArrayList<String>();
38+
39+
int row = 1;
40+
int col = 1;
41+
42+
while(GenericHelper.isElementPresent(By.xpath(getTableXpath(locator, row, col)))){
43+
while(GenericHelper.isElementPresent(By.xpath(getTableXpath(locator, row, col)))){
44+
list.add((getColumnValue(locator, row, col)));
45+
col++;
46+
}
47+
row++;
48+
col = 1;
49+
}
50+
return list;
51+
}
52+
53+
public static void clickButtonInGrid(String locator,int row,int col) {
54+
getGridElement(locator, row, col).click();
55+
}
56+
57+
}

src/testcase/TestWebTable.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package testcase;
2+
3+
import helper.GridHelper;
4+
import helper.StartWebDriver;
5+
import helper.WindowHelper;
6+
7+
import org.testng.annotations.Test;
8+
9+
public class TestWebTable extends StartWebDriver {
10+
11+
12+
@Test
13+
public void testGrid() {
14+
15+
/*for (int row = 1; row <= 6; row++) {
16+
for (int col = 1; col <= 5; col++) {
17+
String xpath = "//table[@id='wb-auto-1']//tbody//tr[" + row + "]//td[" + col + "]";
18+
//System.out.println(xpath);
19+
WebElement col1 = driver.findElement(By.xpath(xpath));
20+
System.out.print(col1.getText() + " ");
21+
22+
}
23+
System.out.println();
24+
}*/
25+
26+
/*System.out.println(GridHelper.getColumnValue("//table[@id='wb-auto-1']", 1, 4));
27+
List<String> data = GridHelper.getAllValues("//table[@id='wb-auto-1']");
28+
29+
for (String string : data) {
30+
System.out.println(string);
31+
}
32+
System.out.println();*/
33+
WindowHelper.navigateToPage("http://demos.telerik.com/kendo-ui/grid/custom-command");
34+
GridHelper.clickButtonInGrid("//div[@class='k-grid-content k-auto-scrollable']//table[@role='grid']", 2, 3);
35+
}
36+
37+
}

0 commit comments

Comments
 (0)