📄 mainclass.java
字号:
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
public class MainClass implements ActionListener
{
static
{
JFrame.setDefaultLookAndFeelDecorated(true);
}
private static final String NUM_OF_LOGINS = "Number of remaining logins: ";
private static final String LOGIN_PANEL = "LOGIN PANEL";
JLabel loginLabel = new JLabel(LOGIN_PANEL);
JLabel loginLabel2 = new JLabel("Your password is expired!");
JLabel unLabel = new JLabel("Username");
JLabel pswLabel = new JLabel("Password");
JLabel pswLabel2 = new JLabel("Enter new password:");
JTextField userField = new JTextField("");
JTextField pswField = new JTextField("");
JButton loginButton = new JButton("Login");
JButton logoutButton = new JButton("Logout");
JButton setButton = new JButton("Set new password");
JLabel rlLabel = new JLabel(NUM_OF_LOGINS);
JLabel sdLabel = new JLabel("STUDENT DATA");
JLabel sidLabel = new JLabel("Student ID");
JLabel nameLabel = new JLabel("Name");
JLabel gpaLabel = new JLabel("GPA");
JTextField idField = new JTextField("");
JTextField nameField = new JTextField("");
JTextField gpaField = new JTextField("");
JButton fetchButton = new JButton("Fetch Record");
JButton clearButton = new JButton("Clear search fields");
JButton updateButton = new JButton("Update Record");
JPanel pan = new JPanel();
JFrame frame = new JFrame("Login test");
boolean isLoggegOn = false;
UserRecord lastUser = null;
int lastStudent = 0;
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static GridBagConstraints getGBC(GridBagConstraints c, int cell_x, int cell_y, int cells_w, int cells_h)
{
c.gridx = cell_x;
c.gridy = cell_y;
c.gridwidth = cells_w;
c.gridheight = cells_h;
c.weightx=5;
return c;
}
private void setLoginMode()
{
loginLabel.setVisible(true);
loginLabel2.setVisible(false);
unLabel.setVisible(true);
userField.setVisible(true);
pswLabel.setVisible(true);
pswLabel2.setVisible(false);
loginButton.setVisible(true);
setButton.setVisible(false);
}
private void setNewPswMode()
{
loginLabel.setVisible(false);
loginLabel2.setVisible(true);
unLabel.setVisible(false);
userField.setVisible(false);
pswLabel.setVisible(false);
pswLabel2.setVisible(true);
loginButton.setVisible(false);
setButton.setVisible(true);
}
private void setLoggedOn()
{
isLoggegOn=true;
loginLabel.setText(LOGIN_PANEL+" (Logged in)");
loginButton.setVisible(false);
logoutButton.setVisible(true);
}
private void setLoggedOut()
{
isLoggegOn=false;
loginLabel.setText(LOGIN_PANEL);
userField.setText("");
pswField.setText("");
loginButton.setVisible(true);
logoutButton.setVisible(false);
rlLabel.setText(NUM_OF_LOGINS);
}
public void login()
{
DerbyDriver drv = new DerbyDriver();
Connection conn = null;
try
{
conn = openConnection();
String name = userField.getText();
String psw = pswField.getText();
UserRecord sr = new UserRecord(name, null, -1);
java.util.List res = drv.getUserRecords(conn, sr);
if(res.size()>0)
{
sr = (UserRecord)res.get(0);
if(!sr.getPassword().equals(psw))
{
JOptionPane.showMessageDialog(frame,"Invalid password.");
return;
}
sr = (UserRecord)res.get(0);
rlLabel.setText(NUM_OF_LOGINS+sr.getLoginAtt());
if(sr.getLoginAtt()==0)
{
lastUser = sr;
setNewPswMode();
return;
}
else
{
UserRecord newSr = new UserRecord(sr.getName(), sr.getPassword(), sr.getLoginAtt()-1);
rlLabel.setText(NUM_OF_LOGINS+(sr.getLoginAtt()-1));
drv.updateUserRecord(conn, sr.getName(), newSr);
lastUser = newSr;
setLoggedOn();
}
}
else
{
JOptionPane.showMessageDialog(frame,"Invalid username.");
return;
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if(conn!=null)
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
private void clearStudFields()
{
idField.setText("");
nameField.setText("");
gpaField.setText("");
}
public void fetch()
{
if(!isLoggegOn)
{
clearStudFields();
return;
}
String sid = idField.getText();
String name = nameField.getText();
String sgpa = gpaField.getText();
if(sid.length()+name.length()+sgpa.length()==0)
{
JOptionPane.showMessageDialog(frame,"Please enter any data for record fetching!");
return;
}
float grade = -1;
int id=-1;
if(sid.length()>0)
{
try
{
id = Integer.parseInt(sid);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(frame,"ID must be a number!");
return;
}
}
if(sgpa.length()>0)
{
try
{
grade = Float.parseFloat(sgpa);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(frame,"GPA must be a float number!");
return;
}
}
DerbyDriver drv = new DerbyDriver();
Connection conn = null;
try
{
conn = openConnection();
StudentRecord srec = null;
srec = new StudentRecord(id, name, grade);
java.util.List res = drv.getStudentRecords(conn, id, srec);
if(res!=null)
if(res.size()>0)
{
StudentRecord sr = (StudentRecord)res.get(0);
nameField.setText(sr.getName());
idField.setText(String.valueOf(sr.getId()));
gpaField.setText(String.valueOf(sr.getGrade()));
lastStudent = sr.getId();
return;
}
StringBuffer sb = new StringBuffer("There is no student record with ");
if(id>-1)
sb.append("ID=").append(id).append(" ");
if(name.length()>0)
sb.append("Name='").append(name).append("' ");
if(sgpa.length()>0)
sb.append("GPA=").append(sgpa).append(" ");
JOptionPane.showMessageDialog(frame,sb.toString());
return;
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if(conn!=null)
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
public void update()
{
String sid = idField.getText();
String name = nameField.getText();
String sgpa = gpaField.getText();
int id=-1;
float grade = -1;
if(sid.length()>0)
{
try
{
id = Integer.parseInt(sid);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(frame,"ID must be a number!");
return;
}
}
else
{
JOptionPane.showMessageDialog(frame,"You must enter ID of record to be updated!");
return;
}
if(sgpa.length()>0)
{
try
{
grade = Float.parseFloat(sgpa);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -