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

Test case for Valid Coupon Code

This test case checks whether a valid coupon code is successfully applied to a booking cart, the total is updated accordingly, and a success message is displayed. package com.seleniumExamples2024; pu

Keyboard shortcut keys for various operations

General Shortcuts: Ctrl + C: Copy Ctrl + X: Cut Ctrl + V: Paste Ctrl + Z: Undo Ctrl + Y: Redo Ctrl + A: Select All Ctrl + S: Save Ctrl + P: Print Ctrl + F: Find Windows Shortcuts: Windows Key: Open or

bottom of page