Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Advanced Java Practical Slip 12 Answers

 



Q.1. Advanced Java:

A) Write a java program to count the number of records in a table.

Answer :

import java.io.*;

import java.sql.*;

 

class Slip12A {

    public static void main(String args[]) throws Exception {

 

        Statement stmt;

        ResultSet rs;

        int cnt = 0;

 

        Class.forName("com.mysql.jdbc.Driver");

 

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bcadb", "root", "");

 

        stmt = con.createStatement();

 

        rs = stmt.executeQuery("select * from student");

 

        while (rs.next()) {

            cnt++;

        }

 

        System.out.println("Total number of records in table is : " + cnt);

 

        con.close();

    }

}

 

B) Write a program in java which will show lifecycle (creation, sleep, and dead) of a

thread. Program should print randomly the name of thread and value of sleep time. The

name of the thread should be hard coded through constructor. The sleep time of a thread

will be a random integer in the range 0 to 4999.

Answer :

import java.util.Random;

public class MyThread extends Thread {
    private String threadName;

    public MyThread(String name) {
        threadName = name;
    }

    public void run() {
        Random rand = new Random();
        int sleepTime = rand.nextInt(5000);
        System.out.println(threadName + " sleeping for " + sleepTime + " ms.");
        try {
            Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
            System.out.println(threadName + " interrupted.");
        }
        System.out.println(threadName + " finished.");
    }

    public static void main(String[] args) {
        MyThread t = new MyThread("MyThread");
        System.out.println(t.getName() + " created.");
        t.start();
        try {
            t.join();
        } catch (InterruptedException e) {
            System.out.println("Main thread interrupted.");
        }
        System.out.println(t.getName() + " dead.");
    }
}

Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Advanced Java Practical Slip 12 Answers Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Advanced Java Practical Slip 12 Answers Reviewed by technical_saurabh on September 21, 2022 Rating: 5

No comments:

Powered by Blogger.