Code
Code
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
//Main Window
//View Window
//Update WIndow
//Remove Window
JTable datatable;
JScrollPane viewdata;
mainpanel.setBackground(Color.PINK);
mainpanel.setLayout(null);
mainpanel.add(welcomelabel);
mainpanel.add(welcominglabel);
mainpanel.add(insertbtn);
mainpanel.add(updatebtn);
mainpanel.add(removebtn);
mainpanel.add(viewbtn);
mainpanel.add(cplabel);
mainframe.add(mainpanel);
mainframe.setSize(900,550);
mainframe.setVisible(true);
insertbtn.addActionListener(this);
updatebtn.addActionListener(this);
removebtn.addActionListener(this);
viewbtn.addActionListener(this);
insertpanel.setBackground(Color.PINK);
insertstudentidlabel.setForeground(Color.BLACK);
insertfirstnamelabel.setForeground(Color.BLACK);
insertlastnamelabel.setForeground(Color.BLACK);
insertmiddlenamelabel.setForeground(Color.BLACK);
insertagelabel.setForeground(Color.BLACK);
insertpanel.setLayout(null);
insertpanel.add(insertinstructionlabel);
insertpanel.add(insertstudentidlabel);
insertpanel.add(insertstudentidtext);
insertpanel.add(insertfirstnamelabel);
insertpanel.add(insertfirstnametext);
insertpanel.add(insertmiddlenamelabel);
insertpanel.add(insertmiddlenametext);
insertpanel.add(insertlastnamelabel);
insertpanel.add(insertlastnametext);
insertpanel.add(insertagelabel);
insertpanel.add(insertagetext);
insertpanel.add(insertok);
insertframe.add(insertpanel);
insertframe.setSize(900,550);
insertframe.setVisible(true);
insertok.addActionListener(this);
insertframe.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
mainframe.setVisible(true);
});
datacol.removeAllElements();
datarow.removeAllElements();
OpenDatabase();
try{
Statement st = con.createStatement();
for(int i=1;i<=cols;i++){
datacol.addElement(rms.getColumnName(i));
while(rs.next()){
for(int i=1;i<=cols;i++){
row.addElement(rs.getObject(i));
datarow.addElement(row);
catch(Exception e){
JOptionPane.showConfirmDialog(null, "Problem in Database connectivity or Data","Result",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
viewpanel.setLayout(null);
viewpanel.add(viewdata);
viewframe.add(viewpanel);
viewframe.setSize(900,500);
viewframe.setVisible(true);
viewframe.addWindowListener(new WindowAdapter(){
mainframe.setVisible(true);
});
ufirstnametext.disable();
umiddlenametext.disable();
ulastnametext.disable();
uagetext.disable();
ufirstnametext.setText("");
umiddlenametext.setText("");
ulastnametext.setText("");
uagetext.setText("");
updatepanel.setBackground(Color.PINK);
uinstructionlabel.setForeground(Color.BLACK);
ustudentidlabel.setForeground(Color.BLACK);
ufirstnamelabel.setForeground(Color.BLACK);
umiddlenamelabel.setForeground(Color.BLACK);
ulastnamelabel.setForeground(Color.BLACK);
uagelabel.setForeground(Color.BLACK);
updatepanel.setLayout(null);
updatepanel.add(uinstructionlabel);
updatepanel.add(ustudentidlabel);
updatepanel.add(ustudentidtext);
updatepanel.add(ufirstnamelabel);
updatepanel.add(ufirstnametext);
updatepanel.add(umiddlenamelabel);
updatepanel.add(umiddlenametext);
updatepanel.add(ulastnamelabel);
updatepanel.add(ulastnametext);
updatepanel.add(uagelabel);
updatepanel.add(uagetext);
updatepanel.add(updateok);
updateframe.add(updatepanel);
updateframe.setSize(900,550);
updateframe.setVisible(true);
updateok.addActionListener(this);
updateframe.addWindowListener(new WindowAdapter(){
mainframe.setVisible(true);
});
ustudentidtext.addActionListener(this);
removepanel.setBackground(Color.PINK);
rinstructionlabel.setForeground(Color.BLACK);
removestudentidlabel.setForeground(Color.BLACK);
removepanel.setLayout(null);
rinstructionlabel.setFont(new java.awt.Font("Times New Roman", 0, 30));
removepanel.add(rinstructionlabel);
removepanel.add(removestudentidlabel);
removepanel.add(removestudentidtext);
removepanel.add(removeok);
removeframe.add(removepanel);
removeframe.setSize(900,550);
removeframe.setVisible(true);
removeok.addActionListener(this);
removestudentidtext.addActionListener(this);
removeframe.addWindowListener(new WindowAdapter(){
mainframe.setVisible(true);
});
public void insertdata(int sid, String sfname, String smname, String slname, int sage){
try{
Statement st = con.createStatement();
}catch(Exception e){
try{
Statement st = con.createStatement();
}catch(Exception e){
public void updatedata(int sid, String sfname, String smname, String slname, int sage){
try{
Statement st = con.createStatement();
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(Exception e){
System.out.println(e);
try{
con.close();
}catch(SQLException e){
e.printStackTrace();
@Override
if(arg0.getSource()==insertbtn){
mainframe.setVisible(false);
insertwindow();
if(arg0.getSource()==viewbtn){
mainframe.setVisible(false);
viewwindow();
if(arg0.getSource()==updatebtn){
mainframe.setVisible(false);
updatewindow();
if(arg0.getSource()==removebtn){
mainframe.setVisible(false);
removewindow();
if(arg0.getSource()==insertok){
try{
OpenDatabase();
CloseDatabase();
}catch(Exception e){
System.out.println(e);
insertstudentidtext.setText("");
insertfirstnametext.setText("");
insertmiddlenametext.setText("");
insertlastnametext.setText("");
insertagetext.setText("");
}
if(arg0.getSource()==updateok){
OpenDatabase();
CloseDatabase();
if(arg0.getSource()==ustudentidtext){
try{
OpenDatabase();
Statement st = con.createStatement();
if(rs.next()){
ufirstnametext.setText(rs.getString("firstname"));
umiddlenametext.setText(rs.getString("middlename"));
ulastnametext.setText(rs.getString("lastname"));
uagetext.setText(String.valueOf(rs.getInt("age")));
CloseDatabase();
ustudentidtext.disable();
ufirstnametext.enable();
umiddlenametext.enable();
ulastnametext.enable();
uagetext.enable();
}catch(Exception e){
if(arg0.getSource()==removeok){
OpenDatabase();
removedata(sid);
CloseDatabase();
sl.run();