📄 mainclass.java
字号:
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(frame,"GPA must be a float number!");
return;
}
}
if(name.length()==0)
{
JOptionPane.showMessageDialog(frame,"Please enter the name!");
return;
}
DerbyDriver drv = new DerbyDriver();
Connection conn = null;
try
{
StudentRecord sr = new StudentRecord(id, name, grade);
conn = openConnection();
int num = drv.updateStudentRecord(conn, id, sr);
if(num==0)
JOptionPane.showMessageDialog(frame,"Can't find record for updating with ID="+sid+"!");
else
JOptionPane.showMessageDialog(frame,"Record with ID="+sid+" updated successfully!");
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if(conn!=null)
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
private boolean isNum(char chr)
{
try
{
StringBuffer sb = new StringBuffer();
sb.append(chr);
Integer.parseInt(sb.toString());
}
catch(Exception ex)
{
return false;
}
return true;
}
public void setPsw()
{
System.out.println("Set new password");
String psw = pswField.getText();
int letters = 0;
int nums = 0;
for(int i=0; i<psw.length(); i++)
if(isNum(psw.charAt(i)))
nums++;
else
letters++;
if(nums<3)
{
JOptionPane.showMessageDialog(frame, "You password must contain at least 3 digits!");
return;
}
if(letters<5)
{
JOptionPane.showMessageDialog(frame, "You password must contain at least 5 letters!");
return;
}
String oldPsw = lastUser.getPassword();
int differs=0;
int len = Math.min(oldPsw.length(), psw.length());
for(int i=0; i<len; i++)
{
if(psw.charAt(i)!=oldPsw.charAt(i))
differs++;
}
if(differs<2)
{
JOptionPane.showMessageDialog(frame, "Old and new password must differ more than 1 characters!");
return;
}
Connection conn = null;
try
{
DerbyDriver drv = new DerbyDriver();
conn = openConnection();
UserRecord newData = new UserRecord(lastUser.getName(), psw, 3);
drv.updateUserRecord(conn, lastUser.getName(), newData);
userField.setText("");
pswField.setText("");
setLoginMode();
setLoggedOut();
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if(conn!=null)
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if("login".equals(cmd))
login();
if("fetch".equals(cmd))
if(isLoggegOn)
{
fetch();
}
else
{
JOptionPane.showMessageDialog(frame,"You are not logged in!");
}
if("clear".equals(cmd))
clearStudFields();
if("logout".equals(cmd))
if(isLoggegOn)
{
setLoggedOut();
}
else
{
JOptionPane.showMessageDialog(frame,"You are not logged in!");
}
if("update".equals(cmd))
if(isLoggegOn)
{
update();
}
else
{
JOptionPane.showMessageDialog(frame,"You are not logged in!");
}
if("set_psw".equals(cmd))
setPsw();
}
private void createAndShowGUI()
{
Container cnt = frame.getContentPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cnt.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
userField.setSize(100,20);
userField.setLocation(100,100);
pswField.setSize(100,20);
pswField.setLocation(100,150);
cnt.add(loginLabel, getGBC(c,1,0,1,1));
cnt.add(loginLabel2, getGBC(c,1,0,1,1));
cnt.add(unLabel, getGBC(c,0,1,1,1));
cnt.add(userField, getGBC(c,0,2,3,1));
cnt.add(pswLabel, getGBC(c,0,3,1,1));
cnt.add(pswLabel2, getGBC(c,0,3,1,1));
cnt.add(pswField, getGBC(c,0,4,3,1));
cnt.add(loginButton,getGBC(c,1,5,1,1));
cnt.add(logoutButton,getGBC(c,1,5,1,1));
cnt.add(setButton,getGBC(c,1,5,1,1));
cnt.add(rlLabel,getGBC(c,0,6,3,1));
pan.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
c.fill = GridBagConstraints.VERTICAL;
cnt.add(pan, getGBC(c,3,0,1,8));
c.fill = GridBagConstraints.HORIZONTAL;
cnt.add(sdLabel, getGBC(c,5,0,2,1));
cnt.add(sidLabel, getGBC(c,4,2,1,1));
cnt.add(idField, getGBC(c,5,2,2,1));
cnt.add(fetchButton, getGBC(c,7,2,1,1));
cnt.add(clearButton, getGBC(c,7,3,1,1));
cnt.add(nameLabel, getGBC(c,4,4,1,1));
cnt.add(nameField, getGBC(c,5,4,2,1));
cnt.add(gpaLabel, getGBC(c,4,5,1,1));
cnt.add(gpaField, getGBC(c,5,5,2,1));
cnt.add(updateButton, getGBC(c,7,5,1,1));
frame.setBounds(0,0,600,200);
//Display the window.
//frame.pack();
frame.setVisible(true);
clearButton.setActionCommand("clear");
clearButton.addActionListener(this);
logoutButton.setActionCommand("logout");
logoutButton.addActionListener(this);
loginButton.setActionCommand("login");
loginButton.addActionListener(this);
fetchButton.setActionCommand("fetch");
fetchButton.addActionListener(this);
updateButton.setActionCommand("update");
updateButton.addActionListener(this);
setButton.setActionCommand("set_psw");
setButton.addActionListener(this);
setLoginMode();
setLoggedOut();
}
public Connection openConnection()
{
Connection conn = null;
try
{
DerbyDriver drv = new DerbyDriver();
conn = drv.getConnection(DerbyDriver.DB_DRIVER, DerbyDriver.DB_NAME);
if(conn==null)
throw new Exception("Error creating DB connection!");
}
catch (Exception ex)
{
ex.printStackTrace();
}
return conn;
}
public void testDB()
{
Connection conn = null;
try
{
DerbyDriver drv = new DerbyDriver();
conn = openConnection();
drv.printDB(conn);
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if(conn!=null)
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
public static void main(String[] args)
{
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
MainClass mc = new MainClass();
mc.testDB();
mc.createAndShowGUI();
}
}
);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -