Assignment Java

You might also like

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

Create a PROJECT table with fields project_id, Project_name, Project_description,

Project_Status. etc. Insert values in the table. Display all the details of the PROJECT
table in a tabular format on the screen.(using swing).

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

class ProjectDisplay extends JFrame implements ActionListener {

Connection con;
ResultSet rs;
Statement st;

static JTable table;


// This column names are taken from database table which we have created...i.e here we have create
project table.
String[] columnNames = { "p_id", "p_name", "p_description", "p_status" };
JFrame frm;
JPanel p1;
String p_id = "", p_name = "", p_description = "", p_status = "";
JTextField txtid, txtname, txtdesc, textstatus;

JButton Insert, Update, Delete, Display, Exit;

Insert iobj;
Update uobj;
Delete dobj;

ProjectDisplay() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("PROJECT INFO");

p1 = new JPanel();

p1.setLayout(new GridLayout(5, 5, 30, 30));

Insert = new JButton("Insert");


p1.add(Insert);

Update = new JButton("Update");


p1.add(Update);

Delete = new JButton("Delete");


p1.add(Delete);

Display = new JButton("Display");


p1.add(Display);

Exit = new JButton("Exit");


p1.add(Exit);

Insert.addActionListener(this);
Update.addActionListener(this);
Delete.addActionListener(this);
Display.addActionListener(this);
Exit.addActionListener(this);

add(p1, BorderLayout.CENTER);
setVisible(true);
setSize(600, 600);
}// ProjectDetails

public void actionPerformed(ActionEvent ae) {

if (ae.getSource() == Display) {
frm = new JFrame("DISPLAY");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

setLayout(new BorderLayout());
DefaultTableModel model = new DefaultTableModel();

model.setColumnIdentifiers(columnNames);
table = new JTable();

table.setModel(model);

table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

table.setFillsViewportHeight(true);

JScrollPane scroll = new JScrollPane(table);

scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

try {
Class.forName("org.postgresql.Driver");
// Use database name & password according to your "dbname","pass"
con = DriverManager.getConnection("jdbc:postgresql://192.168.1.21:5432/ty90", "ty90", "");

st = con.createStatement();
rs = st.executeQuery("select * from project");
while (rs.next()) {
p_id = rs.getString(1);
p_name = rs.getString(2);
p_description = rs.getString(3);
p_status = rs.getString(4);

// This all coloumn names are taken from project table.


model.addRow(new Object[] { p_id, p_name, p_description, p_status });

} // while
frm.add(scroll);

frm.setVisible(true);
frm.setSize(400, 400);
} // try

catch (Exception e) {
JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE);
}
}

if (ae.getSource() == Insert) {
iobj = new Insert();
}

if (ae.getSource() == Update) {
uobj = new Update();
}

if (ae.getSource() == Delete) {
dobj = new Delete();
}

if (ae.getSource() == Exit) {

System.exit(1);

public static void main(String arg[]) {

new ProjectDisplay();
}// main
}// class

class Insert extends JFrame implements ActionListener {


JTextField txtst, txtpid, txtpname, txtdsc;
JButton btnadd, btnclear;

Insert() {
setTitle("Peoject Record Inserts");
setSize(400, 500);
setVisible(true);
setLayout(new GridLayout(6, 2, 40, 40));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

JLabel id = new JLabel("Enter Project Number: ");


add(id);
txtpid = new JTextField(10);
add(txtpid);

JLabel name = new JLabel("Enter Project Name: ");


add(name);
txtpname = new JTextField(10);
add(txtpname);

JLabel dsc = new JLabel("Enter Project Description: ");


add(dsc);
txtdsc = new JTextField(10);
add(txtdsc);

JLabel st = new JLabel("Enter Project Status: ");


add(st);
txtst = new JTextField(10);
add(txtst);

btnadd = new JButton("Insert");


add(btnadd);
btnadd.addActionListener(this);

btnclear = new JButton("Cancel");


add(btnclear);
btnclear.addActionListener(this);

public void actionPerformed(ActionEvent ae) {

int p_id = Integer.parseInt(txtpid.getText());


String pname = (txtpname.getText());
String ds = (txtdsc.getText());
String st = (txtst.getText());

Connection conn = null;


PreparedStatement pstmt = null;
try {
// load a driver
Class.forName("org.postgresql.Driver");

// Establish Connection
conn = DriverManager.getConnection("jdbc:postgresql://192.168.1.21:5432/ty90", "ty90", "");

if (ae.getSource() == btnadd) {
pstmt = conn.prepareStatement("insert into project values(?,?,?,?)");

pstmt.setInt(1, p_id);
pstmt.setString(2, pname);
pstmt.setString(3, ds);
pstmt.setString(4, st);

int result = pstmt.executeUpdate();


if (result == 1) {

JOptionPane.showMessageDialog(null, "Succesfully Inserted", st,


JOptionPane.INFORMATION_MESSAGE);
}

pstmt.close();
conn.close();
} // if of submit

if (ae.getSource() == btnclear) {
// System.exit(1);
txtpid.setText("");
txtdsc.setText("");
txtpname.setText("");
txtst.setText("");

} // if of clear

} // try
catch (Exception e) {
JOptionPane.showMessageDialog(null, e, "ERROR OCCURED",
JOptionPane.ERROR_MESSAGE);
System.out.println(e);
} // catch
}

}// insert

class Update extends JFrame implements ActionListener {

JTextField txtst, txtpid, txtpname, txtdsc;


JButton btnadd;
Update() {
setTitle("Peoject Record Update");
setSize(500, 500);
setVisible(true);
setLayout(new GridLayout(3, 2, 40, 40));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

JLabel pid = new JLabel("Enter Projcet ID To Update: ");


add(pid);
txtpid = new JTextField(10);
add(txtpid);

JLabel st = new JLabel("Enter Updated Project Status: ");


add(st);
txtst = new JTextField(10);
add(txtst);

btnadd = new JButton("Update");


add(btnadd);
btnadd.addActionListener(this);

}// update()

public void actionPerformed(ActionEvent ae) {


String str = ae.getActionCommand();

int p_id = Integer.parseInt(txtpid.getText());


String st = (txtst.getText());

Connection conn = null;


PreparedStatement pstmt = null;
try {
// load a driver
Class.forName("org.postgresql.Driver");

// Establish Connection
conn = DriverManager.getConnection("jdbc:postgresql://192.168.1.21:5432/ty90", "ty90", "");
if (str.equals("Update")) {
String SQL = "Update project set p_status=? where p_id=?";
pstmt = conn.prepareStatement(SQL);

pstmt.setString(1, st);
pstmt.setInt(2, p_id);

int update = pstmt.executeUpdate();


if (update == 1) {
JOptionPane.showMessageDialog(null, "Succesfully Updated", st,
JOptionPane.INFORMATION_MESSAGE);
} // if

txtpid.setText("");
txtst.setText("");
pstmt.close();
conn.close();
} // if

} // try
catch (Exception e) {
System.out.println(e);
} // catch

}// method
}// update class

class Delete extends JFrame implements ActionListener {

JTextField txtpid, txtst;


JButton btnadd;

Delete() {
setTitle("Peoject Record Delete");
setSize(400, 400);
setVisible(true);
setLayout(new GridLayout(3, 2, 20, 20));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

JLabel pid = new JLabel("Enter Projcet ID To Delete: ");


add(pid);
txtpid = new JTextField(10);
add(txtpid);

JLabel st = new JLabel("Enter Updated Project Status: ");


add(st);
txtst = new JTextField(10);
add(txtst);

btnadd = new JButton("Delete");


add(btnadd);
btnadd.addActionListener(this);

}// Delete()

public void actionPerformed(ActionEvent ae) {


String str = ae.getActionCommand();
int p_id = Integer.parseInt(txtpid.getText());
String st = (txtst.getText());

Connection conn = null;


PreparedStatement pstmt = null;
try {
// load a driver
Class.forName("org.postgresql.Driver");

// Establish Connection
conn = DriverManager.getConnection("jdbc:postgresql://192.168.1.21:5432/ty90", "ty90", "");
if (str.equals("Delete")) {
String SQL = "delete from project where p_id =?";
pstmt = conn.prepareStatement(SQL);

pstmt.setInt(1, p_id);

int delete = pstmt.executeUpdate();


if (delete == 1) {
JOptionPane.showMessageDialog(null, "Succesfully Deleted", st,
JOptionPane.INFORMATION_MESSAGE);
}
txtpid.setText("");
txtst.setText("");
pstmt.close();
conn.close();
} // if

} // try
catch (Exception e) {
System.out.println(e);
} // catch
}
}// class

b) Write a program to display information about the database and list all the tables in the
database. (Use DatabaseMetaData).

import java.sql.*;

public class Metadata {


public static void main(String[] args) {
try {
// load a driver
Class.forName("org.postgresql.Driver");

// Establish Connection
Connection conn =
DriverManager.getConnection("jdbc:postgresql://192.168.1.21:5432/ty90","ty90", "");
DatabaseMetaData dbmd = conn.getMetaData();
System.out.println("\
t-----------------------------------------------------------------------");
System.out.println("\t\tDriver Name : " + dbmd.getDriverName());
System.out.println("\t\tDriver Version : " + dbmd.getDriverVersion());
System.out.println("\t\tUserName : " + dbmd.getUserName());
System.out.println("\t\tDatabase Product Name : " +
dbmd.getDatabaseProductName());
System.out.println("\t\tDatabase Product Version : " +
dbmd.getDatabaseProductVersion());
System.out.println("\
t---------------------------------------------------------------------");

String table[] = { "TABLE" };


ResultSet rs = dbmd.getTables(null, null, null, table);
System.out.println("\t\tTable Names:");

while (rs.next()) {
System.out.println(rs.getString("TABLE_NAME"));
}
rs.close();
conn.close();
} // try
catch (Exception e) {
System.out.println(e);
} // catch
}// main
}// metadata

c) Write a program to display information about all columns in the DONAR table using
ResultSetMetaData.

import java.sql.*;

public class DONOR {


public static void main(String[] args) {
try {
// load a driver
Class.forName("org.postgresql.Driver");

// Establish Connection
Connection conn =
DriverManager.getConnection("jdbc:postgresql://192.168.1.21:5432/ty90","ty90", "");

Statement stmt = null;


stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from donor");
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println("\t-------------------------------------------------");

int count = rsmd.getColumnCount();


System.out.println("\t No. of Columns: " + rsmd.getColumnCount());
System.out.println("\t-------------------------------------------------");
for (int i = 1; i <= count; i++)
{
System.out.println("\t\tColumn No : " + i);
System.out.println("\t\tColumn Name : " + rsmd.getColumnName(i));
System.out.println("\t\tColumn Type : " + rsmd.getColumnTypeName(i));
System.out.println("\t\tColumn Display Size : " + rsmd.getColumnDisplaySize(i));
System.out.println();
} // for
System.out.println("\t--------------------------------------------------");

rs.close();
stmt.close();
conn.close();
} // try
catch (Exception e) {
System.out.println(e);
} // catch
}
}

Set B
a) Create a MOBILE table with fields Model_Number, Model_Name, Model_Color,
Sim_Type, NetworkType, BatteryCapacity, InternalStorage, RAM and
ProcessorType. Insert values in the table. Write a menu driven program to pass the
input using Command line argument to perform the following operations on MOBILE
table.
1. Insert 2. Modify 3. Delete 4. Search 5. View All 6. Exit

import java.sql.*;
import java.util.*;

public class mobile {


public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
Statement stmt = null;
ResultSet rs = null;
try {
// load a driver
Class.forName("org.postgresql.Driver");

// Establish Connection
conn = DriverManager.getConnection("jdbc:postgresql://192.168.1.21:5432/ty90","ty90", "");

Scanner sc = new Scanner(System.in);


System.out.println("\n\tMobile Information\n");
do {
System.out.println("\n\t1.Insert \n\t2.Modify\n\t3.Delete\n\t4.Search \n\t5.View All\n\t6.Exit\
n");
System.out.println("Enter Your Choice: ");
int ch = sc.nextInt();
switch (ch) {
case 1:
pstmt = conn.prepareStatement("insert into mobile values(?,?,?,?,?,?,?,?)");

System.out.println("Enter Model_Number: ");


int mno = sc.nextInt();
pstmt.setInt(1, mno);

sc.nextLine();
System.out.println("Enter Model_Name: ");
String name = sc.nextLine();
pstmt.setString(2, name);

System.out.println("Enter Model_Color: ");


String color = sc.nextLine();
pstmt.setString(3, color);

System.out.println("Enter Sim_Type: ");


String sim = sc.nextLine();
pstmt.setString(4, sim);

System.out.println("Enter Battery Capacity: ");


int Battery = sc.nextInt();
pstmt.setInt(5, Battery);

System.out.println("Enter Internal Storage In GB: ");


int internal = sc.nextInt();
pstmt.setInt(6, internal);

System.out.println("Enter RAM In GB: ");


int ram = sc.nextInt();
pstmt.setInt(7, ram);

sc.nextLine();
System.out.println("Enter Processor_Type: ");
String pr = sc.nextLine();
pstmt.setString(8, pr);

int result = pstmt.executeUpdate();


System.out.println(result + " Record Inserted\n");
break;

case 2:
String SQL = "update mobile set name=? where mno=?";
pstmt = conn.prepareStatement(SQL);

System.out.println("Enter Model No for Update Record: ");


int no = sc.nextInt();
pstmt.setInt(2, no);

sc.nextLine();
System.out.println("Enter Updated Model name: ");
String mname = sc.nextLine();
pstmt.setString(1, mname);

int result2 = pstmt.executeUpdate();


System.out.println(result2 + " Record Updated\n");
break;

case 3:
pstmt = conn.prepareStatement("delete from mobile where mno=?");
System.out.println("Enter Model No for Delete Record: ");
int model = sc.nextInt();
pstmt.setInt(1, model);

int result3 = pstmt.executeUpdate();


System.out.println(result3 + " Record Deleted\n");
break;

case 4:
pstmt = conn.prepareStatement("select * from mobile where mno=?");
System.out.println("Enter Model No for serach Record: ");
int m = sc.nextInt();

pstmt.setInt(1, m);

rs = pstmt.executeQuery();
System.out.println("\
n------------------------------------------------------------------------------------------------------------------");
while (rs.next()) {
System.out.println(rs.getInt(1) + "\t" + rs.getString(2) + "\t" + rs.getString(3) + "\t"
+ rs.getString(4) + "\t" + rs.getInt(5) + "\t" + rs.getInt(6) + "\t" + rs.getInt(7)
+ "\t" + rs.getString(8));
}

System.out.println("----------------------------------------------------------------------------------------------------
--------------");
break;
case 5:
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from mobile");
System.out.println("\
n------------------------------------------------------------------------------------------------------------------");
while (rs.next()) {
System.out.println(rs.getInt(1) + "\t" + rs.getString(2) + "\t" + rs.getString(3) + "\t"
+ rs.getString(4) + "\t" + rs.getInt(5) + "\t" + rs.getInt(6) + "\t" + rs.getInt(7)
+ "\t" + rs.getString(8));
}

System.out.println("----------------------------------------------------------------------------------------------------
--------------");

break;

case 6:
System.exit(1);
rs.close();
stmt.close();
pstmt.close();
conn.close();
sc.close();
}

} while (true);

} // try
catch (Exception e) {
System.out.println(e);
} // catch
}// main
}// class

b) Design a following Registration form and raise an appropriate exception if invalid


information is entered like Birth Year ‘0000’

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

class InvalidBirthDateException extends Exception {

String msg = "Invalid Date Exception\n";

public String toString() {


return msg;
}

public class Cowin extends JFrame implements ActionListener {


JTextField adhar, byear, phone, hosp;
JPanel p1, p2, p3, p4;
JButton add, update, delete, view, search;
JRadioButton r1, r2, r3, r4, r5, r6, r7, r8;
ButtonGroup bg,bg1,bg2;
JComboBox hos;
String s[] = { "Tambe Hospital", "Daima Hospital", "Nighute Hospital" };

Cowin() {
setTitle("Cowin Registration");

setSize(800, 600);

setLayout(new GridLayout(8, 2, 40, 40));


setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel adharno = new JLabel("Adhar Card Number: ");


add(adharno);
adhar = new JTextField(10);
add(adhar);

JLabel Byear = new JLabel("Birth Year: ");


add(Byear);
byear = new JTextField(10);
add(byear);

JLabel phoneNo = new JLabel("Mobile Number: ");


add(phoneNo);
phone = new JTextField(10);
add(phone);

// Age Radio Button


p1 = new JPanel();
p1.setLayout(new FlowLayout());

JLabel Age = new JLabel("Age Group : ");


add(Age);

r1 = new JRadioButton("18 & above");


p1.add(r1);
// to get the value of radio button
r1.setActionCommand("18 & above");

r2 = new JRadioButton("45 & above");


r2.setActionCommand("45 & above");
p1.add(r2);
add(p1);

JLabel hospital = new JLabel("Select Hospital: ");


add(hospital);

hos = new JComboBox(s);


add(hos);

// Vaccines Radio Button


p2 = new JPanel();
p2.setLayout(new FlowLayout());

JLabel Vaccines = new JLabel("Vaccines : : ");


add(Vaccines);

r3 = new JRadioButton("Covishield");
p2.add(r3);
r3.setActionCommand("Covishield");

r4 = new JRadioButton("Covaxin");
p2.add(r4);
r4.setActionCommand("Covaxin");

r5 = new JRadioButton("Sputnik V");


p2.add(r5);
r5.setActionCommand("SputnikV");
add(p2);

// TimeSlot Radio Button


p3 = new JPanel();
p3.setLayout(new FlowLayout());

JLabel Time = new JLabel("Time Slot :: ");


add(Time);
r6 = new JRadioButton("Morning");
p3.add(r6);
r6.setActionCommand("Morning");

r7 = new JRadioButton("Afternoon");
p3.add(r7);
r7.setActionCommand("Afternoon");

r8 = new JRadioButton("Evening");
p3.add(r8);
r8.setActionCommand("Evening");

add(p3);
// Button
p4 = new JPanel();
p4.setLayout(new FlowLayout());

add = new JButton("Add");


p4.add(add);
update = new JButton("Update");
p4.add(update);
delete = new JButton("Delete");
p4.add(delete);
view = new JButton("View");
p4.add(view);
search = new JButton("Search");
p4.add(search);
add(p4);

add.addActionListener(this);

bg = new ButtonGroup();
bg.add(r1);
bg.add(r2);

bg1 = new ButtonGroup();


bg1.add(r4);
bg1.add(r3);
bg1.add(r5);

bg2 = new ButtonGroup();


bg2.add(r6);
bg2.add(r7);
bg2.add(r8);

setVisible(true);
}

public void actionPerformed(ActionEvent ae) {


if (ae.getSource() == add) {

String adharno = (adhar.getText());


int year = Integer.parseInt(byear.getText());
String phNo = (phone.getText());
String hospital = (String) (hos.getSelectedItem());
String age=bg.getSelection().getActionCommand();
String vaccine=bg1.getSelection().getActionCommand();
String timestamp=bg2.getSelection().getActionCommand();

try {
if (year == 0000) {
throw new InvalidBirthDateException();
} else {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// load a driver
Class.forName("org.postgresql.Driver");

// Establish Connection
// Use database name & password according to your
"dbname","pass"
conn =
DriverManager.getConnection("jdbc:postgresql://192.168.1.21:5432/ty90","ty90", "");
pstmt = conn.prepareStatement("insert into cowin
values(?,?,?,?,?,?,?)");

pstmt.setString(1, adharno);
pstmt.setInt(2, year);
pstmt.setString(3, phNo);
pstmt.setString(4, hospital);
pstmt.setString(5, age);
pstmt.setString(6, vaccine);
pstmt.setString(7, timestamp);

int result = pstmt.executeUpdate();


if (result == 1) {

JOptionPane.showMessageDialog(null,
"Succesfully Inserted", hospital,

JOptionPane.INFORMATION_MESSAGE);
}

pstmt.close();
conn.close();

} // try
catch (Exception e) {
JOptionPane.showMessageDialog(null, e, "ERROR
OCCURED", JOptionPane.ERROR_MESSAGE);
} // catch
}
} catch (InvalidBirthDateException e) {
JOptionPane.showMessageDialog(null, e, "ERROR OCCURED",
JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String[] args) {
new Cowin();
}// MAIN

}// CLASS

You might also like