最近课程设计让做一个图书管理系统,别的不多说,直接上源码吧!
首先是表结构:
用户表:
列名 |
类型 |
说明 |
userid |
bigint |
用户ID(主键) |
useraccount |
nvarchar(MAX) |
用户名 |
userpwd |
nvarchar(MAX) |
用户密码 |
username |
nvarchar(MAX) |
姓名 |
usersex |
nvarchar(MAX) |
性别 |
userbirth |
datetime |
出生日期 |
userage |
nvarchar(MAX) |
用户年龄 |
usernote |
nvarchar(MAX) |
用户备注 |
userregisterdate |
datetime |
用户注册日期 |
userlogdate |
datetime |
用户最后登录日期 |
usercdt |
int |
用户信誉度 |
usertype |
nvarchar(MAX) |
用户类型 |
图书表:
列名 |
类型 |
说明 |
bookid |
bigint |
图书ID(主键) |
bookname |
nvarchar(MAX) |
图书名称 |
isbn |
nvarchar(MAX) |
图书编号 |
author |
nvarchar(MAX) |
作者 |
press |
nvarchar(MAX) |
出版社 |
pressdate |
datetime |
出版日期 |
bookstock |
int |
库存量 |
borrowtimes |
int |
借阅次数 |
借阅表:
列名 |
类型 |
说明 |
userid |
bigint |
用户ID(主键) |
bookid |
bigint |
图书ID(主键) |
borrowstime |
datetime |
借阅开始时间 |
borrowetime |
datetime |
借阅到期时间 |
borroweddays |
int |
已经借阅天数 |
申请表:
列名 |
类型 |
说明 |
userid |
bigint |
用户ID(主键) |
bookid |
bigint |
图书ID(主键) |
applytime |
datetime |
申请时间 |
applyeddays |
int |
申请天数 |
/*登录模块*/
package Main;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.*;
import javax.swing.*;
public class LoginModule implements ActionListener
{
JFrame frm = new JFrame();
JTextField login = new JTextField();
JPasswordField pwd = new JPasswordField();
JPanel loglabmessage = new JPanel();
JPanel logmessage = new JPanel();
JPanel labelmessage = new JPanel();
JPanel buttonmessage = new JPanel();
JPanel layout = new JPanel();
JPanel tc = new JPanel();
JPanel tc1 = new JPanel();
Label loginLabel = new Label("用户名:");
Label pwdLabel = new Label("密 码:");
JButton loginButton;
JButton registerButton;
JButton exitButton;
public LoginModule()
{
frm.setTitle("XXXXXX图书管理系统");
login.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER)
{
loginButton.doClick();
}
}
public void keyReleased(KeyEvent args)
{
}
public void keyTyped(KeyEvent args)
{
}
});
pwd.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER)
{
loginButton.doClick();
}
}
public void keyReleased(KeyEvent args)
{
}
public void keyTyped(KeyEvent args)
{
}
});
setBak();
Container c = frm.getContentPane();
JPanel jp = new JPanel();
jp.setOpaque(false);
c.add(jp);
frm.setSize(1000, 600);
frm.setLayout(new BorderLayout());
layout.setOpaque(false);
layout.setLayout(new GridLayout(1, 3));
tc.setOpaque(false);
tc1.setOpaque(false);
logmessage.setOpaque(false);
logmessage.setSize(200, 400);
logmessage.setLayout(new GridLayout(2, 2));
logmessage.setLayout(new GridLayout(2, 1));
labelmessage.setLayout(new GridLayout(2, 1));
buttonmessage.setLayout(new GridLayout(1, 3));
labelmessage.add(loginLabel);
logmessage.add(login);
labelmessage.add(pwdLabel);
logmessage.add(pwd);
loginButton = new JButton("登录");
registerButton = new JButton("注册");
exitButton = new JButton("退出系统");
loginButton.addActionListener(this);
registerButton.addActionListener(this);
exitButton.addActionListener(this);
buttonmessage.add(loginButton);
buttonmessage.add(registerButton);
buttonmessage.add(exitButton);
loglabmessage.setLayout(new BorderLayout());
loglabmessage.add(labelmessage, BorderLayout.WEST);
loglabmessage.add(logmessage, BorderLayout.CENTER);
loglabmessage.add(buttonmessage, BorderLayout.SOUTH);
layout.add(tc);
layout.add(tc1);
layout.add(loglabmessage);
frm.add(layout, BorderLayout.SOUTH);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension fra = frm.getSize();
frm.setLocation((screenSize.width - fra.width) / 2, (screenSize.height - fra.height) / 2);
frm.setResizable(false);
frm.setUndecorated(true);
login.requestFocus();
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void setBak()
{
((JPanel) frm.getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon(LoginModule.class.getResource("/img/lib.jpg"));
JLabel background = new JLabel(img);
frm.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("登录"))
{
int i;
i = 0;
String account = login.getText().trim();
@SuppressWarnings("deprecation")
String password = pwd.getText().trim();
String pwdsqlString = new String();
String date = new String();
String usertype = "普通会员";
String useraccount = "";
if (account.compareTo("") == 0 || password.compareTo("") == 0)
{
JOptionPane.showMessageDialog(null, "用户名或密码不能为空", "提示", JOptionPane.WARNING_MESSAGE);
login.setText("");
pwd.setText("");
login.requestFocus();
return;
}
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select useraccount,userpwd,usertype from usertable where useraccount='" + account + "'");
while (rs.next())
{
pwdsqlString = rs.getString("userpwd");
usertype = rs.getString("usertype");
useraccount = rs.getString("useraccount");
i++;
}
if (password.compareTo(pwdsqlString) == 0)
{
rs = stmt.executeQuery("select getdate()");
rs.next();
date = rs.getString(1);
stmt.execute("update usertable set userlogdate='" + date + "' where useraccount='" + account + "'");
frm.dispose();
if (usertype.equals("超级管理员"))
new UserManageModule(useraccount, usertype);
if (usertype.equals("图书管理员"))
new BookManageModule(useraccount, usertype);
if (usertype.equals("借阅管理员"))
new BookBorrowModule(useraccount, usertype);
if (usertype.equals("普通会员"))
new InquiryModule(useraccount, usertype);
} else if (i == 0)
{
JOptionPane.showMessageDialog(null, "用户名不存在,请您修改用户名或注册!", "提示", JOptionPane.WARNING_MESSAGE);
login.setText("");
pwd.setText("");
login.requestFocus();
} else if (i != 0)
{
JOptionPane.showMessageDialog(null, "用户名和密码不匹配,请重新输入!", "提示", JOptionPane.WARNING_MESSAGE);
login.setText("");
pwd.setText("");
login.requestFocus();
i = 0;
}
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
} else if (s.equals("注册"))
{
new RegisterModule();
} else if (s.equals("退出系统"))
{
System.exit(0);
}
}
public static void main(String[] args)
{
new LoginModule();
}
}
/*用户管理模块*/
package Main;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.JOptionPane;
public class UserManageModule extends MouseAdapter implements ActionListener
{
JFrame f = new JFrame("用户管理");
ButtonGroup group = new ButtonGroup();
JRadioButton AdministratorRadio = new JRadioButton("超级管理员");
JRadioButton BookmanagerRadio = new JRadioButton("图书管理员");
JRadioButton BorrowmanagerRadio = new JRadioButton("借阅管理员");
JRadioButton UserRadio = new JRadioButton("普通会员", true);
JMenu mAdministrator;
JMenu mBookManager;
JMenu mBorrowManager;
JMenu mUser;
JMenu mOther;
JMenuItem mUserManage;
JMenuItem mBookManage;
JMenuItem mBorrowManage;
JMenuItem mInquiry;
JMenuItem mBorrowInfo;
JMenuItem mUserSetting;
JMenuItem mLogout;
JMenuItem mExitItem;
JMenuItem mAuthor;
JMenuItem mAbout;
JLabel usernameLabel = new JLabel("姓 名:");
JLabel userpwdLabel = new JLabel("密 码:");
JLabel usercdtLabel = new JLabel("用户信用:");
JLabel usertypeLabel = new JLabel("用户类型:");
JLabel useraccountLabel = new JLabel("用户帐户:");
JTextField useraccount = new JTextField();
JTextField username = new JTextField();
JPasswordField userpwd = new JPasswordField();
JTextField usercdt = new JTextField();
JPanel deletemodify = new JPanel();
JPanel usertype = new JPanel();
JPanel framecenter = new JPanel();
JPanel underselect = new JPanel();
JPanel underselectleft = new JPanel();
JPanel allmessage = new JPanel();
JPanel leftmessage = new JPanel();
JPanel rightmessage = new JPanel();
JPanel username_userpwd_Label = new JPanel();
JPanel username_userpwd_TextField = new JPanel();
JPanel usercdt_usertype_Label = new JPanel();
JPanel usercdt_usertype_TextField = new JPanel();
JButton inquiryButton = new JButton("查询");
JButton ModifyButton = new JButton("修改");
JButton DeleteButton = new JButton("删除");
String superaccount = new String();
String[] names;
int i, j, RowNum, ColNum;
Object[][] info;
JTable table;
String staticsuperuseraccount = new String();
String staticsuperusertype = new String();
public UserManageModule(String superuseraccount, String superusertype)
{
staticsuperuseraccount = superuseraccount;
staticsuperusertype = superusertype;
updateSwing();
JMenuBar mBar = new JMenuBar();
mBar.setOpaque(true);
mAdministrator = new JMenu("超级管理员选项");
mBookManager = new JMenu("图书管理员选项");
mBorrowManager = new JMenu("借阅管理员选项");
mUser = new JMenu("用户选项");
mUserManage = new JMenuItem("用户管理");
mBookManage = new JMenuItem("图书管理");
mBorrowManage = new JMenuItem("借阅管理");
mInquiry = new JMenuItem("图书查询");
mBorrowInfo = new JMenuItem("您的借阅信息");
mUserSetting = new JMenuItem("用户信息");
mLogout = new JMenuItem("注销");
mExitItem = new JMenuItem("退出系统");
mOther = new JMenu("其他");
mAuthor = new JMenuItem("作者");
mAbout = new JMenuItem("关于");
mAdministrator.add(mUserManage);
mUserManage.setEnabled(false);
mAdministrator.add(mBookManage);
mAdministrator.add(mBorrowManage);
mAdministrator.add(mInquiry);
mAdministrator.add(mBorrowInfo);
mAdministrator.add(mUserSetting);
mAdministrator.add(mLogout);
mAdministrator.add(mExitItem);
mOther.add(mAuthor);
mOther.add(mAbout);
mBar.add(mAdministrator);
mBar.add(mOther);
mUserManage.addActionListener(this);
mBookManage.addActionListener(this);
mBorrowManage.addActionListener(this);
mInquiry.addActionListener(this);
mBorrowInfo.addActionListener(this);
mUserSetting.addActionListener(this);
mLogout.addActionListener(this);
mExitItem.addActionListener(this);
mAuthor.addActionListener(this);
mAbout.addActionListener(this);
inquiryButton.addActionListener(this);
ModifyButton.addActionListener(this);
DeleteButton.addActionListener(this);
AdministratorRadio.setActionCommand("超级管理员");
BookmanagerRadio.setActionCommand("图书管理员");
BorrowmanagerRadio.setActionCommand("借阅管理员");
UserRadio.setActionCommand("普通会员");
group.add(AdministratorRadio);
group.add(BookmanagerRadio);
group.add(BorrowmanagerRadio);
group.add(UserRadio);
AdministratorRadio.addActionListener(this);
BookmanagerRadio.addActionListener(this);
BorrowmanagerRadio.addActionListener(this);
UserRadio.addActionListener(this);
f.setJMenuBar(mBar);
f.setLayout(new BorderLayout(2, 2));
deletemodify.setLayout(new GridLayout(1, 2));
usertype.setLayout(new GridLayout(1, 4));
framecenter.setLayout(new BorderLayout());
underselect.setLayout(new GridLayout(1, 2));
underselectleft.setLayout(new BorderLayout());
allmessage.setLayout(new GridLayout(1, 2));
leftmessage.setLayout(new BorderLayout());
rightmessage.setLayout(new BorderLayout());
username_userpwd_Label.setLayout(new GridLayout(2, 1, 3, 3));
username_userpwd_TextField.setLayout(new GridLayout(2, 1, 3, 3));
usercdt_usertype_Label.setLayout(new GridLayout(2, 1, 3, 3));
usercdt_usertype_TextField.setLayout(new GridLayout(2, 1, 3, 3));
deletemodify.add(DeleteButton);
deletemodify.add(ModifyButton);
framecenter.add(new JScrollPane(table), BorderLayout.CENTER);
framecenter.add(deletemodify, BorderLayout.SOUTH);
underselectleft.add(useraccountLabel, BorderLayout.WEST);
underselectleft.add(useraccount, BorderLayout.CENTER);
underselect.add(underselectleft);
underselect.add(inquiryButton);
usertype.add(AdministratorRadio);
usertype.add(BookmanagerRadio);
usertype.add(BorrowmanagerRadio);
usertype.add(UserRadio);
username_userpwd_Label.add(usernameLabel);
username_userpwd_Label.add(userpwdLabel);
username_userpwd_TextField.add(username);
username.setEditable(false);
username_userpwd_TextField.add(userpwd);
usercdt_usertype_Label.add(usercdtLabel);
usercdt_usertype_Label.add(usertypeLabel);
usercdt_usertype_TextField.add(usercdt);
usercdt_usertype_TextField.add(usertype);
leftmessage.add(username_userpwd_Label, BorderLayout.WEST);
leftmessage.add(username_userpwd_TextField, BorderLayout.CENTER);
rightmessage.add(usercdt_usertype_Label, BorderLayout.WEST);
rightmessage.add(usercdt_usertype_TextField, BorderLayout.CENTER);
allmessage.add(leftmessage);
allmessage.add(rightmessage);
useraccount.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER)
{
inquiryButton.doClick();
}
}
public void keyReleased(KeyEvent args)
{
}
public void keyTyped(KeyEvent args)
{
}
});
f.add(allmessage, BorderLayout.NORTH);
f.add(underselect, BorderLayout.SOUTH);
table.addMouseListener(this);
f.add(framecenter, BorderLayout.CENTER);
f.setSize(new Dimension(1000, 600));
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension fra = f.getSize();
f.setLocation((screenSize.width - fra.width) / 2, (screenSize.height - fra.height) / 2);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
JOptionPane.showMessageDialog(null, "已经注销!", "提示", JOptionPane.INFORMATION_MESSAGE);
f.dispose();
new LoginModule();
}
});
}
public void updateSwing()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select count(*) from usertable");
rs.next();
RowNum = rs.getInt(1) - 1;
rs = stmt.executeQuery("select userid as '用户ID',useraccount as '用户帐户',userlogdate as '最后登录时间',usercdt as '用户信用',usertype as '用户种类' from usertable");
ResultSetMetaData rsmd = rs.getMetaData();
ColNum = rsmd.getColumnCount();
names = new String[ColNum];
for (i = 1; i <= ColNum; i++)
names[i - 1] = rsmd.getColumnName(i);
info = new Object[RowNum][];
i = 0;
rs.next();
while (rs.next())
{
info[i] = new Object[ColNum];
for (j = 1; j <= ColNum; j++)
info[i][j - 1] = rs.getObject(j);
i++;
}
DefaultTableModel model = new DefaultTableModel(info, names);
table = new JTable(model);
table.setPreferredScrollableViewportSize(f.getSize());
} catch (Exception e)
{
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + e.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
public void mouseClicked(MouseEvent e)
{
int i = table.getSelectedRow();
String selectaccount = table.getValueAt(i, 1).toString();
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from usertable where useraccount='" + selectaccount + "'");
rs.next();
superaccount = rs.getString("useraccount");
username.setText(rs.getString("username"));
usercdt.setText(rs.getString("usercdt"));
String selectionusertypeString = rs.getString("usertype");
if (selectionusertypeString.compareTo("超级管理员") == 0)
AdministratorRadio.setSelected(true);
if (selectionusertypeString.compareTo("图书管理员") == 0)
BookmanagerRadio.setSelected(true);
if (selectionusertypeString.compareTo("借阅管理员") == 0)
BorrowmanagerRadio.setSelected(true);
if (selectionusertypeString.compareTo("普通会员") == 0)
UserRadio.setSelected(true);
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("用户管理"))
{
new UserManageModule(staticsuperuseraccount, staticsuperusertype);
f.dispose();
} else if (s.equals("图书管理"))
{
new BookManageModule(staticsuperuseraccount, staticsuperusertype);
f.dispose();
} else if (s.equals("借阅管理"))
{
new BookBorrowModule(staticsuperuseraccount, staticsuperusertype);
f.dispose();
} else if (s.equals("图书查询"))
{
new InquiryModule(staticsuperuseraccount, staticsuperusertype);
f.dispose();
} else if (s.equals("用户信息"))
{
new UserInformationModule(staticsuperuseraccount, staticsuperusertype);
} else if (s.equals("注销"))
{
int result = JOptionPane.showConfirmDialog(f, "你确定要注销吗?");
if (result == JOptionPane.OK_OPTION)
{
f.dispose();
new LoginModule();
} else
return;
} else if (s.equals("退出系统"))
{
System.exit(0);
} else if (s.equals("作者"))
{
JOptionPane.showMessageDialog(null, "/*~Sqrt5~*/", "作者", JOptionPane.INFORMATION_MESSAGE);
} else if (s.equals("关于"))
{
JOptionPane.showMessageDialog(null, "图书管理系统Java版\n(c) Copyright LibraryInformationManagementSystem 2012.\nAll rights reserved.\nVersion 1.0.0", "关于 图书管理系统Java版", JOptionPane.INFORMATION_MESSAGE);
} else if (s.equals("查询"))
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
String useraccountString = useraccount.getText().trim();
String useraccountStringsql = new String();
if (useraccountString.compareTo("") == 0)
useraccountStringsql = " 0=0 ";
else
useraccountStringsql = " useraccount like '%" + useraccountString + "%' ";
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.setRowCount(0);
ResultSet rs = stmt.executeQuery("select userid as '用户ID',useraccount as '用户帐户',userlogdate as '最后登录时间',usercdt as '用户信用',usertype as '用户种类' from usertable where " + useraccountStringsql);
while (rs.next())
{
String[] arr = new String[5];
arr[0] = rs.getString("用户ID");
arr[1] = rs.getString("用户帐户");
arr[2] = rs.getString("最后登录时间");
arr[3] = rs.getString("用户信用");
arr[4] = rs.getString("用户种类");
tableModel.addRow(arr);
}
table.invalidate();
rs.close();
stmt.close();
con.close();
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
} else if (s.equals("修改"))
{
int result = JOptionPane.showConfirmDialog(f, "你确定要修改吗?");
if (result == JOptionPane.OK_OPTION)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
@SuppressWarnings("deprecation")
String userpwdString = userpwd.getText().trim();
String usercdtString = usercdt.getText().trim();
String modifyusertype = group.getSelection().getActionCommand();
if (userpwdString.equals(""))
stmt.execute("update usertable set usercdt='" + usercdtString + "' where useraccount='" + superaccount + "'");
else
{
stmt.execute("update usertable set usercdt='" + usercdtString + "' where useraccount='" + superaccount + "'");
stmt.execute("update usertable set userpwd='" + userpwdString + "' where useraccount='" + superaccount + "'");
}
stmt.execute("update usertable set usertype='" + modifyusertype + "' where useraccount='" + superaccount + "'");
superaccount = "";
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.setRowCount(0);
ResultSet rs = stmt.executeQuery("select userid as '用户ID',useraccount as '用户帐户',userlogdate as '最后登录时间',usercdt as '用户信用',usertype as '用户种类' from usertable");
rs.next();
while (rs.next())
{
String[] arr = new String[5];
arr[0] = rs.getString("用户ID");
arr[1] = rs.getString("用户帐户");
arr[2] = rs.getString("最后登录时间");
arr[3] = rs.getString("用户信用");
arr[4] = rs.getString("用户种类");
tableModel.addRow(arr);
}
table.invalidate();
rs.close();
stmt.close();
con.close();
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
useraccount.setText("");
userpwd.setText("");
usercdt.setText("");
UserRadio.setSelected(true);
}
} else if (s.equals("删除"))
{
int result = JOptionPane.showConfirmDialog(f, "你确定要删除吗?");
if (result == JOptionPane.OK_OPTION)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
stmt.execute("delete usertable where useraccount='" + superaccount + "'");
superaccount = "";
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.setRowCount(0);
ResultSet rs = stmt.executeQuery("select userid as '用户ID',useraccount as '用户帐户',userlogdate as '最后登录时间',usercdt as '用户信用',usertype as '用户种类' from usertable");
rs.next();
while (rs.next())
{
String[] arr = new String[5];
arr[0] = rs.getString("用户ID");
arr[1] = rs.getString("用户帐户");
arr[2] = rs.getString("最后登录时间");
arr[3] = rs.getString("用户信用");
arr[4] = rs.getString("用户种类");
tableModel.addRow(arr);
}
table.invalidate();
rs.close();
stmt.close();
con.close();
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
useraccount.setText("");
userpwd.setText("");
usercdt.setText("");
UserRadio.setSelected(true);
}
} else if (s.equals("您的借阅信息"))
{
new UserBorrowInformationModule(staticsuperuseraccount, staticsuperusertype);
}
}
}
/*图书管理模块*/
package Main;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.JOptionPane;
public class BookManageModule extends MouseAdapter implements ActionListener
{
JFrame f = new JFrame("图书管理");
ButtonGroup group = new ButtonGroup();
JMenu mAdministrator;
JMenu mBookManager;
JMenu mBorrowManager;
JMenu mUser;
JMenu mOther;
JMenuItem mUserManage;
JMenuItem mBookManage;
JMenuItem mBorrowManage;
JMenuItem mInquiry;
JMenuItem mBorrowInfo;
JMenuItem mUserSetting;
JMenuItem mLogout;
JMenuItem mExitItem;
JMenuItem mAuthor;
JMenuItem mAbout;
JLabel booknameJLabel = new JLabel("书名(*):");
JLabel isbnJLabel = new JLabel("ISBN:");
JLabel booknamesJLabel = new JLabel("书名:");
JLabel isbnsJLabel = new JLabel("ISBN:");
JLabel authorlJLabel = new JLabel("作者(*):");
JLabel pressJLabel = new JLabel("出版社(*):");
JLabel pressdateJLabel = new JLabel("出版日期:");
JLabel bookstockJLabel = new JLabel("库存量(*):");
JTextField booknameselectJTextField = new JTextField();
JTextField isbnselectJTextField = new JTextField();
JTextField bookname = new JTextField();
JTextField isbn = new JTextField();
JTextField author = new JTextField();
JTextField press = new JTextField();
JTextField pressdate = new JTextField();
JTextField bookstock = new JTextField();
JPanel booknameselect = new JPanel();
JPanel isbnselect = new JPanel();
JPanel deletemodify = new JPanel();
JPanel framecenter = new JPanel();
JPanel underselect = new JPanel();
JPanel underselectleft = new JPanel();
JPanel allmessage = new JPanel();
JPanel leftmessage = new JPanel();
JPanel rightmessage = new JPanel();
JPanel bookname_isbn_author_Label = new JPanel();
JPanel bookname_isbn_author_TextField = new JPanel();
JPanel press_pressdate_bookstock_Label = new JPanel();
JPanel press_pressdate_bookstock_TextField = new JPanel();
JButton inquiryButton = new JButton("查询");
JButton ModifyButton = new JButton("修改");
JButton AddButton = new JButton("添加");
JButton DeleteButton = new JButton("删除");
int superbookid;
String[] names;
int i, j, RowNum, ColNum;
Object[][] info;
JTable table;
String staticsuperuseraccount = new String();
String staticsuperusertype = new String();
public BookManageModule(String superaccount, String superusertype)
{
staticsuperuseraccount = superaccount;
staticsuperusertype = superusertype;
updateSwing();
JMenuBar mBar = new JMenuBar();
mBar.setOpaque(true);
mAdministrator = new JMenu("超级管理员选项");
mBookManager = new JMenu("图书管理员选项");
mBorrowManager = new JMenu("借阅管理员选项");
mUser = new JMenu("用户选项");
mUserManage = new JMenuItem("用户管理");
mBookManage = new JMenuItem("图书管理");
mBorrowManage = new JMenuItem("借阅管理");
mInquiry = new JMenuItem("图书查询");
mBorrowInfo = new JMenuItem("您的借阅信息");
mUserSetting = new JMenuItem("用户信息");
mLogout = new JMenuItem("注销");
mExitItem = new JMenuItem("退出系统");
mOther = new JMenu("其他");
mAuthor = new JMenuItem("作者");
mAbout = new JMenuItem("关于");
if (superusertype.compareTo("超级管理员") == 0)
{
mAdministrator.add(mUserManage);
mAdministrator.add(mBookManage);
mBookManage.setEnabled(false);
mAdministrator.add(mBorrowManage);
mAdministrator.add(mInquiry);
mAdministrator.add(mBorrowInfo);
mAdministrator.add(mUserSetting);
mAdministrator.add(mLogout);
mAdministrator.add(mExitItem);
mOther.add(mAuthor);
mOther.add(mAbout);
mBar.add(mAdministrator);
mBar.add(mOther);
mUserManage.addActionListener(this);
mBookManage.addActionListener(this);
mBorrowManage.addActionListener(this);
mInquiry.addActionListener(this);
mBorrowInfo.addActionListener(this);
mUserSetting.addActionListener(this);
mLogout.addActionListener(this);
mExitItem.addActionListener(this);
mAuthor.addActionListener(this);
mAbout.addActionListener(this);
}
if (superusertype.compareTo("图书管理员") == 0)
{
mBookManager.add(mBookManage);
mBookManage.setEnabled(false);
mBookManager.add(mInquiry);
mBookManager.add(mBorrowInfo);
mBookManager.add(mUserSetting);
mBookManager.add(mLogout);
mBookManager.add(mExitItem);
mOther.add(mAuthor);
mOther.add(mAbout);
mBar.add(mBookManager);
mBar.add(mOther);
mBookManage.addActionListener(this);
mInquiry.addActionListener(this);
mBorrowInfo.addActionListener(this);
mUserSetting.addActionListener(this);
mLogout.addActionListener(this);
mExitItem.addActionListener(this);
mAuthor.addActionListener(this);
mAbout.addActionListener(this);
}
inquiryButton.addActionListener(this);
ModifyButton.addActionListener(this);
AddButton.addActionListener(this);
DeleteButton.addActionListener(this);
table.addMouseListener(this);
f.setJMenuBar(mBar);
f.setLayout(new BorderLayout(2, 2));
deletemodify.setLayout(new GridLayout(1, 3));
framecenter.setLayout(new BorderLayout());
underselect.setLayout(new GridLayout(1, 2));
underselectleft.setLayout(new GridLayout(1, 2));
booknameselect.setLayout(new BorderLayout());
isbnselect.setLayout(new BorderLayout());
allmessage.setLayout(new GridLayout(1, 2));
leftmessage.setLayout(new BorderLayout());
rightmessage.setLayout(new BorderLayout());
bookname_isbn_author_Label.setLayout(new GridLayout(3, 1));
bookname_isbn_author_TextField.setLayout(new GridLayout(3, 1));
press_pressdate_bookstock_Label.setLayout(new GridLayout(3, 1));
press_pressdate_bookstock_TextField.setLayout(new GridLayout(3, 1));
deletemodify.add(DeleteButton);
deletemodify.add(AddButton);
deletemodify.add(ModifyButton);
framecenter.add(new JScrollPane(table), BorderLayout.CENTER);
framecenter.add(deletemodify, BorderLayout.SOUTH);
booknameselect.add(booknamesJLabel, BorderLayout.WEST);
booknameselect.add(booknameselectJTextField, BorderLayout.CENTER);
isbnselect.add(isbnsJLabel, BorderLayout.WEST);
isbnselect.add(isbnselectJTextField, BorderLayout.CENTER);
underselectleft.add(booknameselect);
underselectleft.add(isbnselect);
underselect.add(underselectleft);
underselect.add(inquiryButton);
bookname_isbn_author_Label.add(booknameJLabel);
bookname_isbn_author_Label.add(isbnJLabel);
bookname_isbn_author_Label.add(authorlJLabel);
bookname_isbn_author_TextField.add(bookname);
bookname_isbn_author_TextField.add(isbn);
bookname_isbn_author_TextField.add(author);
press_pressdate_bookstock_Label.add(pressJLabel);
press_pressdate_bookstock_Label.add(pressdateJLabel);
press_pressdate_bookstock_Label.add(bookstockJLabel);
press_pressdate_bookstock_TextField.add(press);
press_pressdate_bookstock_TextField.add(pressdate);
press_pressdate_bookstock_TextField.add(bookstock);
leftmessage.add(bookname_isbn_author_Label, BorderLayout.WEST);
leftmessage.add(bookname_isbn_author_TextField, BorderLayout.CENTER);
rightmessage.add(press_pressdate_bookstock_Label, BorderLayout.WEST);
rightmessage.add(press_pressdate_bookstock_TextField, BorderLayout.CENTER);
allmessage.add(leftmessage);
allmessage.add(rightmessage);
booknameselectJTextField.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER)
{
inquiryButton.doClick();
}
}
public void keyReleased(KeyEvent args)
{
}
public void keyTyped(KeyEvent args)
{
}
});
isbnselectJTextField.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER)
{
inquiryButton.doClick();
}
}
public void keyReleased(KeyEvent args)
{
}
public void keyTyped(KeyEvent args)
{
}
});
f.add(allmessage, BorderLayout.NORTH);
f.add(underselect, BorderLayout.SOUTH);
f.add(framecenter, BorderLayout.CENTER);
f.setSize(new Dimension(1000, 600));
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension fra = f.getSize();
f.setLocation((screenSize.width - fra.width) / 2, (screenSize.height - fra.height) / 2);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
JOptionPane.showMessageDialog(null, "已经注销!", "提示", JOptionPane.INFORMATION_MESSAGE);
f.dispose();
new LoginModule();
}
});
}
public void updateSwing()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select count(*) from booktable");
rs.next();
RowNum = rs.getInt(1);
rs = stmt.executeQuery("select bookid as '图书ID',bookname as '书名',isbn as 'ISBN',author as '作者',press as '出版社',pressdate as '出版日期',bookstock as '库存量',borrowtimes as '借阅次数' from booktable");
ResultSetMetaData rsmd = rs.getMetaData();
ColNum = rsmd.getColumnCount();
names = new String[ColNum];
for (i = 1; i <= ColNum; i++)
names[i - 1] = rsmd.getColumnName(i);
info = new Object[RowNum][];
i = 0;
while (rs.next())
{
info[i] = new Object[ColNum];
for (j = 1; j <= ColNum; j++)
info[i][j - 1] = rs.getObject(j);
i++;
}
DefaultTableModel model = new DefaultTableModel(info, names);
table = new JTable(model);
table.setPreferredScrollableViewportSize(f.getSize());
} catch (Exception e)
{
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + e.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
public void mouseClicked(MouseEvent e)
{
int i = table.getSelectedRow();
String selectbookid = table.getValueAt(i, 0).toString();
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from booktable where bookid='" + selectbookid + "'");
rs.next();
superbookid = rs.getInt("bookid");
bookname.setText(rs.getString("bookname"));
isbn.setText(rs.getString("isbn"));
author.setText(rs.getString("author"));
press.setText(rs.getString("press"));
pressdate.setText(rs.getString("pressdate"));
bookstock.setText(rs.getString("bookstock"));
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("用户管理"))
{
new UserManageModule(staticsuperuseraccount, staticsuperusertype);
f.dispose();
} else if (s.equals("图书管理"))
{
new BookManageModule(staticsuperuseraccount, staticsuperusertype);
f.dispose();
} else if (s.equals("借阅管理"))
{
new BookBorrowModule(staticsuperuseraccount, staticsuperusertype);
f.dispose();
} else if (s.equals("图书查询"))
{
new InquiryModule(staticsuperuseraccount, staticsuperusertype);
f.dispose();
} else if (s.equals("用户信息"))
{
new UserInformationModule(staticsuperuseraccount, staticsuperusertype);
} else if (s.equals("注销"))
{
int result = JOptionPane.showConfirmDialog(f, "你确定要注销吗?");
if (result == JOptionPane.OK_OPTION)
{
f.dispose();
new LoginModule();
} else
return;
} else if (s.equals("退出系统"))
{
System.exit(0);
} else if (s.equals("作者"))
{
JOptionPane.showMessageDialog(null, "/*~Sqrt5~*/", "作者", JOptionPane.INFORMATION_MESSAGE);
} else if (s.equals("关于"))
{
JOptionPane.showMessageDialog(null, "图书管理系统Java版\n(c) Copyright LibraryInformationManagementSystem 2012.\nAll rights reserved.\nVersion 1.0.0", "关于 图书管理系统Java版", JOptionPane.INFORMATION_MESSAGE);
} else if (s.equals("查询"))
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
String booknameString = booknameselectJTextField.getText().trim();
String booknameStringsql = new String();
String isbnString = isbnselectJTextField.getText().trim();
String isbnStringsql = new String();
if (booknameString.compareTo("") == 0)
booknameStringsql = " 0=0 ";
else
booknameStringsql = " bookname like '%" + booknameString + "%' ";
if (isbnString.compareTo("") == 0)
isbnStringsql = " 0=0 ";
else
isbnStringsql = " isbn like '%" + isbnString + "%' ";
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.setRowCount(0);
ResultSet rs = stmt.executeQuery("select bookid as '图书ID',bookname as '书名',isbn as 'ISBN',author as '作者',press as '出版社',pressdate as '出版日期',bookstock as '库存量',borrowtimes as '借阅次数' from booktable where " + booknameStringsql + " and " + isbnStringsql);
while (rs.next())
{
String[] arr = new String[8];
arr[0] = rs.getString("图书ID");
arr[1] = rs.getString("书名");
arr[2] = rs.getString("ISBN");
arr[3] = rs.getString("作者");
arr[4] = rs.getString("出版社");
arr[5] = rs.getString("出版日期");
arr[6] = rs.getString("库存量");
arr[7] = rs.getString("借阅次数");
tableModel.addRow(arr);
}
table.invalidate();
rs.close();
stmt.close();
con.close();
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
} else if (s.equals("修改"))
{
int result = JOptionPane.showConfirmDialog(f, "你确定要修改吗?");
if (result == JOptionPane.OK_OPTION)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
stmt.execute("update booktable set bookname='" + bookname.getText() + "' where bookid=" + superbookid);
stmt.execute("update booktable set isbn='" + isbn.getText() + "' where bookid=" + superbookid);
stmt.execute("update booktable set author='" + author.getText() + "' where bookid=" + superbookid);
stmt.execute("update booktable set press='" + press.getText() + "' where bookid=" + superbookid);
if (!pressdate.getText().trim().equals(""))
stmt.execute("update booktable set pressdate='" + pressdate.getText() + "' where bookid=" + superbookid);
stmt.execute("update booktable set bookstock='" + bookstock.getText() + "' where bookid=" + superbookid);
superbookid = 0;
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.setRowCount(0);
ResultSet rs = stmt.executeQuery("select bookid as '图书ID',bookname as '书名',isbn as 'ISBN',author as '作者',press as '出版社',pressdate as '出版日期',bookstock as '库存量',borrowtimes as '借阅次数' from booktable");
while (rs.next())
{
String[] arr = new String[8];
arr[0] = rs.getString("图书ID");
arr[1] = rs.getString("书名");
arr[2] = rs.getString("ISBN");
arr[3] = rs.getString("作者");
arr[4] = rs.getString("出版社");
arr[5] = rs.getString("出版日期");
arr[6] = rs.getString("库存量");
arr[7] = rs.getString("借阅次数");
tableModel.addRow(arr);
}
table.invalidate();
rs.close();
stmt.close();
con.close();
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
bookname.setText("");
isbn.setText("");
author.setText("");
press.setText("");
pressdate.setText("");
bookstock.setText("");
}
} else if (s.equals("添加"))
{
if (bookname.getText().trim().equals("") || author.getText().trim().equals("") || press.getText().trim().equals("") || bookstock.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null, "请将带星号内容填写完毕!", "提示", JOptionPane.WARNING_MESSAGE);
return;
}
if (pressdate.getText().trim().equals(""))
{
int resultpressdate = JOptionPane.showConfirmDialog(f, "出版日期将自动默认为今天?");
if (resultpressdate == JOptionPane.NO_OPTION || resultpressdate == JOptionPane.CANCEL_OPTION)
{
return;
}
}
int result = JOptionPane.showConfirmDialog(f, "你确定要添加吗?");
if (result == JOptionPane.OK_OPTION)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
if (!pressdate.getText().trim().equals(""))
stmt.execute("insert into booktable(bookname,isbn,author,press,pressdate,bookstock) values('" + bookname.getText() + "','" + isbn.getText() + "','" + author.getText() + "','" + press.getText() + "','" + pressdate.getText() + "','" + bookstock.getText() + "')");
else
stmt.execute("insert into booktable(bookname,isbn,author,press,bookstock) values('" + bookname.getText() + "','" + isbn.getText() + "','" + author.getText() + "','" + press.getText() + "','" + bookstock.getText() + "')");
superbookid = 0;
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.setRowCount(0);
ResultSet rs = stmt.executeQuery("select bookid as '图书ID',bookname as '书名',isbn as 'ISBN',author as '作者',press as '出版社',pressdate as '出版日期',bookstock as '库存量',borrowtimes as '借阅次数' from booktable");
while (rs.next())
{
String[] arr = new String[8];
arr[0] = rs.getString("图书ID");
arr[1] = rs.getString("书名");
arr[2] = rs.getString("ISBN");
arr[3] = rs.getString("作者");
arr[4] = rs.getString("出版社");
arr[5] = rs.getString("出版日期");
arr[6] = rs.getString("库存量");
arr[7] = rs.getString("借阅次数");
tableModel.addRow(arr);
}
table.invalidate();
rs.close();
stmt.close();
con.close();
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
bookname.setText("");
isbn.setText("");
author.setText("");
press.setText("");
pressdate.setText("");
bookstock.setText("");
}
} else if (s.equals("删除"))
{
int result = JOptionPane.showConfirmDialog(f, "你确定要删除吗?");
if (result == JOptionPane.OK_OPTION)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
stmt.execute("delete booktable where bookid=" + superbookid);
superbookid = 0;
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.setRowCount(0);
ResultSet rs = stmt.executeQuery("select bookid as '图书ID',bookname as '书名',isbn as 'ISBN',author as '作者',press as '出版社',pressdate as '出版日期',bookstock as '库存量',borrowtimes as '借阅次数' from booktable");
while (rs.next())
{
String[] arr = new String[8];
arr[0] = rs.getString("图书ID");
arr[1] = rs.getString("书名");
arr[2] = rs.getString("ISBN");
arr[3] = rs.getString("作者");
arr[4] = rs.getString("出版社");
arr[5] = rs.getString("出版日期");
arr[6] = rs.getString("库存量");
arr[7] = rs.getString("借阅次数");
tableModel.addRow(arr);
}
table.invalidate();
rs.close();
stmt.close();
con.close();
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
bookname.setText("");
isbn.setText("");
author.setText("");
press.setText("");
pressdate.setText("");
bookstock.setText("");
}
} else if (s.equals("您的借阅信息"))
{
new UserBorrowInformationModule(staticsuperuseraccount, staticsuperusertype);
}
}
}
/*借阅管理模块*/
package Main;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.JOptionPane;
public class BookBorrowModule extends MouseAdapter implements ActionListener
{
JFrame f = new JFrame("借阅管理");
int whichtable;
ButtonGroup group = new ButtonGroup();
JMenu mAdministrator;
JMenu mBookManager;
JMenu mBorrowManager;
JMenu mUser;
JMenu mOther;
JMenuItem mUserManage;
JMenuItem mBookManage;
JMenuItem mBorrowManage;
JMenuItem mInquiry;
JMenuItem mBorrowInfo;
JMenuItem mUserSetting;
JMenuItem mLogout;
JMenuItem mExitItem;
JMenuItem mAuthor;
JMenuItem mAbout;
JLabel booknameJLabel = new JLabel("书名:");
JLabel isbnJLabel = new JLabel("ISBN:");
JLabel booknamesJLabel = new JLabel("书 名:");
JLabel bookidsJLabel = new JLabel("图书ID:");
JLabel useridJLabel = new JLabel("用户ID:");
JLabel useraccountJLabel = new JLabel("用户名:");
JLabel authorlJLabel = new JLabel("作者:");
JLabel pressJLabel = new JLabel("出 版 社:");
JLabel pressdateJLabel = new JLabel("出版日期:");
JLabel bookstockJLabel = new JLabel("库 存 量:");
JLabel tablebookname = new JLabel("图书库存:");
JLabel tableapplyrequest = new JLabel("申请请求:");
JLabel tableborrow = new JLabel("已借出书籍:");
JTextField booknameselectJTextField = new JTextField();
JTextField bookidselectJTextField = new JTextField();
JTextField useridselectJTextField = new JTextField();
JTextField useraccountselectJTextField = new JTextField();
JTextField bookname = new JTextField();
JTextField isbn = new JTextField();
JTextField author = new JTextField();
JTextField press = new JTextField();
JTextField pressdate = new JTextField();
JTextField bookstock = new JTextField();
JPanel inoutJPanel = new JPanel();
JPanel booknameselect = new JPanel();
JPanel bookidselect = new JPanel();
JPanel useridselect = new JPanel();
JPanel useraccountselect = new JPanel();
JPanel framecenter = new JPanel();
JPanel underselect = new JPanel();
JPanel bookselect = new JPanel();
JPanel bookselectleft = new JPanel();
JPanel userselect = new JPanel();
JPanel userselectleft = new JPanel();
JPanel allmessage = new JPanel();
JPanel leftmessage = new JPanel();
JPanel rightmessage = new JPanel();
JPanel bookname_isbn_author_Label = new JPanel();
JPanel bookname_isbn_author_TextField = new JPanel();
JPanel press_pressdate_bookstock_Label = new JPanel();
JPanel press_pressdate_bookstock_TextField = new JPanel();
JPanel tableJPanel = new JPanel();
JPanel applytableJPanel = new JPanel();
JPanel borrowtableJPanel = new JPanel();
JButton inquiryBookButton = new JButton("查询图书");
JButton inquiryUserButton = new JButton("查询用户");
JButton outButton = new JButton("借出");
JButton inButton = new JButton("还回");
int superuseridselect;
int superbookidselect;
String[] names;
int i, j, RowNum, ColNum;
Object[][] info;
JTable table;
String[] borrownames;
int borrowi, borrowj, borrowRowNum, borrowColNum;
Object[][] borrowinfo;
JTable borrowtable;
String[] applynames;
int applyi, applyj, applyRowNum, applyColNum;
Object[][] applyinfo;
JTable applytable;
String staticsuperuseraccount = new String();
String staticsuperusertype = new String();
public BookBorrowModule(String superaccount, String superusertype)
{
staticsuperuseraccount = superaccount;
staticsuperusertype = superusertype;
whichtable = 0;
updateSwing();
updateapplySwing();
updateborrowSwing();
JMenuBar mBar = new JMenuBar();
mBar.setOpaque(true);
bookname.setEditable(false);
isbn.setEditable(false);
author.setEditable(false);
press.setEditable(false);
pressdate.setEditable(false);
bookstock.setEditable(false);
mAdministrator = new JMenu("超级管理员选项");
mBookManager = new JMenu("图书管理员选项");
mBorrowManager = new JMenu("借阅管理员选项");
mUser = new JMenu("用户选项");
mUserManage = new JMenuItem("用户管理");
mBookManage = new JMenuItem("图书管理");
mBorrowManage = new JMenuItem("借阅管理");
mInquiry = new JMenuItem("图书查询");
mBorrowInfo = new JMenuItem("您的借阅信息");
mUserSetting = new JMenuItem("用户信息");
mLogout = new JMenuItem("注销");
mExitItem = new JMenuItem("退出系统");
mOther = new JMenu("其他");
mAuthor = new JMenuItem("作者");
mAbout = new JMenuItem("关于");
if (superusertype.compareTo("超级管理员") == 0)
{
mAdministrator.add(mUserManage);
mAdministrator.add(mBookManage);
mAdministrator.add(mBorrowManage);
mBorrowManage.setEnabled(false);
mAdministrator.add(mInquiry);
mAdministrator.add(mBorrowInfo);
mAdministrator.add(mUserSetting);
mAdministrator.add(mLogout);
mAdministrator.add(mExitItem);
mOther.add(mAuthor);
mOther.add(mAbout);
mBar.add(mAdministrator);
mBar.add(mOther);
mUserManage.addActionListener(this);
mBookManage.addActionListener(this);
mBorrowManage.addActionListener(this);
mInquiry.addActionListener(this);
mBorrowInfo.addActionListener(this);
mUserSetting.addActionListener(this);
mLogout.addActionListener(this);
mExitItem.addActionListener(this);
mAuthor.addActionListener(this);
mAbout.addActionListener(this);
}
if (superusertype.compareTo("借阅管理员") == 0)
{
mBorrowManager.add(mBorrowManage);
mBorrowManage.setEnabled(false);
mBorrowManager.add(mInquiry);
mBorrowManager.add(mBorrowInfo);
mBorrowManager.add(mUserSetting);
mBorrowManager.add(mLogout);
mBorrowManager.add(mExitItem);
mOther.add(mAuthor);
mOther.add(mAbout);
mBar.add(mBorrowManager);
mBar.add(mOther);
mBorrowManage.addActionListener(this);
mInquiry.addActionListener(this);
mBorrowInfo.addActionListener(this);
mUserSetting.addActionListener(this);
mLogout.addActionListener(this);
mExitItem.addActionListener(this);
mAuthor.addActionListener(this);
mAbout.addActionListener(this);
}
inquiryBookButton.addActionListener(this);
inquiryUserButton.addActionListener(this);
inButton.addActionListener(this);
outButton.addActionListener(this);
table.addMouseListener(this);
applytable.addMouseListener(this);
borrowtable.addMouseListener(this);
f.setJMenuBar(mBar);
f.setLayout(new BorderLayout(2, 2));
inoutJPanel.setLayout(new GridLayout(1, 3, 3, 3));
framecenter.setLayout(new GridLayout(3, 1));
underselect.setLayout(new GridLayout(2, 1, 3, 3));
bookselect.setLayout(new GridLayout(1, 2));
bookselectleft.setLayout(new GridLayout(1, 2));
userselect.setLayout(new GridLayout(1, 2));
userselectleft.setLayout(new GridLayout(1, 2));
booknameselect.setLayout(new BorderLayout());
bookidselect.setLayout(new BorderLayout());
useridselect.setLayout(new BorderLayout());
useraccountselect.setLayout(new BorderLayout());
allmessage.setLayout(new GridLayout(1, 2));
leftmessage.setLayout(new BorderLayout());
rightmessage.setLayout(new BorderLayout());
bookname_isbn_author_Label.setLayout(new GridLayout(3, 1, 3, 3));
bookname_isbn_author_TextField.setLayout(new GridLayout(3, 1, 3, 3));
press_pressdate_bookstock_Label.setLayout(new GridLayout(3, 1, 3, 3));
press_pressdate_bookstock_TextField.setLayout(new GridLayout(3, 1, 3, 3));
tableJPanel.setLayout(new BorderLayout());
applytableJPanel.setLayout(new BorderLayout());
borrowtableJPanel.setLayout(new BorderLayout());
tableJPanel.add(tablebookname, BorderLayout.NORTH);
tableJPanel.add(new JScrollPane(table), BorderLayout.CENTER);
applytableJPanel.add(tableapplyrequest, BorderLayout.NORTH);
applytableJPanel.add(new JScrollPane(applytable), BorderLayout.CENTER);
borrowtableJPanel.add(tableborrow, BorderLayout.NORTH);
borrowtableJPanel.add(new JScrollPane(borrowtable), BorderLayout.CENTER);
inoutJPanel.add(inquiryUserButton);
inoutJPanel.add(inButton);
inoutJPanel.add(outButton);
framecenter.add(tableJPanel);
framecenter.add(applytableJPanel);
framecenter.add(borrowtableJPanel);
booknameselect.add(booknamesJLabel, BorderLayout.WEST);
booknameselect.add(booknameselectJTextField, BorderLayout.CENTER);
bookidselect.add(bookidsJLabel, BorderLayout.WEST);
bookidselect.add(bookidselectJTextField, BorderLayout.CENTER);
useridselect.add(useridJLabel, BorderLayout.WEST);
useridselect.add(useridselectJTextField, BorderLayout.CENTER);
useraccountselect.add(useraccountJLabel, BorderLayout.WEST);
useraccountselect.add(useraccountselectJTextField, BorderLayout.CENTER);
bookselectleft.add(bookidselect);
bookselectleft.add(booknameselect);
bookselect.add(bookselectleft);
bookselect.add(inquiryBookButton);
userselectleft.add(useridselect);
userselectleft.add(useraccountselect);
userselect.add(userselectleft);
userselect.add(inoutJPanel);
underselect.add(userselect);
underselect.add(bookselect);
bookname_isbn_author_Label.add(booknameJLabel);
bookname_isbn_author_Label.add(isbnJLabel);
bookname_isbn_author_Label.add(authorlJLabel);
bookname_isbn_author_TextField.add(bookname);
bookname_isbn_author_TextField.add(isbn);
bookname_isbn_author_TextField.add(author);
press_pressdate_bookstock_Label.add(pressJLabel);
press_pressdate_bookstock_Label.add(pressdateJLabel);
press_pressdate_bookstock_Label.add(bookstockJLabel);
press_pressdate_bookstock_TextField.add(press);
press_pressdate_bookstock_TextField.add(pressdate);
press_pressdate_bookstock_TextField.add(bookstock);
leftmessage.add(bookname_isbn_author_Label, BorderLayout.WEST);
leftmessage.add(bookname_isbn_author_TextField, BorderLayout.CENTER);
rightmessage.add(press_pressdate_bookstock_Label, BorderLayout.WEST);
rightmessage.add(press_pressdate_bookstock_TextField, BorderLayout.CENTER);
allmessage.add(leftmessage);
allmessage.add(rightmessage);
booknameselectJTextField.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER)
{
inquiryBookButton.doClick();
}
}
public void keyReleased(KeyEvent args)
{
}
public void keyTyped(KeyEvent args)
{
}
});
bookidselectJTextField.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER)
{
inquiryBookButton.doClick();
}
}
public void keyReleased(KeyEvent args)
{
}
public void keyTyped(KeyEvent args)
{
}
});
useridselectJTextField.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER)
{
inquiryUserButton.doClick();
}
}
public void keyReleased(KeyEvent args)
{
}
public void keyTyped(KeyEvent args)
{
}
});
useraccountselectJTextField.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER)
{
inquiryUserButton.doClick();
}
}
public void keyReleased(KeyEvent args)
{
}
public void keyTyped(KeyEvent args)
{
}
});
f.add(allmessage, BorderLayout.NORTH);
f.add(underselect, BorderLayout.SOUTH);
f.add(framecenter, BorderLayout.CENTER);
f.setSize(new Dimension(1000, 600));
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension fra = f.getSize();
f.setLocation((screenSize.width - fra.width) / 2, (screenSize.height - fra.height) / 2);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
JOptionPane.showMessageDialog(null, "已经注销!", "提示", JOptionPane.INFORMATION_MESSAGE);
f.dispose();
new LoginModule();
}
});
}
public void updateSwing()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select count(*) from booktable");
rs.next();
RowNum = rs.getInt(1);
rs = stmt.executeQuery("select bookid as '图书ID',bookname as '书名',isbn as 'ISBN',author as '作者',press as '出版社',pressdate as '出版日期',bookstock as '库存量',borrowtimes as '借阅次数' from booktable");
ResultSetMetaData rsmd = rs.getMetaData();
ColNum = rsmd.getColumnCount();
names = new String[ColNum];
for (i = 1; i <= ColNum; i++)
names[i - 1] = rsmd.getColumnName(i);
info = new Object[RowNum][];
i = 0;
while (rs.next())
{
info[i] = new Object[ColNum];
for (j = 1; j <= ColNum; j++)
info[i][j - 1] = rs.getObject(j);
i++;
}
DefaultTableModel model = new DefaultTableModel(info, names);
table = new JTable(model);
table.setPreferredScrollableViewportSize(f.getSize());
rs.close();
stmt.close();
con.close();
} catch (Exception e)
{
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + e.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
public void updateborrowSwing()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select count(*) from borrowtable");
rs.next();
borrowRowNum = rs.getInt(1);
stmt.execute("update borrowtable set borroweddays=datediff(day,borrowstime,getdate())");
rs = stmt.executeQuery("select usertable.userid as '用户ID',usertable.useraccount as '用户名',usertable.username as '姓名',booktable.bookid as '图书ID',booktable.bookname as '书名',borrowstime as '借出日期',borrowetime as '归还日期',borroweddays as '已借天数' " + "from usertable,booktable,borrowtable " + "where usertable.userid=borrowtable.userid and booktable.bookid=borrowtable.bookid");
ResultSetMetaData rsmd = rs.getMetaData();
borrowColNum = rsmd.getColumnCount();
borrownames = new String[borrowColNum];
for (borrowi = 1; borrowi <= borrowColNum; borrowi++)
borrownames[borrowi - 1] = rsmd.getColumnName(borrowi);
borrowinfo = new Object[borrowRowNum][];
borrowi = 0;
while (rs.next())
{
borrowinfo[borrowi] = new Object[borrowColNum];
for (borrowj = 1; borrowj <= borrowColNum; borrowj++)
borrowinfo[borrowi][borrowj - 1] = rs.getObject(borrowj);
borrowi++;
}
DefaultTableModel model = new DefaultTableModel(borrowinfo, borrownames);
borrowtable = new JTable(model);
borrowtable.setPreferredScrollableViewportSize(f.getSize());
rs.close();
stmt.close();
con.close();
} catch (Exception e)
{
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + e.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
public void updateapplySwing()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select count(*) from applyborrowtable");
rs.next();
applyRowNum = rs.getInt(1);
stmt.execute("update applyborrowtable set applyeddays=datediff(day,applytime,getdate())");
rs = stmt.executeQuery("select * from(select usertable.userid as '用户ID',usertable.useraccount as '用户名',usertable.username as '姓名',booktable.bookid as '图书ID',booktable.bookname as '书名',applytime as '申请日期',applyeddays as '申请天数' from usertable,booktable,applyborrowtable where usertable.userid=applyborrowtable.userid and booktable.bookid=applyborrowtable.bookid) A ");
ResultSetMetaData rsmd = rs.getMetaData();
applyColNum = rsmd.getColumnCount();
applynames = new String[applyColNum];
for (applyi = 1; applyi <= applyColNum; applyi++)
applynames[applyi - 1] = rsmd.getColumnName(applyi);
applyinfo = new Object[applyRowNum][];
applyi = 0;
while (rs.next())
{
applyinfo[applyi] = new Object[applyColNum];
for (applyj = 1; applyj <= applyColNum; applyj++)
applyinfo[applyi][applyj - 1] = rs.getObject(applyj);
applyi++;
}
DefaultTableModel model = new DefaultTableModel(applyinfo, applynames);
applytable = new JTable(model);
applytable.setPreferredScrollableViewportSize(f.getSize());
} catch (Exception e)
{
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + e.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
public void mouseClicked(MouseEvent e)
{
if (table == e.getComponent())
{
try
{
int i = table.getSelectedRow();
String selectbookid = table.getValueAt(i, 0).toString();
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from booktable where bookid='" + selectbookid + "'");
rs.next();
superbookidselect = rs.getInt("bookid");
bookname.setText(rs.getString("bookname"));
isbn.setText(rs.getString("isbn"));
author.setText(rs.getString("author"));
press.setText(rs.getString("press"));
pressdate.setText(rs.getString("pressdate"));
bookstock.setText(rs.getString("bookstock"));
useridselectJTextField.setText("");
useraccountselectJTextField.setText("");
whichtable = 1;
DefaultTableModel tableModel = (DefaultTableModel) borrowtable.getModel();
tableModel.setRowCount(0);
stmt.execute("update borrowtable set borroweddays=datediff(day,borrowstime,getdate())");
ResultSet rst = stmt.executeQuery("select usertable.userid as '用户ID',usertable.useraccount as '用户名',usertable.username as '姓名',booktable.bookid as '图书ID',booktable.bookname as '书名',borrowstime as '借出日期',borrowetime as '归还日期',borroweddays as '已借天数' " + "from usertable,booktable,borrowtable " + "where usertable.userid=borrowtable.userid and booktable.bookid=borrowtable.bookid");
while (rst.next())
{
String[] arr = new String[8];
arr[0] = rst.getString("用户ID");
arr[1] = rst.getString("用户名");
arr[2] = rst.getString("姓名");
arr[3] = rst.getString("图书ID");
arr[4] = rst.getString("书名");
arr[5] = rst.getString("借出日期");
arr[6] = rst.getString("归还日期");
arr[7] = rst.getString("已借天数");
tableModel.addRow(arr);
}
borrowtable.invalidate();
tableModel = (DefaultTableModel) applytable.getModel();
tableModel.setRowCount(0);
stmt.execute("update applyborrowtable set applyeddays=datediff(day,applytime,getdate())");
ResultSet rset = stmt.executeQuery("select * from(select usertable.userid as '用户ID',usertable.useraccount as '用户名',usertable.username as '姓名',booktable.bookid as '图书ID',booktable.bookname as '书名',applytime as '申请日期',applyeddays as '申请天数' from usertable,booktable,applyborrowtable where usertable.userid=applyborrowtable.userid and booktable.bookid=applyborrowtable.bookid) A");
while (rset.next())
{
String[] arr = new String[7];
arr[0] = rset.getString("用户ID");
arr[1] = rset.getString("用户名");
arr[2] = rset.getString("姓名");
arr[3] = rset.getString("图书ID");
arr[4] = rset.getString("书名");
arr[5] = rset.getString("申请日期");
arr[6] = rset.getString("申请天数");
tableModel.addRow(arr);
}
applytable.invalidate();
rset.close();
rst.close();
rs.close();
stmt.close();
con.close();
return;
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
if (borrowtable == e.getComponent())
{
try
{
int borrowi = borrowtable.getSelectedRow();
String selectuserid = borrowtable.getValueAt(borrowi, 0).toString();
String selectbookid = borrowtable.getValueAt(borrowi, 3).toString();
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from borrowtable where userid='" + selectuserid + "' and bookid='" + selectbookid + "'");
rs.next();
superuseridselect = rs.getInt("userid");
superbookidselect = rs.getInt("bookid");
useridselectJTextField.setText("" + superuseridselect);
rs = stmt.executeQuery("select * from usertable where userid=" + superuseridselect);
rs.next();
useraccountselectJTextField.setText(rs.getString("useraccount"));
rs = stmt.executeQuery("select * from booktable where bookid=" + superbookidselect);
rs.next();
bookname.setText(rs.getString("bookname"));
isbn.setText(rs.getString("isbn"));
author.setText(rs.getString("author"));
press.setText(rs.getString("press"));
pressdate.setText(rs.getString("pressdate"));
bookstock.setText(rs.getString("bookstock"));
whichtable = 2;
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.setRowCount(0);
rs = stmt.executeQuery("select bookid as '图书ID',bookname as '书名',isbn as 'ISBN',author as '作者',press as '出版社',pressdate as '出版日期',bookstock as '库存量',borrowtimes as '借阅次数' from booktable");
while (rs.next())
{
String[] arr = new String[8];
arr[0] = rs.getString("图书ID");
arr[1] = rs.getString("书名");
arr[2] = rs.getString("ISBN");
arr[3] = rs.getString("作者");
arr[4] = rs.getString("出版社");
arr[5] = rs.getString("出版日期");
arr[6] = rs.getString("库存量");
arr[7] = rs.getString("借阅次数");
tableModel.addRow(arr);
}
table.invalidate();
tableModel = (DefaultTableModel) applytable.getModel();
tableModel.setRowCount(0);
stmt.execute("update applyborrowtable set applyeddays=datediff(day,applytime,getdate())");
ResultSet rset = stmt.executeQuery("select * from(select usertable.userid as '用户ID',usertable.useraccount as '用户名',usertable.username as '姓名',booktable.bookid as '图书ID',booktable.bookname as '书名',applytime as '申请日期',applyeddays as '申请天数' from usertable,booktable,applyborrowtable where usertable.userid=applyborrowtable.userid and booktable.bookid=applyborrowtable.bookid) A");
while (rset.next())
{
String[] arr = new String[7];
arr[0] = rset.getString("用户ID");
arr[1] = rset.getString("用户名");
arr[2] = rset.getString("姓名");
arr[3] = rset.getString("图书ID");
arr[4] = rset.getString("书名");
arr[5] = rset.getString("申请日期");
arr[6] = rset.getString("申请天数");
tableModel.addRow(arr);
}
applytable.invalidate();
rset.close();
rs.close();
stmt.close();
con.close();
return;
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
if (applytable == e.getComponent())
{
try
{
int applyi = applytable.getSelectedRow();
String selectuserid = applytable.getValueAt(applyi, 0).toString();
String selectbookid = applytable.getValueAt(applyi, 3).toString();
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from applyborrowtable where userid='" + selectuserid + "' and bookid='" + selectbookid + "'");
rs.next();
superuseridselect = rs.getInt("userid");
superbookidselect = rs.getInt("bookid");
useridselectJTextField.setText(rs.getString("userid"));
rs = stmt.executeQuery("select * from usertable where userid='" + superuseridselect + "'");
rs.next();
useraccountselectJTextField.setText(rs.getString("useraccount"));
rs = stmt.executeQuery("select * from booktable where bookid='" + superbookidselect + "'");
rs.next();
bookname.setText(rs.getString("bookname"));
isbn.setText(rs.getString("isbn"));
author.setText(rs.getString("author"));
press.setText(rs.getString("press"));
pressdate.setText(rs.getString("pressdate"));
bookstock.setText(rs.getString("bookstock"));
whichtable = 3;
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.setRowCount(0);
rs = stmt.executeQuery("select bookid as '图书ID',bookname as '书名',isbn as 'ISBN',author as '作者',press as '出版社',pressdate as '出版日期',bookstock as '库存量',borrowtimes as '借阅次数' from booktable");
while (rs.next())
{
String[] arr = new String[8];
arr[0] = rs.getString("图书ID");
arr[1] = rs.getString("书名");
arr[2] = rs.getString("ISBN");
arr[3] = rs.getString("作者");
arr[4] = rs.getString("出版社");
arr[5] = rs.getString("出版日期");
arr[6] = rs.getString("库存量");
arr[7] = rs.getString("借阅次数");
tableModel.addRow(arr);
}
table.invalidate();
tableModel = (DefaultTableModel) borrowtable.getModel();
tableModel.setRowCount(0);
stmt.execute("update borrowtable set borroweddays=datediff(day,borrowstime,getdate())");
ResultSet rst = stmt.executeQuery("select usertable.userid as '用户ID',usertable.useraccount as '用户名',usertable.username as '姓名',booktable.bookid as '图书ID',booktable.bookname as '书名',borrowstime as '借出日期',borrowetime as '归还日期',borroweddays as '已借天数' " + "from usertable,booktable,borrowtable " + "where usertable.userid=borrowtable.userid and booktable.bookid=borrowtable.bookid");
while (rst.next())
{
String[] arr = new String[8];
arr[0] = rst.getString("用户ID");
arr[1] = rst.getString("用户名");
arr[2] = rst.getString("姓名");
arr[3] = rst.getString("图书ID");
arr[4] = rst.getString("书名");
arr[5] = rst.getString("借出日期");
arr[6] = rst.getString("归还日期");
arr[7] = rst.getString("已借天数");
tableModel.addRow(arr);
}
borrowtable.invalidate();
rst.close();
rs.close();
stmt.close();
con.close();
return;
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("用户管理"))
{
new UserManageModule(staticsuperuseraccount, staticsuperusertype);
f.dispose();
} else if (s.equals("图书管理"))
{
new BookManageModule(staticsuperuseraccount, staticsuperusertype);
f.dispose();
} else if (s.equals("借阅管理"))
{
new BookBorrowModule(staticsuperuseraccount, staticsuperusertype);
f.dispose();
} else if (s.equals("图书查询"))
{
new InquiryModule(staticsuperuseraccount, staticsuperusertype);
f.dispose();
} else if (s.equals("用户信息"))
{
new UserInformationModule(staticsuperuseraccount, staticsuperusertype);
} else if (s.equals("注销"))
{
int result = JOptionPane.showConfirmDialog(f, "你确定要注销吗?");
if (result == JOptionPane.OK_OPTION)
{
f.dispose();
new LoginModule();
} else
return;
} else if (s.equals("退出系统"))
{
System.exit(0);
} else if (s.equals("作者"))
{
JOptionPane.showMessageDialog(null, "/*~Sqrt5~*/", "作者", JOptionPane.INFORMATION_MESSAGE);
} else if (s.equals("关于"))
{
JOptionPane.showMessageDialog(null, "图书管理系统Java版\n(c) Copyright LibraryInformationManagementSystem 2012.\nAll rights reserved.\nVersion 1.0.0", "关于 图书管理系统Java版", JOptionPane.INFORMATION_MESSAGE);
} else if (s.equals("借出"))
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
if (useridselectJTextField.getText().trim().equals("") && useraccountselectJTextField.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null, "请输入要借给的用户ID或用户名!", "提示", JOptionPane.INFORMATION_MESSAGE);
stmt.close();
con.close();
return;
}
if (whichtable == 1 || whichtable == 3)
{
String borrowtouser = new String();
if (!useridselectJTextField.getText().trim().equals(""))
borrowtouser = useridselectJTextField.getText().trim();
if (!useraccountselectJTextField.getText().trim().equals(""))
{
ResultSet rs = stmt.executeQuery("select userid from usertable where useraccount='" + useraccountselectJTextField.getText().trim() + "'");
rs.next();
borrowtouser = rs.getString("userid");
}
ResultSet rs = stmt.executeQuery("select userid from borrowtable where userid=" + borrowtouser + " and bookid=" + superbookidselect);
boolean isOk;
isOk = true;
while (rs.next())
{
isOk = false;
}
if (!isOk)
{
JOptionPane.showMessageDialog(null, "该用户已经借过这本书!", "提示", JOptionPane.INFORMATION_MESSAGE);
rs.close();
stmt.close();
con.close();
return;
}
rs = stmt.executeQuery("select bookstock from booktable where bookid='" + superbookidselect + "'");
int bookstock = 0;
rs.next();
bookstock = rs.getInt("bookstock");
if (bookstock < 1)
{
JOptionPane.showMessageDialog(null, "该书已经没有库存!", "提示", JOptionPane.INFORMATION_MESSAGE);
rs.close();
stmt.close();
con.close();
return;
}
rs = stmt.executeQuery("select userid from borrowtable where userid='" + borrowtouser + "'");
int borrownum = 0;
int applynum = 0;
int borrowusercdt = 0;
rs = stmt.executeQuery("select userid from borrowtable where userid='" + borrowtouser + "'");
while (rs.next())
{
borrownum++;
}
rs = stmt.executeQuery("select userid from applyborrowtable where userid='" + borrowtouser + "'");
while (rs.next())
{
applynum++;
}
rs = stmt.executeQuery("select usercdt from usertable where userid='" + borrowtouser + "'");
rs.next();
borrowusercdt = rs.getInt("usercdt");
if (borrownum + applynum <= borrowusercdt + 5)
{
int result = JOptionPane.showConfirmDialog(f, "确定?");
if (result == JOptionPane.OK_OPTION)
{
JOptionPane.showMessageDialog(null, "该用户可以借" + (borrowusercdt + 5) + "本书\n已借" + (borrownum + 1) + "本书!\n已申请" + (applynum - 1) + "本书", "提示", JOptionPane.INFORMATION_MESSAGE);
borrownum = 0;
borrowusercdt = 0;
stmt.execute("insert into borrowtable(userid,bookid,borrowstime,borrowetime) values(" + borrowtouser + "," + superbookidselect + ",getdate(),dateadd(day,30,getdate()))");
stmt.execute("update borrowtable set borroweddays=datediff(day,borrowstime,getdate()) where userid=" + borrowtouser + " and bookid=" + superbookidselect);
if (whichtable == 3)
{
stmt.execute("delete applyborrowtable where userid=" + superuseridselect + " and bookid=" + superbookidselect);
} else
{
stmt.execute("update booktable set bookstock=bookstock-1 where bookid=" + superbookidselect);
}
stmt.execute("update booktable set borrowtimes=borrowtimes+1 where bookid=" + superbookidselect);
JOptionPane.showMessageDialog(null, "借书成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.setRowCount(0);
rs = stmt.executeQuery("select bookid as '图书ID',bookname as '书名',isbn as 'ISBN',author as '作者',press as '出版社',pressdate as '出版日期',bookstock as '库存量',borrowtimes as '借阅次数' from booktable");
while (rs.next())
{
String[] arr = new String[8];
arr[0] = rs.getString("图书ID");
arr[1] = rs.getString("书名");
arr[2] = rs.getString("ISBN");
arr[3] = rs.getString("作者");
arr[4] = rs.getString("出版社");
arr[5] = rs.getString("出版日期");
arr[6] = rs.getString("库存量");
arr[7] = rs.getString("借阅次数");
tableModel.addRow(arr);
}
table.invalidate();
tableModel = (DefaultTableModel) borrowtable.getModel();
tableModel.setRowCount(0);
stmt.execute("update borrowtable set borroweddays=datediff(day,borrowstime,getdate())");
ResultSet rst = stmt.executeQuery("select usertable.userid as '用户ID',usertable.useraccount as '用户名',usertable.username as '姓名',booktable.bookid as '图书ID',booktable.bookname as '书名',borrowstime as '借出日期',borrowetime as '归还日期',borroweddays as '已借天数' " + "from usertable,booktable,borrowtable " + "where usertable.userid=borrowtable.userid and booktable.bookid=borrowtable.bookid");
while (rst.next())
{
String[] arr = new String[8];
arr[0] = rst.getString("用户ID");
arr[1] = rst.getString("用户名");
arr[2] = rst.getString("姓名");
arr[3] = rst.getString("图书ID");
arr[4] = rst.getString("书名");
arr[5] = rst.getString("借出日期");
arr[6] = rst.getString("归还日期");
arr[7] = rst.getString("已借天数");
tableModel.addRow(arr);
}
borrowtable.invalidate();
tableModel = (DefaultTableModel) applytable.getModel();
tableModel.setRowCount(0);
stmt.execute("update applyborrowtable set applyeddays=datediff(day,applytime,getdate())");
ResultSet rset = stmt.executeQuery("select * from(select usertable.userid as '用户ID',usertable.useraccount as '用户名',usertable.username as '姓名',booktable.bookid as '图书ID',booktable.bookname as '书名',applytime as '申请日期',applyeddays as '申请天数' from usertable,booktable,applyborrowtable where usertable.userid=applyborrowtable.userid and booktable.bookid=applyborrowtable.bookid) A");
while (rset.next())
{
String[] arr = new String[7];
arr[0] = rset.getString("用户ID");
arr[1] = rset.getString("用户名");
arr[2] = rset.getString("姓名");
arr[3] = rset.getString("图书ID");
arr[4] = rset.getString("书名");
arr[5] = rset.getString("申请日期");
arr[6] = rset.getString("申请天数");
tableModel.addRow(arr);
}
applytable.invalidate();
rset.close();
rst.close();
rs.close();
stmt.close();
con.close();
return;
} else
{
rs.close();
stmt.close();
con.close();
return;
}
} else
{
JOptionPane.showMessageDialog(null, "该用户书已达到借书极限或信誉度不够,不能借书!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
} else
{
JOptionPane.showMessageDialog(null, "请重新点选书籍或申请请求!", "提示", JOptionPane.INFORMATION_MESSAGE);
stmt.close();
con.close();
return;
}
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
} else if (s.equals("还回"))
{
if (whichtable == 2)
{
int result = JOptionPane.showConfirmDialog(f, "确定?");
if (result == JOptionPane.OK_OPTION)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
boolean isLonger = false;
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from borrowtable where getdate()>borrowetime and userid=" + superuseridselect + " and bookid=" + superbookidselect);
while (rs.next())
{
isLonger = true;
}
if (isLonger)
{
ResultSet resultSet = stmt.executeQuery("select datediff(day,borrowetime,getdate()) from borrowtable where userid=" + superuseridselect + " and bookid=" + superbookidselect);
resultSet.next();
int longerdays = resultSet.getInt(1);
JOptionPane.showMessageDialog(null, "该用户还书日期超过指定日期" + longerdays + "天,\n扣除信誉度1,罚款" + longerdays / 10 + "元!", "提示", JOptionPane.INFORMATION_MESSAGE);
stmt.execute("update usertable set usercdt=usercdt-1 where userid=" + superuseridselect);
JOptionPane.showMessageDialog(null, "还书成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
} else
{
ResultSet resultSet = stmt.executeQuery("select usercdt from usertable where userid='" + superuseridselect + "'");
resultSet.next();
int borrowusercdt = resultSet.getInt(1);
if (borrowusercdt >= 20)
{
JOptionPane.showMessageDialog(null, "还书成功,该用户信用度已达到最大值!", "提示", JOptionPane.INFORMATION_MESSAGE);
} else
{
stmt.execute("update usertable set usercdt=usercdt+1 where userid=" + superuseridselect);
JOptionPane.showMessageDialog(null, "按期还书成功,信誉度+1!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
resultSet.close();
}
stmt.execute("delete borrowtable where userid=" + superuseridselect + " and bookid=" + superbookidselect);
stmt.execute("update booktable set bookstock=bookstock+1 where bookid=" + superbookidselect);
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.setRowCount(0);
rs = stmt.executeQuery("select bookid as '图书ID',bookname as '书名',isbn as 'ISBN',author as '作者',press as '出版社',pressdate as '出版日期',bookstock as '库存量',borrowtimes as '借阅次数' from booktable");
while (rs.next())
{
String[] arr = new String[8];
arr[0] = rs.getString("图书ID");
arr[1] = rs.getString("书名");
arr[2] = rs.getString("ISBN");
arr[3] = rs.getString("作者");
arr[4] = rs.getString("出版社");
arr[5] = rs.getString("出版日期");
arr[6] = rs.getString("库存量");
arr[7] = rs.getString("借阅次数");
tableModel.addRow(arr);
}
table.invalidate();
tableModel = (DefaultTableModel) borrowtable.getModel();
tableModel.setRowCount(0);
stmt.execute("update borrowtable set borroweddays=datediff(day,borrowstime,getdate())");
ResultSet rst = stmt.executeQuery("select usertable.userid as '用户ID',usertable.useraccount as '用户名',usertable.username as '姓名',booktable.bookid as '图书ID',booktable.bookname as '书名',borrowstime as '借出日期',borrowetime as '归还日期',borroweddays as '已借天数' " + "from usertable,booktable,borrowtable " + "where usertable.userid=borrowtable.userid and booktable.bookid=borrowtable.bookid");
while (rst.next())
{
String[] arr = new String[8];
arr[0] = rst.getString("用户ID");
arr[1] = rst.getString("用户名");
arr[2] = rst.getString("姓名");
arr[3] = rst.getString("图书ID");
arr[4] = rst.getString("书名");
arr[5] = rst.getString("借出日期");
arr[6] = rst.getString("归还日期");
arr[7] = rst.getString("已借天数");
tableModel.addRow(arr);
}
borrowtable.invalidate();
tableModel = (DefaultTableModel) applytable.getModel();
tableModel.setRowCount(0);
stmt.execute("update applyborrowtable set applyeddays=datediff(day,applytime,getdate())");
ResultSet rset = stmt.executeQuery("select * from(select usertable.userid as '用户ID',usertable.useraccount as '用户名',usertable.username as '姓名',booktable.bookid as '图书ID',booktable.bookname as '书名',applytime as '申请日期',applyeddays as '申请天数' from usertable,booktable,applyborrowtable where usertable.userid=applyborrowtable.userid and booktable.bookid=applyborrowtable.bookid) A");
while (rset.next())
{
String[] arr = new String[7];
arr[0] = rset.getString("用户ID");
arr[1] = rset.getString("用户名");
arr[2] = rset.getString("姓名");
arr[3] = rset.getString("图书ID");
arr[4] = rset.getString("书名");
arr[5] = rset.getString("申请日期");
arr[6] = rset.getString("申请天数");
tableModel.addRow(arr);
}
applytable.invalidate();
rset.close();
rst.close();
rs.close();
stmt.close();
con.close();
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
} else
{
JOptionPane.showMessageDialog(null, "请重新点选用户借书信息!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
} else if (s.equals("查询用户"))
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
String useridString = useridselectJTextField.getText().trim();
String useridStringsql = new String();
String useraccountString = useraccountselectJTextField.getText().trim();
String useraccountStringsql = new String();
if (useridString.compareTo("") == 0)
useridStringsql = " 0=0 ";
else
useridStringsql = " A.用户ID='" + useridString + "'";
if (useraccountString.compareTo("") == 0)
useraccountStringsql = " 0=0 ";
else
useraccountStringsql = " A.用户名 like '%" + useraccountString + "%' ";
DefaultTableModel tableModel = (DefaultTableModel) borrowtable.getModel();
tableModel.setRowCount(0);
ResultSet rst = stmt.executeQuery("select * from(select usertable.userid as '用户ID',usertable.useraccount as '用户名',usertable.username as '姓名',booktable.bookid as '图书ID',booktable.bookname as '书名',borrowstime as '借出日期',borrowetime as '归还日期',borroweddays as '已借天数' from usertable,booktable,borrowtable where usertable.userid=borrowtable.userid and booktable.bookid=borrowtable.bookid) A where " + useridStringsql + " and " + useraccountStringsql);
while (rst.next())
{
String[] arr = new String[8];
arr[0] = rst.getString("用户ID");
arr[1] = rst.getString("用户名");
arr[2] = rst.getString("姓名");
arr[3] = rst.getString("图书ID");
arr[4] = rst.getString("书名");
arr[5] = rst.getString("借出日期");
arr[6] = rst.getString("归还日期");
arr[7] = rst.getString("已借天数");
tableModel.addRow(arr);
}
borrowtable.invalidate();
tableModel = (DefaultTableModel) applytable.getModel();
tableModel.setRowCount(0);
stmt.execute("update applyborrowtable set applyeddays=datediff(day,applytime,getdate())");
ResultSet rset = stmt.executeQuery("select * from(select usertable.userid as '用户ID',usertable.useraccount as '用户名',usertable.username as '姓名',booktable.bookid as '图书ID',booktable.bookname as '书名',applytime as '申请日期',applyeddays as '申请天数' from usertable,booktable,applyborrowtable where usertable.userid=applyborrowtable.userid and booktable.bookid=applyborrowtable.bookid) A where " + useridStringsql + " and " + useraccountStringsql);
while (rset.next())
{
String[] arr = new String[7];
arr[0] = rset.getString("用户ID");
arr[1] = rset.getString("用户名");
arr[2] = rset.getString("姓名");
arr[3] = rset.getString("图书ID");
arr[4] = rset.getString("书名");
arr[5] = rset.getString("申请日期");
arr[6] = rset.getString("申请天数");
tableModel.addRow(arr);
}
applytable.invalidate();
useridselectJTextField.setText("");
useraccountselectJTextField.setText("");
useridselectJTextField.requestFocus();
rset.close();
rst.close();
stmt.close();
con.close();
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
} else if (s.equals("查询图书"))
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
String booknameString = booknameselectJTextField.getText().trim();
String booknameStringsql = new String();
String bookidString = bookidselectJTextField.getText().trim();
String bookidStringsql = new String();
if (booknameString.compareTo("") == 0)
booknameStringsql = " 0=0 ";
else
booknameStringsql = " bookname like '%" + booknameString + "%' ";
if (bookidString.compareTo("") == 0)
bookidStringsql = " 0=0 ";
else
bookidStringsql = " bookid='" + bookidString + "' ";
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.setRowCount(0);
ResultSet rs = stmt.executeQuery("select bookid as '图书ID',bookname as '书名',isbn as 'ISBN',author as '作者',press as '出版社',pressdate as '出版日期',bookstock as '库存量',borrowtimes as '借阅次数' from booktable where " + booknameStringsql + " and " + bookidStringsql);
while (rs.next())
{
String[] arr = new String[8];
arr[0] = rs.getString("图书ID");
arr[1] = rs.getString("书名");
arr[2] = rs.getString("ISBN");
arr[3] = rs.getString("作者");
arr[4] = rs.getString("出版社");
arr[5] = rs.getString("出版日期");
arr[6] = rs.getString("库存量");
arr[7] = rs.getString("借阅次数");
tableModel.addRow(arr);
}
table.invalidate();
booknameselectJTextField.setText("");
bookidselectJTextField.setText("");
bookidselectJTextField.requestFocus();
rs.close();
stmt.close();
con.close();
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
} else if (s.equals("您的借阅信息"))
{
new UserBorrowInformationModule(staticsuperuseraccount, staticsuperusertype);
}
}
}
/*图书查询模块*/
package Main;
//import java.awt.*;
//import java.awt.event.*;
import java.awt.BorderLayout;
//import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
//import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//import java.io.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.JOptionPane;
public class InquiryModule extends MouseAdapter implements ActionListener
{
JFrame f = new JFrame("图书查询");
JMenu mAdministrator;
JMenu mBookManager;
JMenu mBorrowManager;
JMenu mUser;
JMenu mOther;
JMenuItem mUserManage;
JMenuItem mBookManage;
JMenuItem mBorrowManage;
JMenuItem mInquiry;
JMenuItem mBorrowInfo;
JMenuItem mUserSetting;
JMenuItem mLogout;
JMenuItem mExitItem;
JMenuItem mAuthor;
JMenuItem mAbout;
JLabel booknameLabel = new JLabel("书 名:");
JLabel authorLabel = new JLabel("作 者:");
JLabel pressLabel = new JLabel("出 版 社:");
JLabel pressdateLabel = new JLabel("出版年份:");
JTextField bookname = new JTextField();
JTextField author = new JTextField();
JTextField press = new JTextField();
JTextField pressdate = new JTextField();
JPanel allmessage = new JPanel();
JPanel leftmessage = new JPanel();
JPanel rightmessage = new JPanel();
JPanel bookname_author_Label = new JPanel();
JPanel bookname_author_TextField = new JPanel();
JPanel press_pressdate_Label = new JPanel();
JPanel press_pressdate_TextField = new JPanel();
JPanel applyinquiryJPanel = new JPanel();
JButton applybookButton = new JButton("申请借阅");
JButton inquiryButton = new JButton("查询");
String[] names;
int i, j, RowNum, ColNum;
Object[][] info;
JTable table;
String staticsuperuseraccount = new String();
String staticsuperusertype = new String();
int superbookid;
public InquiryModule(String superuseraccount, String superusertype)
{
staticsuperuseraccount = superuseraccount;
staticsuperusertype = superusertype;
superbookid = 0;
JMenuBar mBar = new JMenuBar();
mBar.setOpaque(true);
mAdministrator = new JMenu("超级管理员选项");
mBookManager = new JMenu("图书管理员选项");
mBorrowManager = new JMenu("借阅管理员选项");
mUser = new JMenu("用户选项");
mUserManage = new JMenuItem("用户管理");
mBookManage = new JMenuItem("图书管理");
mBorrowManage = new JMenuItem("借阅管理");
mInquiry = new JMenuItem("图书查询");
mBorrowInfo = new JMenuItem("您的借阅信息");
mUserSetting = new JMenuItem("用户信息");
mLogout = new JMenuItem("注销");
mExitItem = new JMenuItem("退出系统");
mOther = new JMenu("其他");
mAuthor = new JMenuItem("作者");
mAbout = new JMenuItem("关于");
if (superusertype.compareTo("超级管理员") == 0)
{
mAdministrator.add(mUserManage);
mAdministrator.add(mBookManage);
mAdministrator.add(mBorrowManage);
mAdministrator.add(mInquiry);
mInquiry.setEnabled(false);
mAdministrator.add(mBorrowInfo);
mAdministrator.add(mUserSetting);
mAdministrator.add(mLogout);
mAdministrator.add(mExitItem);
mOther.add(mAuthor);
mOther.add(mAbout);
mBar.add(mAdministrator);
mBar.add(mOther);
mUserManage.addActionListener(this);
mBookManage.addActionListener(this);
mBorrowManage.addActionListener(this);
mInquiry.addActionListener(this);
mBorrowInfo.addActionListener(this);
mUserSetting.addActionListener(this);
mLogout.addActionListener(this);
mExitItem.addActionListener(this);
mAuthor.addActionListener(this);
mAbout.addActionListener(this);
}
if (superusertype.compareTo("图书管理员") == 0)
{
mBookManager.add(mBookManage);
mBookManager.add(mInquiry);
mInquiry.setEnabled(false);
mBookManager.add(mBorrowInfo);
mBookManager.add(mUserSetting);
mBookManager.add(mLogout);
mBookManager.add(mExitItem);
mOther.add(mAuthor);
mOther.add(mAbout);
mBar.add(mBookManager);
mBar.add(mOther);
mBookManage.addActionListener(this);
mInquiry.addActionListener(this);
mBorrowInfo.addActionListener(this);
mUserSetting.addActionListener(this);
mLogout.addActionListener(this);
mExitItem.addActionListener(this);
mAuthor.addActionListener(this);
mAbout.addActionListener(this);
}
if (superusertype.compareTo("借阅管理员") == 0)
{
mBorrowManager.add(mBorrowManage);
mBorrowManager.add(mInquiry);
mInquiry.setEnabled(false);
mBorrowManager.add(mBorrowInfo);
mBorrowManager.add(mUserSetting);
mBorrowManager.add(mLogout);
mBorrowManager.add(mExitItem);
mOther.add(mAuthor);
mOther.add(mAbout);
mBar.add(mBorrowManager);
mBar.add(mOther);
mBorrowManage.addActionListener(this);
mInquiry.addActionListener(this);
mBorrowInfo.addActionListener(this);
mUserSetting.addActionListener(this);
mLogout.addActionListener(this);
mExitItem.addActionListener(this);
mAuthor.addActionListener(this);
mAbout.addActionListener(this);
}
if (superusertype.compareTo("普通会员") == 0)
{
mUser.add(mInquiry);
mInquiry.setEnabled(false);
mUser.add(mBorrowInfo);
mUser.add(mUserSetting);
mUser.add(mLogout);
mUser.add(mExitItem);
mOther.add(mAuthor);
mOther.add(mAbout);
mBar.add(mUser);
mBar.add(mOther);
mInquiry.addActionListener(this);
mBorrowInfo.addActionListener(this);
mUserSetting.addActionListener(this);
mLogout.addActionListener(this);
mExitItem.addActionListener(this);
mAuthor.addActionListener(this);
mAbout.addActionListener(this);
}
inquiryButton.addActionListener(this);
applybookButton.addActionListener(this);
f.setJMenuBar(mBar);
f.setLayout(new BorderLayout());
allmessage.setLayout(new GridLayout(1, 2));
leftmessage.setLayout(new BorderLayout());
rightmessage.setLayout(new BorderLayout());
bookname_author_Label.setLayout(new GridLayout(2, 1));
bookname_author_TextField.setLayout(new GridLayout(2, 1));
press_pressdate_Label.setLayout(new GridLayout(2, 1));
press_pressdate_TextField.setLayout(new GridLayout(2, 1));
applyinquiryJPanel.setLayout(new GridLayout(1, 2, 3, 3));
bookname_author_Label.add(booknameLabel);
bookname_author_Label.add(authorLabel);
bookname_author_TextField.add(bookname);
bookname_author_TextField.add(author);
press_pressdate_Label.add(pressLabel);
press_pressdate_Label.add(pressdateLabel);
press_pressdate_TextField.add(press);
press_pressdate_TextField.add(pressdate);
leftmessage.add(bookname_author_Label, BorderLayout.WEST);
leftmessage.add(bookname_author_TextField, BorderLayout.CENTER);
rightmessage.add(press_pressdate_Label, BorderLayout.WEST);
rightmessage.add(press_pressdate_TextField, BorderLayout.CENTER);
allmessage.add(leftmessage);
allmessage.add(rightmessage);
applyinquiryJPanel.add(applybookButton);
applyinquiryJPanel.add(inquiryButton);
selectSwing();
bookname.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER)
{
inquiryButton.doClick();
}
}
public void keyReleased(KeyEvent args)
{
}
public void keyTyped(KeyEvent args)
{
}
});
author.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER)
{
inquiryButton.doClick();
}
}
public void keyReleased(KeyEvent args)
{
}
public void keyTyped(KeyEvent args)
{
}
});
press.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER)
{
inquiryButton.doClick();
}
}
public void keyReleased(KeyEvent args)
{
}
public void keyTyped(KeyEvent args)
{
}
});
pressdate.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER)
{
inquiryButton.doClick();
}
}
public void keyReleased(KeyEvent args)
{
}
public void keyTyped(KeyEvent args)
{
}
});
table.addMouseListener(this);
f.add(allmessage, BorderLayout.NORTH);
f.add(applyinquiryJPanel, BorderLayout.SOUTH);
f.add(new JScrollPane(table), BorderLayout.CENTER);
f.setSize(new Dimension(1000, 600));
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension fra = f.getSize();
f.setLocation((screenSize.width - fra.width) / 2, (screenSize.height - fra.height) / 2);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
JOptionPane.showMessageDialog(null, "已经注销!", "提示", JOptionPane.INFORMATION_MESSAGE);
f.dispose();
new LoginModule();
}
});
}
public void selectSwing()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select count(*) from booktable");
rs.next();
RowNum = rs.getInt(1);
rs = stmt.executeQuery("select bookid as '图书ID',bookname as '书名',isbn as 'ISBN',author as '作者',press as '出版社',pressdate as '出版日期',bookstock as '库存量',borrowtimes as '借阅次数' from booktable");
ResultSetMetaData rsmd = rs.getMetaData();
ColNum = rsmd.getColumnCount();
names = new String[ColNum];
for (i = 1; i <= ColNum; i++)
names[i - 1] = rsmd.getColumnName(i);
info = new Object[RowNum][];
i = 0;
while (rs.next())
{
info[i] = new Object[ColNum];
for (j = 1; j <= ColNum; j++)
info[i][j - 1] = rs.getObject(j);
i++;
}
DefaultTableModel model = new DefaultTableModel(info, names);
table = new JTable(model);
table.setPreferredScrollableViewportSize(f.getSize());
} catch (Exception e)
{
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + e.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
public void mouseClicked(MouseEvent e)
{
int i = table.getSelectedRow();
String selectbookid = table.getValueAt(i, 0).toString();
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from booktable where bookid='" + selectbookid + "'");
rs.next();
superbookid = rs.getInt("bookid");
bookname.setText(rs.getString("bookname"));
author.setText(rs.getString("author"));
press.setText(rs.getString("press"));
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("用户管理"))
{
new UserManageModule(staticsuperuseraccount, staticsuperusertype);
f.dispose();
} else if (s.equals("图书管理"))
{
new BookManageModule(staticsuperuseraccount, staticsuperusertype);
f.dispose();
} else if (s.equals("借阅管理"))
{
new BookBorrowModule(staticsuperuseraccount, staticsuperusertype);
f.dispose();
} else if (s.equals("图书查询"))
{
new InquiryModule(staticsuperuseraccount, staticsuperusertype);
f.dispose();
} else if (s.equals("用户信息"))
{
new UserInformationModule(staticsuperuseraccount, staticsuperusertype);
} else if (s.equals("注销"))
{
int result = JOptionPane.showConfirmDialog(f, "你确定要注销吗?");
if (result == JOptionPane.OK_OPTION)
{
f.dispose();
new LoginModule();
} else
return;
} else if (s.equals("退出系统"))
{
System.exit(0);
} else if (s.equals("作者"))
{
JOptionPane.showMessageDialog(null, "/*~Sqrt5~*/", "作者", JOptionPane.INFORMATION_MESSAGE);
} else if (s.equals("关于"))
{
JOptionPane.showMessageDialog(null, "图书管理系统Java版\n(c) Copyright LibraryInformationManagementSystem 2012.\nAll rights reserved.\nVersion 1.0.0", "关于 图书管理系统Java版", JOptionPane.INFORMATION_MESSAGE);
} else if (s.equals("申请借阅"))
{
if (superbookid == 0)
{
JOptionPane.showMessageDialog(null, "请重新选中要借的书籍!", "提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select userid from usertable where useraccount='" + staticsuperuseraccount + "'");
rs.next();
int borrowtouser = rs.getInt("userid");
boolean isBorrowed;
isBorrowed = false;
boolean isApplyed;
isApplyed = false;
rs = stmt.executeQuery("select userid from borrowtable where userid=" + borrowtouser + " and bookid=" + superbookid);
while (rs.next())
{
isBorrowed = true;
}
rs = stmt.executeQuery("select userid from applyborrowtable where userid=" + borrowtouser + " and bookid=" + superbookid);
while (rs.next())
{
isApplyed = true;
}
if (isBorrowed)
{
JOptionPane.showMessageDialog(null, "您已经借过这本书!", "提示", JOptionPane.INFORMATION_MESSAGE);
rs.close();
stmt.close();
con.close();
return;
}
if (isApplyed)
{
JOptionPane.showMessageDialog(null, "您已经申请过这本书!", "提示", JOptionPane.INFORMATION_MESSAGE);
rs.close();
stmt.close();
con.close();
return;
}
rs = stmt.executeQuery("select bookstock from booktable where bookid='" + superbookid + "'");
int bookstock = 0;
rs.next();
bookstock = rs.getInt("bookstock");
if (bookstock < 1)
{
JOptionPane.showMessageDialog(null, "该书已经没有库存!", "提示", JOptionPane.INFORMATION_MESSAGE);
rs.close();
stmt.close();
con.close();
return;
}
int borrownum = 0;
int applynum = 0;
int borrowusercdt = 0;
rs = stmt.executeQuery("select userid from borrowtable where userid='" + borrowtouser + "'");
while (rs.next())
{
borrownum++;
}
rs = stmt.executeQuery("select userid from applyborrowtable where userid='" + borrowtouser + "'");
while (rs.next())
{
applynum++;
}
rs = stmt.executeQuery("select usercdt from usertable where userid='" + borrowtouser + "'");
rs.next();
borrowusercdt = rs.getInt("usercdt");
if (borrownum + applynum + 1 <= borrowusercdt + 5)
{
int result = JOptionPane.showConfirmDialog(f, "确定申请借阅?");
if (result == JOptionPane.OK_OPTION)
{
JOptionPane.showMessageDialog(null, "您可以借" + (borrowusercdt + 5) + "本书\n已借" + borrownum + "本书!\n已申请" + (applynum + 1) + "本书", "提示", JOptionPane.INFORMATION_MESSAGE);
borrownum = 0;
borrowusercdt = 0;
stmt.execute("insert into applyborrowtable(userid,bookid,applytime) values(" + borrowtouser + "," + superbookid + ",getdate())");
stmt.execute("update applyborrowtable set applyeddays=datediff(day,applytime,getdate()) where userid=" + borrowtouser + " and bookid=" + superbookid);
stmt.execute("update booktable set bookstock=bookstock-1 where bookid=" + superbookid);
JOptionPane.showMessageDialog(null, "申请借书成功,请等候借阅管理员处理!", "提示", JOptionPane.INFORMATION_MESSAGE);
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.setRowCount(0);
rs = stmt.executeQuery("select bookid as '图书ID',bookname as '书名',isbn as 'ISBN',author as '作者',press as '出版社',pressdate as '出版日期',bookstock as '库存量',borrowtimes as '借阅次数' from booktable");
while (rs.next())
{
String[] arr = new String[8];
arr[0] = rs.getString("图书ID");
arr[1] = rs.getString("书名");
arr[2] = rs.getString("ISBN");
arr[3] = rs.getString("作者");
arr[4] = rs.getString("出版社");
arr[5] = rs.getString("出版日期");
arr[6] = rs.getString("库存量");
arr[7] = rs.getString("借阅次数");
tableModel.addRow(arr);
}
table.invalidate();
rs.close();
stmt.close();
con.close();
return;
} else
{
rs.close();
stmt.close();
con.close();
return;
}
} else
{
JOptionPane.showMessageDialog(null, "你的信誉度不足,不能申请借书!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
stmt.close();
con.close();
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
} else if (s.equals("查询"))
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
String booknameString = bookname.getText().trim();
String authorString = author.getText().trim();
String pressString = press.getText().trim();
String pressdateString = pressdate.getText().trim();
String booknameStringsql = new String();
String authorStringsql = new String();
String pressStringsql = new String();
String pressdateStringsql = new String();
if (booknameString.compareTo("") == 0)
booknameStringsql = " 0=0 ";
else
booknameStringsql = " bookname like '%" + booknameString + "%' ";
if (authorString.compareTo("") == 0)
authorStringsql = " 0=0 ";
else
authorStringsql = " author like '%" + authorString + "%' ";
if (pressString.compareTo("") == 0)
pressStringsql = " 0=0 ";
else
pressStringsql = " press like '%" + pressString + "%' ";
if (pressdateString.compareTo("") == 0)
pressdateStringsql = " 0=0 ";
else
pressdateStringsql = " pressdate like '%" + pressdateString + "%' ";
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.setRowCount(0);
ResultSet rs = stmt.executeQuery("select bookid as '图书ID',bookname as '书名',isbn as 'ISBN',author as '作者',press as '出版社',pressdate as '出版日期',bookstock as '库存量',borrowtimes as '借阅次数' from booktable where " + booknameStringsql + "and" + authorStringsql + "and" + pressStringsql + "and" + pressdateStringsql);
while (rs.next())
{
String[] arr = new String[8];
arr[0] = rs.getString("图书ID");
arr[1] = rs.getString("书名");
arr[2] = rs.getString("ISBN");
arr[3] = rs.getString("作者");
arr[4] = rs.getString("出版社");
arr[5] = rs.getString("出版日期");
arr[6] = rs.getString("库存量");
arr[7] = rs.getString("借阅次数");
tableModel.addRow(arr);
}
table.invalidate();
rs.close();
stmt.close();
con.close();
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
} else if (s.equals("您的借阅信息"))
{
new UserBorrowInformationModule(staticsuperuseraccount, staticsuperusertype);
}
}
}
/*注册模块*/
package Main;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.*;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class RegisterModule implements ActionListener
{
JFrame f = new JFrame("注册");
ButtonGroup group = new ButtonGroup();
JPanel mainmessageJPanel = new JPanel();
JPanel notemessageJPanel = new JPanel();
JPanel radioJPanel = new JPanel();
JPanel labelPanel = new JPanel();
JPanel ctlPanel = new JPanel();
JPanel eastPanel = new JPanel();
JPanel westPanel = new JPanel();
JPanel northPanel = new JPanel();
JPanel southPanel = new JPanel();
JPanel centerJPanel = new JPanel();
JLabel accountLabel = new JLabel("用户名(*):");
JLabel pwdJLabel = new JLabel("密码(*):");
JLabel setpwdlJLabel = new JLabel("确认密码(*):");
JLabel namelJLabel = new JLabel("姓名:");
JLabel sexJLabel = new JLabel("性别:");
JLabel birthdayJLabel = new JLabel("出生日期:");
JLabel noteJLabel = new JLabel("备注:");
JTextField accountField = new JTextField();
JPasswordField pwdField = new JPasswordField();
JPasswordField setpwdField = new JPasswordField();
JTextField nameField = new JTextField();
JTextField birthdayField = new JTextField();
JTextArea noteField = new JTextArea();
JRadioButton maleButton = new JRadioButton("男", true);
JRadioButton femaleButton = new JRadioButton("女");
JButton resetButton = new JButton("重置");
JButton registerButton = new JButton("注册");
JPanel buttonJPanel = new JPanel();
public RegisterModule()
{
eastPanel.setPreferredSize(new Dimension(20, 150));
westPanel.setPreferredSize(new Dimension(20, 150));
northPanel.setPreferredSize(new Dimension(20, 20));
southPanel.setPreferredSize(new Dimension(20, 20));
f.setLayout(new BorderLayout());
buttonJPanel.setLayout(new GridLayout(1, 2, 5, 5));
centerJPanel.setLayout(new BorderLayout(10, 10));
labelPanel.setLayout(new GridLayout(6, 1, 5, 5));
ctlPanel.setLayout(new GridLayout(6, 1, 5, 5));
radioJPanel.setLayout(new GridLayout(1, 2));
mainmessageJPanel.setLayout(new BorderLayout());
notemessageJPanel.setLayout(new BorderLayout());
buttonJPanel.add(resetButton);
buttonJPanel.add(registerButton);
radioJPanel.add(maleButton);
radioJPanel.add(femaleButton);
labelPanel.add(accountLabel);
labelPanel.add(pwdJLabel);
labelPanel.add(setpwdlJLabel);
labelPanel.add(namelJLabel);
labelPanel.add(sexJLabel);
labelPanel.add(birthdayJLabel);
// labelPanel.add(noteJLabel);
ctlPanel.add(accountField);
ctlPanel.add(pwdField);
ctlPanel.add(setpwdField);
ctlPanel.add(nameField);
ctlPanel.add(radioJPanel);
ctlPanel.add(birthdayField);
// ctlPanel.add(noteField);
mainmessageJPanel.add(labelPanel, BorderLayout.WEST);
mainmessageJPanel.add(ctlPanel, BorderLayout.CENTER);
notemessageJPanel.add(noteJLabel, BorderLayout.NORTH);
notemessageJPanel.add(noteField, BorderLayout.CENTER);
centerJPanel.add(mainmessageJPanel, BorderLayout.NORTH);
centerJPanel.add(notemessageJPanel, BorderLayout.CENTER);
centerJPanel.add(buttonJPanel, BorderLayout.SOUTH);
f.add(eastPanel, BorderLayout.EAST);
f.add(westPanel, BorderLayout.WEST);
f.add(northPanel, BorderLayout.NORTH);
f.add(southPanel, BorderLayout.SOUTH);
f.add(centerJPanel, BorderLayout.CENTER);
maleButton.setActionCommand("男");
femaleButton.setActionCommand("女");
group.add(maleButton);
group.add(femaleButton);
accountField.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER)
{
registerButton.doClick();
}
}
public void keyReleased(KeyEvent args)
{
}
public void keyTyped(KeyEvent args)
{
}
});
pwdField.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER)
{
registerButton.doClick();
}
}
public void keyReleased(KeyEvent args)
{
}
public void keyTyped(KeyEvent args)
{
}
});
setpwdField.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER)
{
registerButton.doClick();
}
}
public void keyReleased(KeyEvent args)
{
}
public void keyTyped(KeyEvent args)
{
}
});
nameField.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER)
{
registerButton.doClick();
}
}
public void keyReleased(KeyEvent args)
{
}
public void keyTyped(KeyEvent args)
{
}
});
birthdayField.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER)
{
registerButton.doClick();
}
}
public void keyReleased(KeyEvent args)
{
}
public void keyTyped(KeyEvent args)
{
}
});
resetButton.addActionListener(this);
registerButton.addActionListener(this);
maleButton.addActionListener(this);
femaleButton.addActionListener(this);
f.setVisible(true);
accountField.requestFocus();
f.setSize(new Dimension(300, 400));
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension fra = f.getSize();
f.setLocation((screenSize.width - fra.width) / 2, (screenSize.height - fra.height) / 2);
}
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("重置"))
{
int result = JOptionPane.showConfirmDialog(f, "确定重置?");
if (result == JOptionPane.OK_OPTION)
{
accountField.setText("");
pwdField.setText("");
setpwdField.setText("");
nameField.setText("");
maleButton.setSelected(true);
birthdayField.setText("");
noteField.setText("");
}
} else if (s.equals("注册"))
{
if (!pwdField.getText().equals(setpwdField.getText()))
{
JOptionPane.showMessageDialog(null, "两次输入的密码不一致,请重新输入!", "提示", JOptionPane.INFORMATION_MESSAGE);
pwdField.setText("");
setpwdField.setText("");
return;
}
if (accountField.getText().trim().equals("") || pwdField.getText().trim().equals("") || setpwdField.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null, "帐户或密码不能为空!", "提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select useraccount from usertable where useraccount='" + accountField.getText().trim() + "'");
boolean isOk;
isOk = true;
while (rs.next())
{
isOk = false;
}
if (!isOk)
{
JOptionPane.showMessageDialog(null, "用户名已经存在!", "提示", JOptionPane.INFORMATION_MESSAGE);
accountField.setText("");
pwdField.setText("");
setpwdField.setText("");
nameField.setText("");
maleButton.setSelected(true);
birthdayField.setText("");
noteField.setText("");
return;
}
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n"+ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
int result = JOptionPane.showConfirmDialog(f, "确定注册?");
if (result == JOptionPane.OK_OPTION)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
stmt.execute("insert into usertable(useraccount,userpwd,username,usersex,userbirth,usernote,userregisterdate) values('" + accountField.getText() + "','" + pwdField.getText() + "','" + nameField.getText() + "','" + group.getSelection().getActionCommand() + "','" + birthdayField.getText() + "','" + noteField.getText() + "',getdate())");
stmt.execute("update usertable set userage=datediff(year,userbirth,getdate()) where useraccount='" + accountField.getText() + "'");
stmt.close();
con.close();
JOptionPane.showMessageDialog(null, "注册成功,按确认键继续!", "注册成功", JOptionPane.INFORMATION_MESSAGE);
f.dispose();
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n"+ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
}
}
}
/*用户信息模块*/
package Main;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;
public class UserInformationModule implements ActionListener
{
String staticsuperuseraccount = new String();
String staticsuperusertype = new String();
JFrame f = new JFrame("用户信息");
JPanel eastJPanel = new JPanel();
JPanel westJPanel = new JPanel();
JPanel northJPanel = new JPanel();
JPanel southJPanel = new JPanel();
JPanel center = new JPanel();
JPanel disableedit_Label = new JPanel();
JPanel disableedit_TextField = new JPanel();
JPanel disableall = new JPanel();
JPanel ableedit_Label = new JPanel();
JPanel ableedit_TextField = new JPanel();
JPanel ableup = new JPanel();
JPanel abledown = new JPanel();
JPanel ableall = new JPanel();
JPanel usersexJPanel = new JPanel();
JLabel useridJLabel = new JLabel("用户ID:");
JLabel useraccountJLabel = new JLabel("用户名:");
JLabel usernameJLabel = new JLabel("姓名:");
JLabel usersexJLabel = new JLabel("性别:");
JLabel useroldpwdJLabel = new JLabel("原密码:");
JLabel userpwdJLabel = new JLabel("新密码:");
JLabel usersetpwdJLabel = new JLabel("确认新密码:");
JLabel userageJLabel = new JLabel("年龄:");
JLabel userregisterdateJLabel = new JLabel("注册时间:");
JLabel userlogdateJLabel = new JLabel("最后登录时间:");
JLabel usercdtJLabel = new JLabel("信誉度:");
JLabel usertypeJLabel = new JLabel("用户类型:");
JLabel userbirthdayJLabel = new JLabel("出生日期:");
JLabel usernoteJLabel = new JLabel("备注:");
JTextField useridJTextField = new JTextField();
JTextField useraccountJTextField = new JTextField();
JTextField usernameJTextField = new JTextField();
JPasswordField useroldpwdJPasswordField = new JPasswordField();
JPasswordField userpwdJPasswordField = new JPasswordField();
JPasswordField usersetpwdJPasswordField = new JPasswordField();
JTextField userageJTextField = new JTextField();
JTextField userregisterdateJTextField = new JTextField();
JTextField userlogdateJTextField = new JTextField();
JTextField usercdtJTextField = new JTextField();
JTextField usertypeJTextField = new JTextField();
JTextField userbirthdayJTextField = new JTextField();
JTextArea usernoteJTextArea = new JTextArea();
JMenuBar mBar = new JMenuBar();
JMenu menuJMenu = new JMenu("选项");
JMenuItem borrowInfoJMenuItem = new JMenuItem("借阅信息");
JMenuItem resetJMenuItem = new JMenuItem("重置");
JMenuItem saveJMenuItem = new JMenuItem("保存");
ButtonGroup group = new ButtonGroup();
JRadioButton maleButton = new JRadioButton("男", true);
JRadioButton femaleButton = new JRadioButton("女");
public UserInformationModule(String superuseraccount, String superusertype)
{
staticsuperuseraccount = superuseraccount;
staticsuperusertype = superusertype;
f.setLayout(new BorderLayout(25, 5));
menuJMenu.add(borrowInfoJMenuItem);
menuJMenu.add(resetJMenuItem);
menuJMenu.add(saveJMenuItem);
mBar.add(menuJMenu);
f.setJMenuBar(mBar);
mBar.setOpaque(true);
maleButton.setActionCommand("男");
femaleButton.setActionCommand("女");
group.add(maleButton);
group.add(femaleButton);
eastJPanel.setPreferredSize(new Dimension(15, 150));
westJPanel.setPreferredSize(new Dimension(15, 150));
northJPanel.setPreferredSize(new Dimension(0, 20));
southJPanel.setPreferredSize(new Dimension(0, 20));
usersexJLabel.setLayout(new GridLayout(1, 2));
disableedit_Label.setLayout(new GridLayout(7, 1, 30, 30));
disableedit_TextField.setLayout(new GridLayout(7, 1, 30, 30));
disableall.setLayout(new BorderLayout());
ableup.setLayout(new BorderLayout());
abledown.setLayout(new BorderLayout());
ableedit_Label.setLayout(new GridLayout(6, 1, 5, 5));
ableedit_TextField.setLayout(new GridLayout(6, 1, 5, 5));
ableall.setLayout(new BorderLayout(5, 5));
center.setLayout(new GridLayout(1, 2, 30, 30));
disableedit_Label.add(useridJLabel);
disableedit_Label.add(useraccountJLabel);
disableedit_Label.add(userageJLabel);
disableedit_Label.add(userregisterdateJLabel);
disableedit_Label.add(userlogdateJLabel);
disableedit_Label.add(usercdtJLabel);
disableedit_Label.add(usertypeJLabel);
disableedit_TextField.add(useridJTextField);
disableedit_TextField.add(useraccountJTextField);
disableedit_TextField.add(userageJTextField);
disableedit_TextField.add(userregisterdateJTextField);
disableedit_TextField.add(userlogdateJTextField);
disableedit_TextField.add(usercdtJTextField);
disableedit_TextField.add(usertypeJTextField);
useridJTextField.setEditable(false);
useraccountJTextField.setEditable(false);
userageJTextField.setEditable(false);
userregisterdateJTextField.setEditable(false);
userlogdateJTextField.setEditable(false);
usercdtJTextField.setEditable(false);
usertypeJTextField.setEditable(false);
usersexJPanel.add(maleButton);
usersexJPanel.add(femaleButton);
ableedit_Label.add(usernameJLabel);
ableedit_Label.add(usersexJLabel);
ableedit_Label.add(userbirthdayJLabel);
ableedit_Label.add(useroldpwdJLabel);
ableedit_Label.add(userpwdJLabel);
ableedit_Label.add(usersetpwdJLabel);
ableedit_TextField.add(usernameJTextField);
ableedit_TextField.add(usersexJPanel);
ableedit_TextField.add(userbirthdayJTextField);
ableedit_TextField.add(useroldpwdJPasswordField);
ableedit_TextField.add(userpwdJPasswordField);
ableedit_TextField.add(usersetpwdJPasswordField);
ableup.add(ableedit_Label, BorderLayout.WEST);
ableup.add(ableedit_TextField, BorderLayout.CENTER);
abledown.add(usernoteJLabel, BorderLayout.NORTH);
abledown.add(usernoteJTextArea, BorderLayout.CENTER);
disableall.add(disableedit_Label, BorderLayout.WEST);
disableall.add(disableedit_TextField, BorderLayout.CENTER);
ableall.add(ableup, BorderLayout.NORTH);
ableall.add(abledown, BorderLayout.CENTER);
center.add(disableall);
center.add(ableall);
maleButton.addActionListener(this);
femaleButton.addActionListener(this);
borrowInfoJMenuItem.addActionListener(this);
resetJMenuItem.addActionListener(this);
saveJMenuItem.addActionListener(this);
f.add(center, BorderLayout.CENTER);
f.add(eastJPanel, BorderLayout.EAST);
f.add(westJPanel, BorderLayout.WEST);
f.add(northJPanel, BorderLayout.NORTH);
f.add(southJPanel, BorderLayout.SOUTH);
reset();
f.setVisible(true);
f.setResizable(false);
f.setSize(new Dimension(650, 470));
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension fra = f.getSize();
f.setLocation((screenSize.width - fra.width) / 2, (screenSize.height - fra.height) / 2);
}
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("借阅信息"))
{
new UserBorrowInformationModule(staticsuperuseraccount, staticsuperusertype);
} else if (s.equals("重置"))
{
int result = JOptionPane.showConfirmDialog(f, "你确定要重置吗?");
if (result == JOptionPane.OK_OPTION)
{
reset();
}
} else if (s.equals("保存"))
{
if (useroldpwdJPasswordField.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null, "请输入原密码!", "提示", JOptionPane.WARNING_MESSAGE);
return;
} else
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select userpwd from usertable where useraccount='" + staticsuperuseraccount + "'");
rs.next();
if (!rs.getString("userpwd").equals(useroldpwdJPasswordField.getText()))
{
JOptionPane.showMessageDialog(null, "原密码错误!", "提示", JOptionPane.WARNING_MESSAGE);
return;
}
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n"+ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
if (!userpwdJPasswordField.getText().equals(usersetpwdJPasswordField.getText()))
{
JOptionPane.showMessageDialog(null, "两次密码不一致!", "提示", JOptionPane.WARNING_MESSAGE);
return;
}
int result = JOptionPane.showConfirmDialog(f, "你确定要修改吗?");
if (result == JOptionPane.OK_OPTION)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
if (!usernameJTextField.getText().equals(""))
{
stmt.execute("update usertable set username='" + usernameJTextField.getText().trim() + "' where useraccount='" + staticsuperuseraccount + "'");
} else
{
JOptionPane.showMessageDialog(null, "姓名为空,姓名将不会被修改!", "提示", JOptionPane.WARNING_MESSAGE);
}
stmt.execute("update usertable set usersex='" + group.getSelection().getActionCommand() + "' where useraccount='" + staticsuperuseraccount + "'");
if (!userbirthdayJTextField.getText().equals(""))
{
stmt.execute("update usertable set userbirth='" + userbirthdayJTextField.getText().trim() + "' where useraccount='" + staticsuperuseraccount + "'");
stmt.execute("update usertable set userage=datediff(year,userbirth,getdate()) where useraccount='" + staticsuperuseraccount + "'");
} else
{
JOptionPane.showMessageDialog(null, "出生日期为空,出生日期将不会被修改!", "提示", JOptionPane.WARNING_MESSAGE);
}
if (!userpwdJPasswordField.getText().equals(""))
{
stmt.execute("update usertable set userpwd='" + userpwdJPasswordField.getText() + "' where useraccount='" + staticsuperuseraccount + "'");
} else
{
JOptionPane.showMessageDialog(null, "新密码为空,密码将不会被修改!", "提示", JOptionPane.WARNING_MESSAGE);
}
reset();
useroldpwdJPasswordField.setText("");
userpwdJPasswordField.setText("");
usersetpwdJPasswordField.setText("");
JOptionPane.showMessageDialog(null, "修改成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n"+ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
}
}
public void reset()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
stmt.execute("update usertable set userage=datediff(year,userbirth,getdate()) where useraccount='" + staticsuperuseraccount + "'");
ResultSet rs = stmt.executeQuery("select * from usertable where useraccount='" + staticsuperuseraccount + "'");
String useridString = "";
String useraccountString = "";
String userageString = "";
String userregisterdateString = "";
String userlogdateString = "";
String usercdtString = "";
String usertypeString = "";
String usernameString = "";
String usersexString = "";
String userbirthdayString = "";
String usernoteString = "";
while (rs.next())
{
useridString = rs.getString("userid");
useraccountString = rs.getString("useraccount");
userageString = rs.getString("userage");
userregisterdateString = rs.getString("userregisterdate");
userlogdateString = rs.getString("userlogdate");
usercdtString = rs.getString("usercdt");
usertypeString = rs.getString("usertype");
usernameString = rs.getString("username");
usersexString = rs.getString("usersex");
userbirthdayString = rs.getString("userbirth");
usernoteString = rs.getString("usernote");
}
useridJTextField.setText(useridString);
useraccountJTextField.setText(useraccountString);
userageJTextField.setText(userageString);
userregisterdateJTextField.setText(userregisterdateString);
userlogdateJTextField.setText(userlogdateString);
usercdtJTextField.setText(usercdtString);
usertypeJTextField.setText(usertypeString);
usernameJTextField.setText(usernameString);
if (usersexString.trim().equals("男"))
maleButton.setSelected(true);
else
femaleButton.setSelected(true);
userbirthdayJTextField.setText(userbirthdayString);
usernoteJTextArea.setText(usernoteString);
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n"+ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
}
/*用户借阅信息模块*/
package Main;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
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.Statement;
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.table.DefaultTableModel;
public class UserBorrowInformationModule extends MouseAdapter implements ActionListener
{
JFrame f = new JFrame("用户借阅信息");
JTable borrowtable = new JTable();
JTable applytable = new JTable();
JLabel borrowbookJLabel = new JLabel("已接书籍:");
JLabel applybookJLabel = new JLabel("申请书籍:");
JPanel borrowbookJPanel = new JPanel();
JPanel applybookJPanel = new JPanel();
JPanel framecenter = new JPanel();
JButton deleteapplyJButton = new JButton("撤销申请");
int superuseridselect;
int superbookidselect;
String[] borrownames;
int borrowi, borrowj, borrowRowNum, borrowColNum;
Object[][] borrowinfo;
String[] applynames;
int applyi, applyj, applyRowNum, applyColNum;
Object[][] applyinfo;
String staticsuperuseraccount = new String();
String staticsuperusertype = new String();
public UserBorrowInformationModule(String superaccount, String superusertype)
{
staticsuperuseraccount = superaccount;
staticsuperusertype = superusertype;
updateborrowSwing();
updateapplySwing();
borrowbookJPanel.setLayout(new BorderLayout());
applybookJPanel.setLayout(new BorderLayout());
borrowbookJPanel.add(borrowbookJLabel, BorderLayout.NORTH);
borrowbookJPanel.add(new JScrollPane(borrowtable), BorderLayout.CENTER);
applybookJPanel.add(applybookJLabel, BorderLayout.NORTH);
applybookJPanel.add(new JScrollPane(applytable), BorderLayout.CENTER);
framecenter.setLayout(new GridLayout(2, 1, 3, 3));
f.setLayout(new BorderLayout());
framecenter.add(borrowbookJPanel);
framecenter.add(applybookJPanel);
borrowtable.addMouseListener(this);
applytable.addMouseListener(this);
deleteapplyJButton.addActionListener(this);
f.add(framecenter, BorderLayout.CENTER);
f.add(deleteapplyJButton, BorderLayout.SOUTH);
f.setVisible(true);
f.setSize(new Dimension(800, 600));
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension fra = f.getSize();
f.setLocation((screenSize.width - fra.width) / 2, (screenSize.height - fra.height) / 2);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
f.dispose();
}
});
}
public void updateborrowSwing()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select userid from usertable where useraccount='" + staticsuperuseraccount + "'");
rs.next();
int borrowtouser = rs.getInt("userid");
rs = stmt.executeQuery("select count(*) from borrowtable where userid=" + borrowtouser);
rs.next();
borrowRowNum = rs.getInt(1);
stmt.execute("update borrowtable set borroweddays=datediff(day,borrowstime,getdate())");
rs = stmt.executeQuery("select * from(select usertable.userid as '用户ID',usertable.useraccount as '用户名',usertable.username as '姓名',booktable.bookid as '图书ID',booktable.bookname as '书名',borrowstime as '借出日期',borrowetime as '归还日期',borroweddays as '已借天数' from usertable,booktable,borrowtable where usertable.userid=borrowtable.userid and booktable.bookid=borrowtable.bookid) A where A.用户名 ='" + staticsuperuseraccount + "'");
ResultSetMetaData rsmd = rs.getMetaData();
borrowColNum = rsmd.getColumnCount();
borrownames = new String[borrowColNum];
for (borrowi = 1; borrowi <= borrowColNum; borrowi++)
borrownames[borrowi - 1] = rsmd.getColumnName(borrowi);
borrowinfo = new Object[borrowRowNum][];
borrowi = 0;
while (rs.next())
{
borrowinfo[borrowi] = new Object[borrowColNum];
for (borrowj = 1; borrowj <= borrowColNum; borrowj++)
borrowinfo[borrowi][borrowj - 1] = rs.getObject(borrowj);
borrowi++;
}
DefaultTableModel model = new DefaultTableModel(borrowinfo, borrownames);
borrowtable = new JTable(model);
borrowtable.setPreferredScrollableViewportSize(f.getSize());
} catch (Exception e)
{
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + e.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
public void updateapplySwing()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select userid from usertable where useraccount='" + staticsuperuseraccount + "'");
rs.next();
int borrowtouser = rs.getInt("userid");
rs = stmt.executeQuery("select count(*) from applyborrowtable where userid=" + borrowtouser);
rs.next();
applyRowNum = rs.getInt(1);
stmt.execute("update applyborrowtable set applyeddays=datediff(day,applytime,getdate())");
rs = stmt.executeQuery("select * from(select usertable.userid as '用户ID',usertable.useraccount as '用户名',usertable.username as '姓名',booktable.bookid as '图书ID',booktable.bookname as '书名',applytime as '申请日期',applyeddays as '申请天数' from usertable,booktable,applyborrowtable where usertable.userid=applyborrowtable.userid and booktable.bookid=applyborrowtable.bookid) A where A.用户名 = '" + staticsuperuseraccount + "' ");
ResultSetMetaData rsmd = rs.getMetaData();
applyColNum = rsmd.getColumnCount();
applynames = new String[applyColNum];
for (applyi = 1; applyi <= applyColNum; applyi++)
applynames[applyi - 1] = rsmd.getColumnName(applyi);
applyinfo = new Object[applyRowNum][];
applyi = 0;
while (rs.next())
{
applyinfo[applyi] = new Object[applyColNum];
for (applyj = 1; applyj <= applyColNum; applyj++)
applyinfo[applyi][applyj - 1] = rs.getObject(applyj);
applyi++;
}
DefaultTableModel model = new DefaultTableModel(applyinfo, applynames);
applytable = new JTable(model);
applytable.setPreferredScrollableViewportSize(f.getSize());
} catch (Exception e)
{
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + e.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
public void mouseClicked(MouseEvent e)
{
if (applytable == e.getComponent())
{
try
{
int applyi = applytable.getSelectedRow();
String selectuserid = applytable.getValueAt(applyi, 0).toString();
String selectbookid = applytable.getValueAt(applyi, 3).toString();
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from applyborrowtable where userid='" + selectuserid + "' and bookid='" + selectbookid + "'");
rs.next();
superuseridselect = rs.getInt("userid");
superbookidselect = rs.getInt("bookid");
System.out.println(superuseridselect);
System.out.println(superbookidselect);
DefaultTableModel tableModel = (DefaultTableModel) borrowtable.getModel();
tableModel.setRowCount(0);
stmt.execute("update borrowtable set borroweddays=datediff(day,borrowstime,getdate())");
ResultSet rst = stmt.executeQuery("select * from(select usertable.userid as '用户ID',usertable.useraccount as '用户名',usertable.username as '姓名',booktable.bookid as '图书ID',booktable.bookname as '书名',borrowstime as '借出日期',borrowetime as '归还日期',borroweddays as '已借天数' from usertable,booktable,borrowtable where usertable.userid=borrowtable.userid and booktable.bookid=borrowtable.bookid) A where A.用户名 ='" + staticsuperuseraccount + "'");
while (rst.next())
{
String[] arr = new String[8];
arr[0] = rst.getString("用户ID");
arr[1] = rst.getString("用户名");
arr[2] = rst.getString("姓名");
arr[3] = rst.getString("图书ID");
arr[4] = rst.getString("书名");
arr[5] = rst.getString("借出日期");
arr[6] = rst.getString("归还日期");
arr[7] = rst.getString("已借天数");
tableModel.addRow(arr);
}
borrowtable.invalidate();
rst.close();
rs.close();
stmt.close();
con.close();
return;
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
if (borrowtable == e.getComponent())
{
superuseridselect = 0;
superbookidselect = 0;
}
}
public void actionPerformed(ActionEvent e)
{
if (superuseridselect != 0 && superbookidselect != 0)
{
int result = JOptionPane.showConfirmDialog(f, "你确定要撤销申请吗?撤销\n后管理员将看不到你的申请!");
if (result == JOptionPane.OK_OPTION)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=LibraryInformationManagementSystem", "sa", "sa");
Statement stmt = con.createStatement();
stmt.execute("delete applyborrowtable where userid=" + superuseridselect + " and bookid=" + superbookidselect);
stmt.execute("update booktable set bookstock=bookstock+1 where bookid=" + superbookidselect);
JOptionPane.showMessageDialog(null, "撤销申请成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
DefaultTableModel tableModel = (DefaultTableModel) applytable.getModel();
tableModel.setRowCount(0);
stmt.execute("update applyborrowtable set applyeddays=datediff(day,applytime,getdate())");
ResultSet rset = stmt.executeQuery("select * from(select usertable.userid as '用户ID',usertable.useraccount as '用户名',usertable.username as '姓名',booktable.bookid as '图书ID',booktable.bookname as '书名',applytime as '申请日期',applyeddays as '申请天数' from usertable,booktable,applyborrowtable where usertable.userid=applyborrowtable.userid and booktable.bookid=applyborrowtable.bookid) A where A.用户名 = '" + staticsuperuseraccount + "'");
while (rset.next())
{
String[] arr = new String[7];
arr[0] = rset.getString("用户ID");
arr[1] = rset.getString("用户名");
arr[2] = rset.getString("姓名");
arr[3] = rset.getString("图书ID");
arr[4] = rset.getString("书名");
arr[5] = rset.getString("申请日期");
arr[6] = rset.getString("申请天数");
tableModel.addRow(arr);
}
applytable.invalidate();
rset.close();
} catch (Exception ex)
{
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "数据库操作出错!\n错误信息:\n" + ex.getMessage(), "错误", JOptionPane.WARNING_MESSAGE);
}
}
} else
{
JOptionPane.showMessageDialog(null, "请重新点选申请书籍!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
}
}
如果有Bug什么的还请指教^_^