Assignment Java
Assignment Java
Assignment Java
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.*;
Connection con;
ResultSet rs;
Statement st;
Insert iobj;
Update uobj;
Delete dobj;
ProjectDisplay() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("PROJECT INFO");
p1 = new JPanel();
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
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);
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);
} // 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);
new ProjectDisplay();
}// main
}// class
Insert() {
setTitle("Peoject Record Inserts");
setSize(400, 500);
setVisible(true);
setLayout(new GridLayout(6, 2, 40, 40));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 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);
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
}// update()
// 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);
txtpid.setText("");
txtst.setText("");
pstmt.close();
conn.close();
} // if
} // try
catch (Exception e) {
System.out.println(e);
} // catch
}// method
}// update class
Delete() {
setTitle("Peoject Record Delete");
setSize(400, 400);
setVisible(true);
setLayout(new GridLayout(3, 2, 20, 20));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}// Delete()
// 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);
} // 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.*;
// 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---------------------------------------------------------------------");
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.*;
// Establish Connection
Connection conn =
DriverManager.getConnection("jdbc:postgresql://192.168.1.21:5432/ty90","ty90", "");
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.*;
// Establish Connection
conn = DriverManager.getConnection("jdbc:postgresql://192.168.1.21:5432/ty90","ty90", "");
sc.nextLine();
System.out.println("Enter Model_Name: ");
String name = sc.nextLine();
pstmt.setString(2, name);
sc.nextLine();
System.out.println("Enter Processor_Type: ");
String pr = sc.nextLine();
pstmt.setString(8, pr);
case 2:
String SQL = "update mobile set name=? where mno=?";
pstmt = conn.prepareStatement(SQL);
sc.nextLine();
System.out.println("Enter Updated Model name: ");
String mname = sc.nextLine();
pstmt.setString(1, mname);
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);
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
Cowin() {
setTitle("Cowin Registration");
setSize(800, 600);
r3 = new JRadioButton("Covishield");
p2.add(r3);
r3.setActionCommand("Covishield");
r4 = new JRadioButton("Covaxin");
p2.add(r4);
r4.setActionCommand("Covaxin");
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.addActionListener(this);
bg = new ButtonGroup();
bg.add(r1);
bg.add(r2);
setVisible(true);
}
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);
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