26B. Java Basics for Selenium - Connecting to Database and Execute Queries using Java

Pubblicato il: 30 marzo 2019
sul canale di: subbus tech
127
1

In this Video we will see how to connect to MySQL Database and how to execute the following queries.
1. Select Query (Retrieving data from database tables)
2. Insert Query (Inserting data into a database table)
3. Update Query (Updating data in a database table)
4. Delete Query (Deleting data from a database table)

Java Database Connectivity:
JDBC is java database connectivity.
Java JDBC is a Java API to connect and execute queries with the database. JDBC API uses jdbc drivers to connect with the database.
There are 5 steps to connect any java application with the database using JDBC. These steps are as follows:
• Register the Driver class
• Create connection
• Create statement
• Execute queries
• Close connection

1. Register the Driver Class
The forName() method of class Class is used to register the driver class.

For MySQL it is:
Class.forName("com.mysql.jdbc.Driver");

For Oracle it is:
Class.forName("oracle.jdbc.driver.OracleDriver");

2. Create a connection object
For MySQL:
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname","root","root");

For Oracle:
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","password");

3. Create the statement object
For MysQL/Oracle:
Statement stmt=con.createStatement();

4. Execute Query
ResultSet rs=stmt.executeQuery("select * from emp");
While(rs.next()){
System.out.println(rs.getInt(1)+” “+rs.getString(2));
}
Note: For selecting data we use stmt.executeQuery
For inserting, updating, deleting data we use stmt.executeUpdate

5. Close the connection
con.close()


In questa pagina del sito puoi guardare il video online 26B. Java Basics for Selenium - Connecting to Database and Execute Queries using Java della durata di ore minuti seconda in buona qualità , che l'utente ha caricato subbus tech 30 marzo 2019, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 127 volte e gli è piaciuto 1 spettatori. Buona visione!