top of page

learner'sBlog


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();


import openpyxl

# Load the Excel workbook
workbook = openpyxl.load_workbook("C://Users/Nikil/PycharmProjects/pythonBasics/Userdata.xlsx")

# Access the specified sheet by name (using workbook[sheet_name])
sheet = workbook["Sheet1"]

# Get the number of rows and columns in the sheet
rows = sheet.max_row
columns = sheet.max_column
print("Number of rows:", rows)
print("Number of columns:", columns)

# Iterate through each row and column to print cell values
for r in range(2, rows + 1):  
# Start from the second row (assuming headers in the first row)
    for c in range(1, columns + 1):
        # Print the value of each cell, separated by spaces
        print(sheet.cell(row=r, column=c).value, end="      ")
# Move to the next line after printing values of all columns in the current row
    print()

bottom of page