Reputation: 149
I have written this code in DB utility and here I am passing a configuration from property file and select query in same method just to check DB connection. But now my requirement is to pass query and configuration from feature file itself. So that we can use this utility everywhere in the framework. Please refer the code
package Utilities;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class DatabaseConnectionUtils {
public static Properties readPropertiesFile(String fileName) throws IOException {
FileInputStream fis = null;
Properties prop = null;
try {
fis = new FileInputStream(fileName);
prop = new Properties();
prop.load(fis);
} catch(FileNotFoundException fnfe) {
fnfe.printStackTrace();
} finally {
fis.close();
}
return prop;
}
public static void connect(String serverIP, Integer port, String keyspace, String username, String password) {
Cluster cluster;
Session session;
Cluster.Builder b = Cluster.builder().addContactPoint(serverIP);
if (port != null) {
b.withPort(port).withCredentials(username,password);
}
cluster = b.build();
session = cluster.connect(keyspace);
String cqlStatement = "select * from partner_configuration where partnerid='15918' allow filtering;";
for (Row row : session.execute(cqlStatement)) {
System.out.println(row.toString());
}
session.close();
}
public static void main(String[] args) throws IOException {
Properties prop = readPropertiesFile("C:\\NMS-Framework\\untitled\\src\\test\\resources\\DB-Credentials.properties");
String address=prop.getProperty("ServerIP");
int port= Integer.parseInt(prop.getProperty("port"));
String keyspace=prop.getProperty("keyspace");
String username=prop.getProperty("username");
String password= prop.getProperty("password");
connect(address,port,keyspace,username,password);
}
}
Upvotes: 1
Views: 13