0% found this document useful (0 votes)
51 views10 pages

21BCM075 Csi0403 Assignment 4

The document contains 5 questions and their solutions related to Java programming concepts like exception handling, threads lifecycle, JDBC, Swing components like JTable. Each question asks to write a Java program demonstrating a concept and the solutions provide the code to do so by taking user input, connecting to a database, handling exceptions, displaying outputs etc.

Uploaded by

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

21BCM075 Csi0403 Assignment 4

The document contains 5 questions and their solutions related to Java programming concepts like exception handling, threads lifecycle, JDBC, Swing components like JTable. Each question asks to write a Java program demonstrating a concept and the solutions provide the code to do so by taking user input, connecting to a database, handling exceptions, displaying outputs etc.

Uploaded by

Chirag Saraf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

ROLL NUMBER : 21BCM075

COURSE CODE : CSI0403


ASSIGNMENT : 4
Q.1 Write a Java program that demonstrate the use of
try, catch and finally in terms of Exception Handling?
import java.util.Scanner;

public class sa1 {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("ENTER FIRST NUMBER :");
        int n1 = sc.nextInt();
        System.out.println("ENTER SECOND NUMBER :");
        int n2 = sc.nextInt();
        try {
            System.out.println(n1 + " + " + n2 + " = " + (n1 + n2));
            System.out.println(n1 + " - " + n2 + " = " + (n1 - n2));
            System.out.println(n1 + " * " + n2 + " = " + (n1 * n2));
            System.out.println(n1 + " / " + n2 + " = " + (n1 / n2));
        } catch (ArithmeticException d) {
            System.out.println(d);
        } finally {
            System.out.println("Done..");
    }
  }

OUTPUT :
Q.2 Explain the complete Life cycle of Thread with
diagram?
Lifecycle of a thread :
It has 5 different phases in its lifecycle :
a. New
b. Runnable
c. Running
d. Waiting
e. Dead
Q.3 Write a Java program that 4 inputs in Text field as
Rollno, Name, Phone no and email id from the user on
the clicking of Registration button and insert it into the
table Student.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class sa3 extends JFrame implements ActionListener {


    sa3() {

        JLabel l1 = new JLabel("Rollno");


        add(l1);
        l1.setBounds(100, 100, 100, 30);

        JTextField t1 = new JTextField();


        add(t1);
        t1.setBounds(200, 100, 150, 30);

        JLabel l2 = new JLabel("Name");


        add(l2);
        l2.setBounds(100, 150, 100, 30);

        JTextField t2 = new JTextField();


        add(t2);
        t2.setBounds(200, 150, 150, 30);

        JLabel l3 = new JLabel("Phone no");


        add(l3);
        l3.setBounds(100, 200, 100, 30);

        JTextField t3 = new JTextField();


        add(t3);
        t3.setBounds(200, 200, 150, 30);

        JLabel l4 = new JLabel("email id");


        add(l4);
        l4.setBounds(100, 250, 100, 30);

        JTextField t4 = new JTextField();


        add(t4);
        t4.setBounds(200, 250, 150, 30);

        JButton b1 = new JButton("Registration");


        add(b1);
        b1.setBounds(100, 350, 130, 30);

        JLabel l5 = new JLabel("Registration Successfully..");


        add(l5);
        l5.setBounds(250, 350, 180, 30);
        l5.setVisible(false);

        setLayout(null);
        setSize(500, 500);
        setTitle("Registration Form");
        getContentPane().setBackground(new Color(204, 255, 204));
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                try {
                    String DB_URL = "jdbc:mysql://localhost:3306/assignment3_ooad";
                    String USER = "root";
                    String PASS = "1234";
                    Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
                    System.out.println("database connected...");

                    String q1 = "insert into student(Rollno,Name,Phone_no,email_id) values(?,?,?,?)";


                    PreparedStatement pstmt = conn.prepareStatement(q1);
                    pstmt.setString(1, t1.getText());
                    pstmt.setString(2, t2.getText());
                    pstmt.setString(3, t3.getText());
                    pstmt.setString(4, t4.getText());

                    pstmt.executeUpdate();
                    System.out.println("data inserted succesfully..");
                    conn.close();
                    l5.setVisible(true);
                } catch (SQLException a) {
                    a.printStackTrace();
        }

      }
        });

  }

    public static void main(String[] args) {


        new sa3();
  }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
  }
}

OUTPUT :
Q.4 Extend the above program by verifying Roll no and
email id by taking input from 2 text field and click login
button which will check that both entries are matched
perfectly or not and display “ Login Successfully”.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class sa4 extends JFrame implements ActionListener {


    sa4() {

        JButton b1 = new JButton("Login");


        add(b1);
        b1.setBounds(220, 250, 80, 30);

        JButton b2 = new JButton("Reset");


        add(b2);
        b2.setBounds(100, 250, 80, 30);

        JLabel l1 = new JLabel("emailID");


        add(l1);
        l1.setBounds(80, 100, 80, 30);

        JLabel l2 = new JLabel("Rollno");


        add(l2);
        l2.setBounds(80, 150, 150, 30);

        JTextField t1 = new JTextField();


        add(t1);
        t1.setBounds(160, 100, 150, 30);

        JTextField t2 = new JTextField();


        add(t2);
        t2.setBounds(160, 150, 150, 30);

        setLayout(null);
        setSize(400, 400);
        setTitle("Login Form");
        getContentPane().setBackground(new Color(255, 204, 219));
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                t1.setText("");
                t2.setText("");
      }
        });

        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                try {
                    String DB_URL = "jdbc:mysql://localhost:3306/assignment3_ooad";
                    String USER = "root";
                    String PASS = "1234";
                    Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
                    System.out.println("database connected...");

                    Statement stmt = conn.createStatement();


                    String emailID = t1.getText();
                    String rollno = t2.getText();

                    String q = "select * from student where email_id='" + emailID + "'and Rollno='" + rollno +
"'";
                    ResultSet rs = stmt.executeQuery(q);
                    if (rs.next()) {
                        dispose();
                        JFrame jf = new JFrame();
                        JLabel l3 = new JLabel("Login Successfull..");
                        jf.add(l3);
                        l3.setBounds(70, 100, 300, 100);
                        l3.setFont(new Font("Cascadia code", Font.BOLD, 26));
                        jf.setLayout(null);
                        jf.setSize(400, 400);
                        jf.getContentPane().setBackground(new Color(255, 204, 219));
                        jf.setVisible(true);

                    } else {
                        JFrame f = new JFrame();
                        JOptionPane.showMessageDialog(f, "emailID or Rollno!!");
                        t1.setText("");
                        t2.setText("");
          }
                    conn.close();

                } catch (Exception a) {
                    a.printStackTrace();
        }
      }

        });
  }

    public static void main(String[] args) {


        new sa4();
  }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

  }
}

OUTPUT :
Q.5 Explain the use of Component “ JTABLE” in the
swing with the help of code.
import javax.swing.*;
import java.awt.event.*;

public class sa5 implements ActionListener {


    sa5() {

        JFrame jf = new JFrame();

        String data[][] = { { "21BCM069", "JEET" },


                { "21BCM075", "CHIRAG" },
                { "21BCM079", "VATSAL" } };
        String column[] = { "ROLL NUMBER", "NAME" };
        JTable jt = new JTable(data, column);
        jt.setBounds(30, 40, 200, 300);
        JScrollPane sp = new JScrollPane(jt);
        jf.add(sp);
        jf.setVisible(true);

        jf.setSize(400, 400);

  }

    public static void main(String[] args) {


        new sa5();
  }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

  }
}

OUTPUT :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy