Image source: http://examples.javacodegeeks.com/wp-content/uploads/2015/12/Assert-Link.jpg
For what motive do we make use of JDBC comparatively ODBC?
ODBC is produced and written in C, which is stage Independent. Whereas JDBC API is a detail gives set of interfaces that are composed and created in Java programming dialect.
How to connect to database utilizing JDBC?
The below are the Steps to Connect to database, sooner than proceeding, you need MySQL Connector. You can download from here Download MySQL Connector Jar and encompass it the assemble manner as we encompass selenium webdriver jar.
Load and Registering the Driver
Setting up Connection.
Making Statement Object
Execute the Statement
Shutting the affiliation.
1. Load and Register the Driver:
For enrolling the Driver we Load the Driver class utilizing forName() angle. forName() is the static manufacturing plant angle which is accessible in predefined class ordinarily called "Class". This angle stacks the category which is specified as parameter.
Class.forName("com.mysql.jdbc.Driver");//class.forName stack the Driver class
Inside this Driver class will enlist the driving power by utilizing static angle ordinarily called registerDriver().
2. Building up Connection:
For construction up affiliation with database we identify static angle ordinarily called getConnection(...) introduce in DriverManager Class. This method incorporates three contentions of string sort. i.e., url, username and mystery key
DriverManager.getConnection("jdbc:mysql://localhost:3306/Employee","root","root");
URL incorporates "jdbc(main protocol):mysql(sub conference for mySql)://localhost:3306(sub identify for mysql (host:prot))/Employee(database)" and this angle return sort is Connection Object ie., Association
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Employee","root","root");
3. Making Statement Object:
For making articulation question we needs to inevitably identify a angle ordinarily called createStatement() which is accessible in Connection Interface.
con.createStatement();
What's more, this angle returns Statement question and it's no contention method.
statement st= con.createStatement();
four.Executing Queries:
For executing inquiries there are particular ways exhibit in Statement Interface for recovering information and for refreshing information.
Recovering information:
for executing elect queries(for bringing information) we identify a angle ordinarily called executeQuery(String qry) by taking string as parameter.
st.executeQuery("Select * from Employee");
This angle returns ResultSet protest.
Resultset rs= st.executeQuery("Select * from Employee");//once executeQuery() executes the inquiry and shops the wisdom in to ResultSet question.
Presently we needs to inevitably get the wisdom from ResultSet question. To get to the resultset protest it makes use of a angle ordinarily called next() which exhibits in ResultSet Interface.
Naturally Resultset reference 'rs' focuses to sooner than first column. it actions rs to next line and returns genuine. When it returns genuine we get better the comprehend-how in first column. next() returns false when rs focuses to after the last column. this next() will rehashes the execution utilizing while circle till it returns false.
Refreshing information:
To refresh information in a desk we make use of a process ordinarily called executeUpdate(String str) which returns complete range an incentive as what variety of information have been refreshed.
5. Shutting the Connection:
When execution of all announcements were accomplished we needs to inevitably shut every one of the associations by utilizing angle ordinarily called shut() instruct in Connection interface
There are fundamentally two things that we needs to inevitably desire to ascertain to confirm database with Selenium.
To with, in the wake of making any job in the cyber net device program (Front-discontinue), we might desire to decide the database if every one of the delicate features we submitted are put away effectually. Presently as an instance, while growing a document, we can enter a bunch of comprehend-how email Id, username and so forth. To ensure a document is made, we can decide document made in database with every one of the delicate features customer gave.
Other case can resemble, subsequent to playing out any job, we might desire to decide the comprehend-how showed in the UI is high by hunting on the Database.