top of page
back.jpg

Java JDBC Database Operations Example


package com.seleniumExamples2024;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DatabaseExample {

    public static void main(String[] args) {
        try {
 // Establish a connection to the MySQL database
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/Education", "username", "password");

 // Create a statement for executing SQL queries
            Statement statement = connection.createStatement()           // Insert a new record into the "student" table
            int rowsAffected = statement.executeUpdate("INSERT INTO student VALUES('Fazil', 105)")
// Execute a SELECT query to retrieve names from the "student" table ordered by name
            ResultSet resultSet = statement.executeQuery("SELECT name FROM student ORDER BY name");
 // Print the names retrieved from the SELECT query
            while (resultSet.next()) {
                System.out.println(resultSet.getString("name"));
            }

 // Execute a SELECT query to retrieve details of a student with ID 102
            ResultSet result = statement.executeQuery("SELECT * FROM student WHERE id = 102");

  // Print details of the student with ID 102
            while (result.next()) {
                System.out.println("Name: " + result.getString("name"));
                System.out.println("ID: " + result.getInt("id"));
            }

// Close the result set, statement, and connection
            resultSet.close();
            result.close();
            statement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

To Establish a connection to the MySQL database with the specified URL, username, and password.

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/Education", “username", “password");


To Create a Statement object for executing SQL queries

Statement statement = connection.createStatement();


To  Insert a new record into the "student" table

int rowsAffected = statement.executeUpdate("INSERT INTO student VALUES('Kathy', 105)");


To Execute a SELECT query and to retrieve names from the "student" table ordered by name.

ResultSet resultSet = statement.executeQuery("SELECT name FROM student ORDER BY name");

Prints the names retrieved from the SELECT query.


while (resultSet.next())

{

System.out.println(resultSet.getString("name"));

}


Executes a SELECT query to retrieve details of a student with ID 102.


ResultSet result = statement.executeQuery("SELECT * FROM student WHERE id = 102");


Prints details of the student with ID 102

while (result.next())

{

System.out.println("Name: " + result.getString("name"));

 System.out.println("ID: " + result.getInt("id"));

}:

Closing Resources

resultSet.close();

result.close();

statement.close();

connection.close();

1 view0 comments

Recent Posts

See All

What is Flaky test? Steps to resolve Test Flakiness

A flaky test An automated test in a software testing environment that may produce inconsistent results, sometimes passing and sometimes failing, even when applied to the same version of the software u

Retrieve data from a JSON file

To retrieve data from a JSON file using Java and Selenium, you can use a library like Gson or Jackson for handling JSON parsing. { "username": "Tester1", "password": "Test1234", "url": "https://demoqa

bottom of page