📄 exp3_company.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.JTable;
import java.util.*;
class AdministratorFrame extends JFrame implements ActionListener
{
JMenuItem login;
JMenuItem viewProject;
JMenuItem viewDepartment;
Container container;
AdministratorFrame(String title)
{
super(title);
container=getContentPane();
container.setLayout(new BorderLayout());
JMenuBar menubar=new JMenuBar();
setJMenuBar(menubar);
container.add(menubar,BorderLayout.NORTH);
JMenu administrator=new JMenu("Administrator");
login=new JMenuItem("Login");
JMenu viewdetails=new JMenu("View");
viewProject=new JMenuItem("ViewProject");
viewDepartment=new JMenuItem("ViewDepartment");
viewdetails.add(viewDepartment);
viewdetails.add(viewProject);
administrator.add(login);
menubar.add(administrator);
menubar.add(viewdetails);
viewDepartment.addActionListener(this);
viewProject.addActionListener(this);
login.addActionListener(this);
setSize(250,250);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
if((e.getActionCommand()).equals("Login"))
{
container=getContentPane();
container.add(new loginpanel(),BorderLayout.SOUTH);
}
else if((e.getActionCommand()).equals("Project"))
{
container=getContentPane();
container.add(new projectpanel(),BorderLayout.EAST);
}
else if((e.getActionCommand()).equals("ViewDepartment"))
{
container=getContentPane();
container.add(new viewdepartmentpanel(),BorderLayout.WEST);
}
else if((e.getActionCommand()).equals("ViewProject"))
{
container=getContentPane();
container.add(new projectpanel(),BorderLayout.WEST);
}
}
}
class loginpanel extends Panel implements ActionListener
{
Statement stmt;
Connection cn;
ResultSet rs;
JTextField txtusername;
JPasswordField txtpassword;
JLabel companyName;
JLabel companyAddress;
JLabel companyPhoneNo;
JLabel username;
JLabel password;
JButton btlogin;
JButton btexit;
loginpanel()
{
setLayout(new GridLayout(9,1));
companyName=new JLabel("CompanyName: ALCATEL");
companyAddress=new JLabel("Address : TidelPark,Chennai");
companyPhoneNo=new JLabel("PhoneNo : 044-23467589");
companyName.setBounds( 10, 30, 150, 20 );
username= new JLabel( "Username:" );
username.setBounds( 10, 15, 150, 20 );
txtusername= new JTextField("",20);
txtusername.setBounds( 10, 35, 150, 20 );
password= new JLabel( "Password:" );
password.setBounds( 10, 60, 150, 20 );
txtpassword = new JPasswordField("",20);
txtpassword.setBounds( 10, 80, 150, 20 );
btlogin=new JButton("Verify");
btexit=new JButton("Exit");
add(companyName);
add(companyAddress);
add(companyPhoneNo);
add(username);
add(txtusername);
add(password);
add(txtpassword);
add(btlogin);
add(btexit);
btlogin.addActionListener(this);
btexit.addActionListener(this);
}
public void actionPerformed(ActionEvent everify)
{
if((everify.getActionCommand()).equals("Verify"))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String gs="jdbc:odbc:companydb";
Connection cn=DriverManager.getConnection(gs," "," ");
String qs="select * from Exp4_Login";
Statement stmt=cn.createStatement();
ResultSet rs=stmt.executeQuery(qs);
String usernamestr=txtusername.getText();
String passwordstr=txtpassword.getText();
boolean more=rs.next();
while(more)
{
if((rs.getString("Password")).equals(passwordstr) && (rs.getString("UserName")).equals(usernamestr))
{
JFrame.setDefaultLookAndFeelDecorated(true);
AddDepartmentFrame adddept=new AddDepartmentFrame("AddFrame");
}
else
{
JOptionPane.showMessageDialog(this, "Incorrect Login Details", "Login", JOptionPane.ERROR_MESSAGE);
}
more=rs.next();
}
}
catch(java.lang.Exception ex)
{
System.out.println("error");
ex.printStackTrace();
}
}
else if((everify.getActionCommand()).equals("Exit"))
{
setVisible(false);
}
}
}
class AddDepartmentFrame extends JFrame implements ActionListener
{
JMenuItem newDepartment;
JMenuItem newStaff;
Container container;
AddDepartmentFrame(String title)
{
super(title);
container=getContentPane();
container.setLayout(new BorderLayout());
JMenuBar menubar=new JMenuBar();
setJMenuBar(menubar);
container.add(menubar,BorderLayout.NORTH);
JMenu adddetails=new JMenu("Add");
newDepartment=new JMenuItem("Department");
newStaff=new JMenuItem("Staff");
adddetails.add(newDepartment);
adddetails.add(newStaff);
menubar.add(adddetails);
newDepartment.addActionListener(this);
newStaff.addActionListener(this);
setSize(250,250);
setVisible(true);
}
public void actionPerformed(ActionEvent Eadddept)
{
if((Eadddept.getActionCommand()).equals("Department"))
{
container.add(new departmentpanel(),BorderLayout.CENTER);
}
else if((Eadddept.getActionCommand()).equals("Staff"))
{
container.add(new staffpanel(),BorderLayout.CENTER);
}
}
}
class departmentpanel extends Panel implements ActionListener
{
Statement stmt;
Connection cn;
ResultSet rs;
JTextField txtdepartmentname;
JTextField txtdepartmentcode;
departmentpanel()
{
JLabel departmentName=new JLabel("DepartmentName:");
departmentName.setBounds( 10, 15, 150, 20 );
JLabel departmentCode=new JLabel("DepartmentCode:");
departmentCode.setBounds( 10, 35, 150, 20 );
txtdepartmentname= new JTextField("",20);
txtdepartmentname.setBounds( 10, 60, 150, 20 );
txtdepartmentcode= new JTextField("",20);
txtdepartmentcode.setBounds( 10, 80, 150, 20 );
JButton btdept=new JButton("Submit");
JButton btexit=new JButton("Exit");
add(departmentName);
add(txtdepartmentname);
add(departmentCode);
add(txtdepartmentcode);
add(btdept);
add(btexit);
btdept.addActionListener(this);
btexit.addActionListener(this);
}
public void actionPerformed(ActionEvent Eadddeptpanel)
{
if((Eadddeptpanel.getActionCommand()).equals("Submit"))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String gs="jdbc:odbc:companydb";
Connection cn=DriverManager.getConnection(gs," "," ");
String insertquery="INSERT INTO Exp4_Departments VALUES ('"+txtdepartmentname.getText()+"','"+txtdepartmentcode.getText()+"')";
Statement stmt=cn.createStatement();
stmt.executeUpdate(insertquery);
}
catch(java.lang.Exception ex)
{
System.out.println("error");
ex.printStackTrace();
}
}
else if((Eadddeptpanel.getActionCommand()).equals("Exit"))
{
setVisible(false);
}
}
}
class staffpanel extends Panel implements ActionListener
{
JTextField txtstaffName, txtstaffQuali, txtstaffDesg, txtstaffProj, txtdepartmentName, txtdepartmentCode;
Statement stmt;
Connection cn;
ResultSet rs;
staffpanel()
{
setLayout(new GridLayout(11,1));
JLabel staffName=new JLabel("StaffName:");
staffName.setBounds( 10, 15, 150, 20 );
JLabel staffQuali=new JLabel("Qualification:");
staffQuali.setBounds( 10, 25, 150, 20 );
JLabel staffDesg=new JLabel("Designation:");
staffDesg.setBounds( 10, 35, 150, 20 );
JLabel staffProj=new JLabel("Project:");
staffDesg.setBounds( 10, 45, 150, 20 );
JLabel departmentName=new JLabel("DepartmentName:");
departmentName.setBounds( 10, 55, 150, 20 );
txtstaffName= new JTextField("",20);
txtstaffQuali= new JTextField("",20);
txtstaffDesg= new JTextField("",20);
txtstaffProj= new JTextField("",20);
txtdepartmentName= new JTextField("",20);
JButton btstaff=new JButton("Submit");
JButton btexit=new JButton("Exit");
add(staffName);
add(txtstaffName);
add(staffQuali);
add(txtstaffQuali);
add(staffDesg);
add(txtstaffDesg);
add(departmentName);
add(txtdepartmentName);
add(staffProj);
add(txtstaffProj);
add(btstaff);
add(btexit);
btstaff.addActionListener(this);
btexit.addActionListener(this);
}
public void actionPerformed(ActionEvent Eaddstaffpanel)
{
if((Eaddstaffpanel.getActionCommand()).equals("Submit"))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String gs="jdbc:odbc:companydb";
Connection cn=DriverManager.getConnection(gs," "," ");
String insertquery="INSERT INTO Exp4_Staff VALUES ('"+txtstaffName.getText()+"','"+txtstaffQuali.getText()+"','"+txtstaffDesg.getText()+"','"+txtstaffProj.getText()+"','"+txtdepartmentName.getText()+"')";
Statement stmt=cn.createStatement();
stmt.executeUpdate(insertquery);
}
catch(java.lang.Exception ex)
{
System.out.println("error");
ex.printStackTrace();
}
}
else if((Eaddstaffpanel.getActionCommand()).equals("Exit"))
{
setVisible(false);
}
}
}
class projectpanel extends Panel
{
Statement stmt;
Connection cn;
ResultSet rs;
JTextField txtprojectname;
JTable table;
Container c;
JTextArea textarea;
projectpanel()
{
setLayout(new GridLayout(4,1));
JLabel projectname=new JLabel("ProjectName");
projectname.setBounds( 10, 15, 150, 20 );
txtprojectname=new JTextField("",20);
txtprojectname.setBounds( 10, 25, 150, 20 );
JButton btview=new JButton("ViewTable");
btview.setBounds(10, 30, 150, 20);
JButton btexit=new JButton("Exit");
btexit.setBounds(10, 40, 150, 20);
add(projectname);
add(txtprojectname);
add(btview);
add(btexit);
btview.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent Eviewevent)
{
if(Eviewevent.getActionCommand()=="ViewTable")
getTable();
}
}
);
btexit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent Eviewevent)
{
if(Eviewevent.getActionCommand()=="Exit")
setVisible(false);
}
}
);
}
private void getTable()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String gs="jdbc:odbc:companydb";
Connection cn=DriverManager.getConnection(gs," "," ");
Statement stmt=cn.createStatement();
String proname=txtprojectname.getText();
String qs=" select * from Exp4_Staff where ProjectName='"+proname+"'";
ResultSet rs=stmt.executeQuery(qs);
boolean more=rs.next();
if(!more)
{
JOptionPane.showMessageDialog(this, "No Records", "Information Relating Project", JOptionPane.INFORMATION_MESSAGE);
return;
}
Vector columnheads=new Vector();
Vector rows=new Vector();
ResultSetMetaData metadata=rs.getMetaData();
int numberofcolumns=metadata.getColumnCount();
System.out.println(numberofcolumns);
for(int i=1;i<=numberofcolumns;i++)
columnheads.addElement(metadata.getColumnName(i));
do
{
rows.addElement(getNextRow(rs,metadata));
}while(rs.next());
JTable table=new JTable(rows,columnheads);
add(table);
JScrollPane scroller=new JScrollPane(table);
add(scroller);
}
catch(java.lang.Exception ex)
{
System.out.println("error");
ex.printStackTrace();
}
}
private Vector getNextRow(ResultSet rs,ResultSetMetaData metadata)throws SQLException
{
Vector currentRow=new Vector();
for(int i=1;i<=metadata.getColumnCount();i++)
switch(metadata.getColumnType(i))
{
case Types.VARCHAR:
currentRow.addElement(rs.getString(i));
break;
case Types.INTEGER:
currentRow.addElement(new Long(rs.getLong(i)));
break;
default:
System.out.println("Type mismatch");
}
return currentRow;
}
}
class viewdepartmentpanel extends Panel
{
Statement stmt;
Connection cn;
ResultSet rs;
JTextField txtprojectname;
JTable table;
Container c;
JTextArea textarea;
viewdepartmentpanel()
{
setLayout(new GridLayout(5,1));
JLabel projectname=new JLabel("DepartmentName");
projectname.setBounds( 10, 15, 150, 20 );
txtprojectname=new JTextField("",20);
txtprojectname.setBounds( 10, 25, 150, 20 );
JButton btview=new JButton("ViewTable");
JButton btexit=new JButton("Exit");
add(projectname);
add(txtprojectname);
add(btview);
add(btexit);
btview.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent Eviewevent)
{
if(Eviewevent.getActionCommand()=="ViewTable")
getTable();
}
}
);
btexit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent Eviewevent)
{
if(Eviewevent.getActionCommand()=="Exit")
setVisible(false);
}
}
);
}
private void getTable()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String gs="jdbc:odbc:companydb";
Connection cn=DriverManager.getConnection(gs," "," ");
Statement stmt=cn.createStatement();
String proname=txtprojectname.getText();
String qs=" select * from Exp4_Staff where DeptName='"+proname+"'";
ResultSet rs=stmt.executeQuery(qs);
boolean more=rs.next();
if(!more)
{
JOptionPane.showMessageDialog(this, "No Records", "Information relating Department", JOptionPane.INFORMATION_MESSAGE);
return;
}
Vector columnheads=new Vector();
Vector rows=new Vector();
ResultSetMetaData metadata=rs.getMetaData();
int numberofcolumns=metadata.getColumnCount();
System.out.println(numberofcolumns);
for(int i=1;i<=numberofcolumns;i++)
columnheads.addElement(metadata.getColumnName(i));
do
{
rows.addElement(getNextRow(rs,metadata));
}while(rs.next());
JTable table=new JTable(rows,columnheads);
add(table);
JScrollPane scroller=new JScrollPane(table);
add(scroller);
}
catch(java.lang.Exception ex)
{
System.out.println("error");
ex.printStackTrace();
}
}
private Vector getNextRow(ResultSet rs,ResultSetMetaData metadata)throws SQLException
{
Vector currentRow=new Vector();
for(int i=1;i<=metadata.getColumnCount();i++)
switch(metadata.getColumnType(i))
{
case Types.VARCHAR:
currentRow.addElement(rs.getString(i));
break;
case Types.INTEGER:
currentRow.addElement(new Long(rs.getLong(i)));
break;
default:
System.out.println("Type mismatch");
}
return currentRow;
}
}
class Exp3_Company
{
public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
new AdministratorFrame("ALCATEL");
}
}
/*
username.setVisible(false);
txtusername.setVisible(false);
password.setVisible(false);
txtpassword.setVisible(false);
companyName.setVisible(false);
companyAddress.setVisible(false);
companyPhoneNo.setVisible(false);
btlogin.setVisible(false);
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -