ja30
-
Write a java program for the implementation of synchronization. [15 M]
-
class Counter {
private int count = 0;public synchronized void increment() {
count++;
}public synchronized void decrement() {
count--;
}public int getCount() {
return count;
}
}
public class SynchronizationExample {
public static void main(String[] args) {
Counter counter = new Counter();
Thread thread1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
});
Thread thread2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.decrement();
}
});
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Final Count: " + counter.getCount());
}
}
- Write a Java Program for the implementation of scrollable ResultSet. Assume Teacher
table with attributes (TID, TName, Salary) is already created.
import java.sql.*;
public class ScrollableResultSetExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/your_database_name";
String username = "your_username";
String password = "your_password";
try (Connection connection = DriverManager.getConnection(url, username, password)) {
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet resultSet = statement.executeQuery("SELECT * FROM Teacher");
resultSet.first();
while (resultSet.next()) {
int tid = resultSet.getInt("TID");
String tname = resultSet.getString("TName");
double salary = resultSet.getDouble("Salary");
System.out.println("Teacher ID: " + tid + ", Teacher Name: " + tname + ", Salary: " + salary);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}