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

Publicado el: 30 marzo 2019
en el canal de: 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()


En esta página del sitio puede ver el video en línea 26B. Java Basics for Selenium - Connecting to Database and Execute Queries using Java de Duración hora minuto segunda en buena calidad , que subió el usuario subbus tech 30 marzo 2019, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 127 veces y le gustó 1 a los espectadores. Disfruta viendo!