How to get data from sql DB in selenium webdriver? - selenium-webdriver

How to get data from sql DB in selenium webdriver?
I would like to connect the selenium webdriver and sql DB, and need to get value from DB and to use in the selenium testNg framework.
Can any one provide me the right solution.

First you need to make connection with database by using following commands,
DriverManager.getConnection(URL of database, "username", "password" )
To get value, use following commands
ResultSet result = stmt.executeQuery(select * from tablename;);

You need to implement DB connector helper for connect, execute query and close data base connection. After than you can use result of query in your test.
Data Base connector depends on DB type(you need use specify DB driver).
Folowing java methodis illustrated connection with SQL Server Driver:
public java.sql.Connection getConnection() {
try {
Class.forName(SQLServerDriver.class.getName());
con = java.sql.DriverManager.getConnection(getConnectionUrl(), userName, password);
if (con != null) System.out.println("Connection Successful!");
} catch (Exception e) {}
return con;
}
and execute query:
public void executeQuery(String query) {
con = this.getConnection();
if (con != null) {
Statement st = null;
try {
st = con.createStatement();
st.executeQuery(query);
} catch (SQLException e) {}
}
this.closeConnection();
}
than close connection:
public void closeConnection() {
try {
if (con != null)
con.close();
con = null;
} catch (Exception e) {}
}

Related

SqlBulkCopy Login Failed

I have an issue with SqlBulkCopy command when using SQL Server authentication. The issue does not arise with Windows authentication.
SqlBulkCopy sbc = new SqlBulkCopy(sqConn.ConnectionString, SqlBulkCopyOptions.KeepIdentity);
this throws an error:
Login failed for user 'xx'
Code:
SqlBulkCopy sbc = new SqlBulkCopy(sqConn);
This works fine but does not preserve identity column original values.
"persist security info=true" is required in the connection string. Otherwise password is stripped from sqlConn.ConnectionString if the connection is already open.
The solution is quite straightforward but I am still interested to know why SQL server authentication should be different from Windows authentication.
using (SqlTransaction transaction =
sqConn.BeginTransaction())
{
SqlBulkCopy sbc = new SqlBulkCopy(sqConn,SqlBulkCopyOptions.KeepIdentity,transaction);
sbc.DestinationTableName = file;
sbc.BatchSize = 1000;
sbc.NotifyAfter = 1000;
sbc.SqlRowsCopied += new SqlRowsCopiedEventHandler(OnSqlRowsCopied);
sbc.WriteToServer(SourceTable);
transaction.Commit();
}
Try this it worked for me
private static void BulkInsert(DataTable dtExcel, SqlConnection con)
{
try
{
{
if (con.State == ConnectionState.Closed)
con.Open();
var sqlTransactionScope = con.BeginTransaction();
//Open bulkcopy connection.
using (SqlBulkCopy bulkcopy = new SqlBulkCopy(con, SqlBulkCopyOptions.Default, sqlTransactionScope))
{
//Set destination table name
bulkcopy.BulkCopyTimeout = 0;
bulkcopy.BatchSize = 1000;
bulkcopy.DestinationTableName = "[dbo].[cc_alertowner]";
try
{
foreach (DataColumn col in dtExcel.Columns)
{
bulkcopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(col.ColumnName, col.ColumnName));
}
// bulkcopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping("", ""));
// bulkcopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping("DateCreated", "DateCreated"));
if (con.State == ConnectionState.Closed)
con.Open();
bulkcopy.WriteToServer(dtExcel);
sqlTransactionScope.Commit();
}
catch (Exception ex)
{
sqlTransactionScope.Rollback();
throw;
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
Looks Like your SQL Server Mixed Mode authentication is turned off.
Right Click your DB instance and select Properties.
Click on Security and in Server Authentication select second radio button SQL Server and Windows Authentication Mode.
After this Please restart SQL service from services.msc

JDBC connection to linked server

I'm trying to connect from a Java application to a Linked Server I created with MSSQL Server.
The URL string is
jdbc:sqlserver://172.15.230.11
and the query is
SELECT * FROM OPENQUERY(172.15.230.11,'SELECT * FROM myTable WHERE
myCode = 345')
But when I run the program, this exception occurs:
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'myUser'.
The actual code is here:
private static final String DB_URL_LOCAL = "jdbc:sqlserver://172.15.230.11";
private static final String DB_USERNAME_LOCAL = "myUser";
private static final String DB_PASSWORD_LOCAL = "myPassword";
private static final String DB_CLASS = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
static String SQL_READ = "SELECT * FROM OPENQUERY(172.15.230.11,'SELECT * FROM myTable WHERE myCode = 345')";
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(SQL_READ);
resultSet = preparedStatement.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
}
private static Connection getConnection(){
Connection connection = null;
Properties properties = new Properties();
try {
Class.forName(DB_CLASS);
properties.setProperty("characterEncoding", "utf-8");
properties.setProperty("user", DB_USERNAME_LOCAL);
properties.setProperty("password", DB_PASSWORD_LOCAL);
connection = DriverManager.getConnection(DB_URL_LOCAL, properties);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
First of all you have to make sure that you enable remote connections to MSSQLserver.
Then make sure you use a user in your connection wich has sufficient rights to query your schema.
Then make sure you provide the correct source to the JDBC driver:
dbsource= "jdbc:sqlserver://IP:1433;database=MY_SCHEMA";
Then make sure you load the correct JDBC driver and use the appropriate user and password
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dbCon = DriverManager.getConnection(dbsource, user, password);
On top of all that, if your database is using windows authentication, then you can use 'integratedSecurity':
dbsource= "jdbc:sqlserver://IP:1433;database=MY_SCHEMA;integratedSecurity=true;";

Different ways of performing bulk insert into database from a java application

I am looking for different ways of performing bulk insert into database (e.g. SQL Server 2012) from a Java application. I need to insert lot of entities into database very efficiently without making as many calls to database as there are entities.
My requirement is to perform a bulk insert of entities, where an insert of entity in database could involve inserting data into one or more tables. The following are the two ways which I can think of:
Dynamically generate a batch of SQL statements and execute it against the database by making use of native JDBC support.
Construct XML representation of all the entities and then invoke a stored procedure by passing the generated XML. The stored procedure takes care of parsing the XML and inserting the entities to database.
I am new to Java and not having enough knowledge of available frameworks. IMO, the above two approaches seems to be very naive and not leveraging the available frameworks. I am requesting experts to share different ways of achieving bulk insert along with its pros and cons. I am open to MyBatis, Spring-MyBatis, Spring-JDBC, JDBC, etc which solves the problem in an efficient manner.
Thanks.
I have a demo ,JDBC batch processing
file:demo.txt
The content
1899942 ,demo1
1899944 ,demo2
1899946 ,demo3
1899948 ,demo4
Insert the data reads the file content
my code:
public class Test2 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
String sql = "insert into mobile_place(number,place) values(?,?)";
int count=0;
PreparedStatement pstmt = null;
Connection conn = JDBCUtil.getConnection();
try {
pstmt = conn.prepareStatement(sql);
InputStreamReader is = new InputStreamReader(new FileInputStream(new File("D:/CC.txt")),"utf-8");
BufferedReader br = new BufferedReader(is);
conn.setAutoCommit(false);
String s1 = null;
String s2 = null;
while(br.readLine() != null){
count++;
String str = br.readLine().toString().trim();
s1 = str.substring(0, str.indexOf(","));
s2 = str.substring(str.indexOf(",")+1,str.length());
pstmt.setString(1, s1);
pstmt.setString(2, s2);
pstmt.addBatch();
if(count%1000==0){
pstmt.executeBatch();
conn.commit();
conn.close();
conn = JDBCUtil.getConnection();
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(sql);
}
System.out.println("insert "+count+"line");
}
if(count%1000!=0){
pstmt.executeBatch();
conn.commit();
}
long end = System.currentTimeMillis();
System.out.println("Total time spent:"+(end-start));
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
//getConnection()//get jdbc Connection
public static Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(url, userName, password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
Speak for the first time, I hope I can help
I am the demo above use PreparedStatement [Read data calls a PreparedStatement one-off inserted]
JDBC batch There are 3 ways
1.use PreparedStatement
demo:
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(o_url, userName, password);
conn.setAutoCommit(false);
String sql = "INSERT adlogs(ip,website,yyyymmdd,hour,object_id) VALUES(?,?,?,?,?)";
PreparedStatement prest = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
for(int x = 0; x < size; x++){
prest.setString(1, "192.168.1.1");
prest.setString(2, "localhost");
prest.setString(3, "20081009");
prest.setInt(4, 8);
prest.setString(5, "11111111");
prest.addBatch();
}
prest.executeBatch();
conn.commit();
conn.close();
} catch (SQLException ex) {
Logger.getLogger(MyLogger.class.getName()).log(Level.SEVERE, null, ex);
}
2.use Statement.addBatch methods
demo:
conn.setAutoCommit(false);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
for(int x = 0; x < size; x++){
stmt.addBatch("INSERT INTO adlogs(ip,website,yyyymmdd,hour,object_id) VALUES('192.168.1.3', 'localhost','20081009',8,'23123')");
}
stmt.executeBatch();
conn.commit();
3.Direct use of the Statement
demo:
conn.setAutoCommit(false);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
for(int x = 0; x < size; x++){
stmt.execute("INSERT INTO adlogs(ip,website,yyyymmdd,hour,object_id) VALUES('192.168.1.3', 'localhost','20081009',8,'23123')");
}
conn.commit();
Using the above method Insert the 100000 pieces of data Time consuming:
method 1:17.844s
method 2:18.421s
method 3:16.359s
MS JDBC versions later than 4.1 have SQLServerBulkCopy class that I assume is equivalent to one available in .Net and theoretically it should work as fast as bcp command line utility.
https://msdn.microsoft.com/en-us/library/mt221490%28v=sql.110%29.aspx
you can custom your code with JDBC,there is no framework support your requirement

How to retrieve data using JDBC

I have been trying with the following code.
The connection is being made. But the resultSet is coming as empty (not null), whereas there are a couple of entries (2 fields each) in the database for the same.
It does not enter the while condition. I'm new to JDBC, please help!
My code is:
import java.sql.*;
public class JDBCTest123
{
public static void main(String[] args)
{
System.out.println("oracle Connect Example.");
Connection conn = null;
String url = "jdbc:oracle:thin:#127.0.0.1:1521:XE";
String driver = "oracle.jdbc.driver.OracleDriver";
String userName = "system";
String password = "mumpymamai";
Statement stmt = null;
String query = "select * from table1";
try
{
Class.forName(driver);
conn = DriverManager.getConnection(url, userName, password);
stmt = conn.createStatement();
System.out.println("Connected to the database");
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e)
{
e.printStackTrace();
}
}
}
And the output is:
oracle Connect Example.
Connected to the database
Disconnected from database
So few suggestions. I recommend to you use PreparedStatements which are more faster and safer.
PreparedStatement ps = null;
conn = DriverManager.getConnection(url, userName, password);
ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
while (rs.next())
{
// do some work
}
Second suggestion, call close() method in finally block, because application may crash and then your connection won't be closed. Finally block guarantees that will be always called.
Third suggestion if it doesn't work without Exception, probably you have empty table.

Would singleton database connection affect performance in a weblogic clustered environment?

I have a Java EE struts web application using a singleton database connection. In the past, there is only one weblogic server, but now, there are two weblogic servers in a cluster.
Session replication have been tested to be working in this cluster. The web application consist of a few links that will open up different forms for the user to fill in. Each form has a dynamic dropdownlist that will populate some values depending on which form is clicked. These dropdownlist values are retrieved from the oracle database.
One unique issue is that the first form that is clicked, might took around 2-5 seconds, and the second form clicked could take forever to load or more than 5 mins. I have checked the codes and happened to know that the issue lies when an attempt to call the one instance of the db connection. Could this be a deadlock?
public static synchronized DataSingleton getDataSingleton()
throws ApplicationException {
if (myDataSingleton == null) {
myDataSingleton = new DataSingleton();
}
return myDataSingleton;
}
Any help in explaining such a scenario would be appreciated.
Thank you
A sample read operation calling Singleton
String sql = "...";
DataSingleton myDataSingleton = DataSingleton.getDataSingleton();
conn = myDataSingleton.getConnection();
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
try {
pstmt.setString(1, userId);
ResultSet rs = pstmt.executeQuery();
try {
while (rs.next()) {
String group = rs.getString("mygroup");
}
} catch (SQLException rsEx) {
throw rsEx;
} finally {
rs.close();
}
} catch (SQLException psEx) {
throw psEx;
} finally {
pstmt.close();
}
} catch (SQLException connEx) {
throw connEx;
} finally {
conn.close();
}
The Singleton class
/**
* Private Constructor looking up for Server's Datasource through JNDI
*/
private DataSingleton() throws ApplicationException {
try {
Context ctx = new InitialContext();
SystemConstant mySystemConstant = SystemConstant
.getSystemConstant();
String fullJndiPath = mySystemConstant.getFullJndiPath();
ds = (DataSource) ctx.lookup(fullJndiPath);
} catch (NamingException ne) {
throw new ApplicationException(ne);
}
}
/**
* Singleton: To obtain only 1 instance throughout the system
*
* #return DataSingleton
*/
public static synchronized DataSingleton getDataSingleton()
throws ApplicationException {
if (myDataSingleton == null) {
myDataSingleton = new DataSingleton();
}
return myDataSingleton;
}
/**
* Fetching SQL Connection through Datasource
*
*/
public Connection getConnection() throws ApplicationException {
Connection conn = null;
try {
if (ds == null) {
}
conn = ds.getConnection();
} catch (SQLException sqlE) {
throw new ApplicationException(sqlE);
}
return conn;
}
It sounds like you may not be committing the transaction at the end of your use of the connection.
What's in DataSingleton - is it a database connection? Allowing multiple threads to access the same database connection is not going to work, for example once you have more than one user. Why don't you use a database connection pool, for example a DataSource?

Resources