0% found this document useful (0 votes)
93 views3 pages

Java Synchronization and ResultSet Demo

Programming languages code

Uploaded by

rohitaher1919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views3 pages

Java Synchronization and ResultSet Demo

Programming languages code

Uploaded by

rohitaher1919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

1. Write a java program for the implementation of synchronization.

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------
class Counter {
private int count = 0;

// Synchronized method to increment the counter


public synchronized void increment() {
count++;
}

// Method to get the counter value


public int getCount() {
return count;
}
}

class IncrementerThread extends Thread {


private Counter counter;
private int increments;

public IncrementerThread(Counter counter, int increments) {


this.counter = counter;
this.increments = increments;
}

@Override
public void run() {
for (int i = 0; i < increments; i++) {
counter.increment();
}
}
}

public class SynchronizationDemo {


public static void main(String[] args) {
Counter counter = new Counter();
int threadsCount = 5;
int incrementsPerThread = 1000;

// Create and start multiple threads


IncrementerThread[] threads = new IncrementerThread[threadsCount];
for (int i = 0; i < threadsCount; i++) {
threads[i] = new IncrementerThread(counter, incrementsPerThread);
threads[i].start();
}

// Wait for all threads to finish


try {
for (int i = 0; i < threadsCount; i++) {
threads[i].join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}

// Print the final counter value


System.out.println("Final Counter Value: " + counter.getCount());
}
}
===================================================================================
============================================================================

output

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------------------------------------------
2. Write a Java Program for the implementation of scrollable ResultSet. Assume
Teacher
table with attributes (TID, TName, Salary) is already created.
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ScrollableResultSetDemo {


public static void main(String[] args) {
// Database connection parameters
String url = "jdbc:mysql://localhost:3306/your_database";
String username = "your_username";
String password = "your_password";

// SQL query to select all records from the Teacher table


String query = "SELECT * FROM Teacher";

// JDBC variables
try (Connection connection = DriverManager.getConnection(url, username,
password);
Statement statement =
connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet resultSet = statement.executeQuery(query)) {

// Move to the last row


resultSet.last();

// Get the row count


int rowCount = resultSet.getRow();

// Print the number of rows


System.out.println("Total number of rows in Teacher table: " +
rowCount);

// Move to the first row


resultSet.beforeFirst();

// Loop through the ResultSet and print each row


System.out.println("Teacher table contents:");
while (resultSet.next()) {
int tid = resultSet.getInt("TID");
String tname = resultSet.getString("TName");
double salary = resultSet.getDouble("Salary");
System.out.println("TID: " + tid + ", TName: " + tname + ", Salary:
" + salary);
}

} catch (SQLException e) {
e.printStackTrace();
}
}
}
===================================================================================
============================================================================

output

You might also like