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()
Nesta página do site você pode assistir ao vídeo on-line 26B. Java Basics for Selenium - Connecting to Database and Execute Queries using Java duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário subbus tech 30 Março 2019, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 127 vezes e gostou 1 espectadores. Boa visualização!