Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

SWE1007

PROGRAMMING IN JAVA
LAB DIGITAL ASSIGNMENT – III

NAME:ANNAPOORNA A NAIR

REG-NO:21MIS0130
1. Use AWT to design the following page:

Write a java code that performs the following task:


1. Create a JDBC connection to database called INFO
2. Create a table called Person with the following fields
Name – char
Age – int
Nationality – char
Designation – char
3. Use Prepared Statement to insert the values entered by the user from the form into
the
table Person
4. After insertion, display all the records from the table.

CODE:

import java.awt.event.*;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;

class ApplicationForm extends JFrame implements ActionListener {


JLabel l1, name, age, nationality, designation;
JTextField t1, t2, t3, t4;
JButton submit;

ApplicationForm() {
l1 = new JLabel("Application Form");
l1.setBounds(270, 30, 100, 50);
add(l1);
name = new JLabel("Name: ");
name.setBounds(200, 100, 100, 30);
add(name);
t1 = new JTextField();
t1.setBounds(320, 100, 150, 30);
add(t1);
age = new JLabel("Age: ");
age.setBounds(200, 150, 100, 30);
add(age);
t2 = new JTextField();
t2.setBounds(320, 150, 150, 30);
add(t2);
nationality = new JLabel("Nationality: ");
nationality.setBounds(200, 200, 100, 30);
add(nationality);
t3 = new JTextField();
t3.setBounds(320, 200, 150, 30);
add(t3);
designation = new JLabel("Designation: ");
designation.setBounds(200, 250, 100, 30);
add(designation);
t4 = new JTextField();
t4.setBounds(320, 250, 150, 30);
add(t4);
submit = new JButton("Submit");
submit.setBounds(270, 330, 100, 30);
submit.addActionListener(this);
add(submit);
setLayout(null);
setTitle("Application Form");
setSize(700, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent evt) {


try {
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/app", "root", "root");
String query = "insert into app(Name,Age,Nationality,Designation)" +
"values(?,?,?,?)";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, t1.getText());
pstmt.setString(2, t2.getText());
pstmt.setString(3, t3.getText());
pstmt.setString(4, t4.getText());
pstmt.execute();
con.close();
JOptionPane.showMessageDialog(null, "Updated into Database");
dispose();
}
catch (SQLException e) {
System.out.println(e);
}
}

public static void main(String args[]) {


ApplicationForm applicationForm = new ApplicationForm();
}
}

OUTPUT:
After clicking submit button:
2. Implement a multi-tasking program to find the Armstrong numbers between 1 to 10000.
Create five threads and allocate the above given task for a specific range of values. At the end
of the program, output the count of the Armstrong numbers generated on the whole.(Use
Extends Thread class)

CODE:

class armstrong extends Thread {


int start, end;
static int count = 0;

armstrong(int x, int y) {
this.start = x;
this.end = y;
}

public void run() {


int n, a, b, c;
System.out.println("Armstrong numbers from " + start + " - " + end + " :
");
for (int i = start; i <= end; i++) {
int sum = 0;
n = i;
while (n > 0) {
b = n % 10;
sum = sum + (b * b * b);
n = n / 10;
}
if (sum == i) {
count++;
System.out.print(i + "\n");
}
sum = 0;
}
}
}

class armm {
public static void main(String args[]) throws Exception {
armstrong ob1 = new armstrong(1, 2000);
ob1.start();
armstrong ob2 = new armstrong(2001, 4000);
ob2.start();
armstrong ob3 = new armstrong(4001, 6000);
ob3.start();
armstrong ob4 = new armstrong(6001, 8000);
ob4.start();
armstrong ob5 = new armstrong(8001, 10000);
ob5.start();
ob1.join();
ob2.join();
ob3.join();
ob4.join();
ob5.join();
System.out.println("\nCount is: " + armstrong.count);
}
}

OUTPUT:

3. Implement a Client Server program using TCP protocol. The Client should send an
integer value to the server. The Server receives the value and calculates the factorial
value to the server. The server receives the value and calculates the factorial value
and sends the result to the client. The client should finally display the result to the
user.

CODE:
SERVER:-

import java.net.*;
import java.io.*;

class factserver {
public static void main(String args[]) throws Exception {
ServerSocket ss = new ServerSocket(2222);
while (true) {
System.out.println("Server is waiting");

Socket s = ss.accept();
BufferedReader br = new BufferedReader(new
InputStreamReader(s.getInputStream()));
PrintStream p = new PrintStream(s.getOutputStream());
System.out.println("Server is listening");
int a = Integer.parseInt(br.readLine());
int fact = 1;
for (int i = 1; i <= a; i++) {
fact *= i;
}
p.println("Factorial is :" + fact);
}
}
}

CLIENT:-

import java.net.*;
import java.io.*;
import java.util.*;

class factclient {
public static void main(String args[]) throws Exception {
Socket s = new Socket("localhost", 2222);
BufferedReader br = new BufferedReader(new
InputStreamReader(s.getInputStream()));
PrintStream p = new PrintStream(s.getOutputStream());
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number A: ");
int a = sc.nextInt();
p.println(a);
String msg = br.readLine();
System.out.println(msg);
s.close();
}
}

OUTPUT:-

SERVER:-

CLIENT:-

You might also like