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()
Auf dieser Seite können Sie das Online-Video 26B. Java Basics for Selenium - Connecting to Database and Execute Queries using Java mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer subbus tech 30 März 2019 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 127 Mal angesehen und es wurde von 1 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!