javaSwing已经学了一个月了,重最开始的生疏到后来慢慢的熟悉其实这并不是一个短暂的过程,也是十分消耗时间的.菜单页面设计的不是很好,各方面也需要改进,数据库的关联性也不是很强,等有时间了在回来慢慢改。在这里贴一份源码纪念一下。
登录界面
可以直接到GitHub下载源码
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.Random;
class Login1 {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
DataBaseConnect connect = new DataBaseConnect();
// connect.getConnection();
// connect.Insert("黄洪飞","123456");
connect.Insert("胡彩月", "123456");
// connect.Delete("haungg","23132");
Login login = new Login();
login.LoginJFrame();
}
}
class Login extends JFrame implements ActionListener {
//登录、注册、取消
JButton btnLogin, btnRegister, btnCancel;
JPanel pnlSouth, pnlNorth, pnlCenter1, pnlCenter2;//注册面板
private JLabel jLabel1, jlabel;
private JLabel jLabelPassWord;
private JTextField jTextFieldUser, jtextField;
private JPasswordField jPasswordField;
static String user, passWord;
private JPanel imagePanel;
Code code = new Code();
JFrame jFrame = new JFrame("图书管理系统");
// ImageIcon imageIcon=new ImageIcon("E:\\java\\Login.jpg");
//// private ImageIcon background=new ImageIcon("E:\\java\\Login.jpg");;
// JFrame jFrame = new JFrame("图书管理系统");
public void LoginJFrame() {
// JFrame jFrame = new JFrame("图书管理系统");
ImageIcon imageIcon = new ImageIcon("E:\\java\\Login.jpg");
JLabel label = new JLabel(imageIcon);
label.setBounds(0, 0, imageIcon.getIconWidth(), imageIcon.getIconHeight());
jFrame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
JPanel panel = (JPanel) jFrame.getContentPane();
panel.setOpaque(false);
jFrame.setLayout(null);
jTextFieldUser = new JTextField(15);
pnlNorth = new JPanel();
jLabel1 = new JLabel("欢迎进入图书管理系统!");
jLabel1.setFont(new Font("宋体", Font.BOLD, 24));
pnlNorth.add(jLabel1);
pnlNorth.setOpaque(false);
pnlNorth.setBounds(100, 50, 300, 59);
jFrame.add(pnlNorth);
//登录
pnlCenter1 = new JPanel();
pnlCenter2 = new JPanel();
JLabel jLabelUser = new JLabel(" 用 户:");
jLabelUser.setFont(new Font("宋体", Font.BOLD, 16));
jLabelUser.setOpaque(false);
jTextFieldUser = new JTextField(15);
pnlCenter1.add(jLabelUser);
pnlCenter1.add(jTextFieldUser);
pnlCenter1.setOpaque(false);
pnlCenter1.setBounds(100, 100, 300, 60);
jFrame.add(pnlCenter1);
//密码
jLabelPassWord = new JLabel("密 码:");
jLabelPassWord.setFont(new Font("宋体", Font.BOLD, 16));
jPasswordField = new JPasswordField(15);
pnlCenter2.add(jLabelPassWord);
pnlCenter2.add(jPasswordField);
pnlCenter2.setBounds(100, 150, 300, 60);
pnlCenter2.setOpaque(false);
//验证码
jlabel = new JLabel("验证码");
jlabel.setFont(new Font("宋体", Font.BOLD, 16));
jtextField = new JTextField(6);
JPanel jPanelCenter = new JPanel();
jPanelCenter.add(jlabel);
jPanelCenter.add(jtextField);
jPanelCenter.add(code);
jPanelCenter.setBounds(100, 200, 300, 60);
jPanelCenter.setOpaque(false);
jFrame.add(jPanelCenter);
System.out.println(code.generateCode());
//登录
jFrame.add(pnlCenter2);
pnlSouth = new JPanel();
btnLogin = new JButton("登录");
btnLogin.addActionListener(this);
btnRegister = new JButton("注册");
btnRegister.addActionListener(this);
btnCancel = new JButton("取消");
btnCancel.addActionListener(this);
pnlSouth.add(btnLogin);
pnlSouth.add(btnRegister);
pnlSouth.add(btnCancel);
pnlSouth.setOpaque(false);
pnlSouth.setBounds(130, 250, 300, 50);
jFrame.add(pnlSouth);
jFrame.setSize(imageIcon.getIconWidth(), imageIcon.getIconHeight());
jFrame.setVisible(true);
jFrame.setLocation(300, 300);
jFrame.setResizable(false);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public String getUser() {
return this.user;
}
public String getPassWord() {
return this.passWord;
}
@Override
public void actionPerformed(ActionEvent e) {
//登录
DataBaseConnect connect = new DataBaseConnect();
if (e.getSource() == btnLogin) {
String user = jTextFieldUser.getText();
String passWord = jPasswordField.getText();
Login.user = user;
Login.passWord = passWord;
String codes = jtextField.getText();
if (user.equals("")) {
JOptionPane.showMessageDialog(this, "请输入账号");
} else if (user.length() != 0) {
try {
if (connect.Select(user, passWord)) {
if (codes.equals(code.getCode())) {
JOptionPane.showMessageDialog(this, "恭喜您,登陆成功!");
jFrame.setVisible(false);
dispose();
new MenuJFrame();
} else {
JOptionPane.showMessageDialog(this, "输入验证码有误!");
}
} else {
JOptionPane.showMessageDialog(this, "该账号不存在!");
}
} catch (SQLException | ClassNotFoundException ex) {
ex.printStackTrace();
}
}
}
//注册
else if (e.getSource() == btnRegister) {
String user = jTextFieldUser.getText();
String password = jPasswordField.getText();
try {
boolean result = connect.Insert(user, password);
if (result) {
connect.Insert(user, password);
JOptionPane.showMessageDialog(this, "恭喜,注册成功");
// setVisible(false);
// dispose();
// new MenuJFrame();
} else {
JOptionPane.showMessageDialog(this, "该账号已经存在!");
}
} catch (SQLException | ClassNotFoundException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == btnCancel) {
JOptionPane.showMessageDialog(this, "告辞");
System.exit(0);
}
}
}
class Code extends JComponent implements MouseListener {
public static String codes; //自动生成的验证码
private int width, height = 40; //设置验证码高度、宽度
private int codesLength = 4; //设置代码长度
private Random random = new Random(); //生成数字的方法
public Code() {
width = this.codesLength * 16 + (this.codesLength - 1) * 10; //根据验证码长度设置宽度
setPreferredSize(new Dimension(width, height)); //设置背景大小
setSize(width, height); //设置验证码长度和宽度
this.addMouseListener(this);
setToolTipText("点击可更换验证码");
}
//得到生成的验证码
public int getCodesLength() {
return codesLength;
}
//设置验证码的长度
public void setCodesLength(int codeLength) {
if (codesLength < 4) {
this.codesLength = 4;
} else {
this.codesLength = codeLength;
}
}
public String getCode() {
return codes;
}
//让验证码产生随机的颜色
public Color getRandColor(int min, int max) {
if (min > 255)
min = 255;
if (max > 255)
max = 255;
int red = random.nextInt(max - min) + min;
int green = random.nextInt(max - min) + min;
int blue = random.nextInt(max - min) + min;
return new Color(red, green, blue);
}
// 设置验证码具体的数字或字母是什么
protected String generateCode() {
char[] codes = new char[this.codesLength];
for (int i = 0, len = codes.length; i < len; i++) {
if (random.nextBoolean()) {
codes[i] = (char) (random.nextInt(10) + 48);
} else {
codes[i] = (char) (random.nextInt(26) + 97);
}
}
Code.codes = new String(codes);
return Code.codes;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (this.codes == null || this.codes.length() != this.codesLength) { //判断生成的验证码是否为空或超出长度
this.codes = generateCode();
}
width = this.codesLength * 16 + (this.codesLength - 1) * 10;
super.setSize(width, height); //接口使用,验证码字体大小
super.setPreferredSize(new Dimension(width, height));//接口使用,验证码背景大小
Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25); //设置字体和字体大小
g.setFont(mFont); //设置对象
//绘制出验证码的背景的矩形轮廓
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(getRandColor(200, 250));
g2d.fillRect(0, 0, width, height);
g2d.setColor(getRandColor(180, 200));
g2d.drawRect(0, 0, width - 1, height - 1);
//绘制出验证码背景的线
int i = 0, len = 150;
for (; i < len; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int x1 = random.nextInt(width - 10) + 10;
int y1 = random.nextInt(height - 4) + 4;
g2d.setColor(getRandColor(180, 200));
g2d.drawLine(x, y, x1, y1);
}
//绘制出验证码的具体字母
i = 0;
len = this.codesLength;
FontMetrics fm = g2d.getFontMetrics();
int base = (height - fm.getHeight()) / 2 + fm.getAscent();
for (; i < len; i++) {
int b = random.nextBoolean() ? 1 : -1;
g2d.rotate(random.nextInt(10) * 0.01 * b);
g2d.setColor(getRandColor(20, 130));
g2d.drawString(codes.charAt(i) + "", 16 * i + 10, base);
}
}
//下一个验证码
public void nextCode() {
generateCode();
repaint();
;
}
@Override
public void mouseClicked(MouseEvent e) {
codes = generateCode();
repaint();
// nextCode();
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
import java.sql.*;
class DataBaseConnect {
private static final String USER = "root";
private static final String PASS = "123456";
private Connection connection;
private PreparedStatement sql;
private ResultSet resultSet;
Connection getConnection() throws SQLException, ClassNotFoundException {
String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
Class.forName(JDBC_DRIVER);
String DB_URl = "jdbc:mysql://root@localhost:3306/library?characterEncoding=utf8";
connection = DriverManager.getConnection(DB_URl, USER, PASS);
System.out.println("数据库链接成功");
return connection;
}
public void CloseConnection(Connection connection) throws SQLException {
if (connection != null)
connection.close();
}
public boolean Select(String user, String pass) throws SQLException, ClassNotFoundException {
connection = this.getConnection();
sql = connection.prepareStatement("select *from library.db_账户 ");
int flag = 0;
try {
resultSet = sql.executeQuery();
while (resultSet.next()) {
String username = resultSet.getString("username");
String passWord = resultSet.getString("password");
if (user.equals(username) && pass.equals(passWord))
flag = 1;
}
} catch (Exception e) {
e.printStackTrace();
}
if (flag == 1)
return true;
else
return false;
}
public boolean Insert(String user, String password) throws SQLException, ClassNotFoundException {
connection = this.getConnection();
// sql=connection.prepareStatement("insert into library.db_book(ID,username, password) values(?,?,?)");
boolean result = this.Select(user, password);
if (!result) {
sql = connection.prepareStatement("insert into library.db_账户(username, password) values(?,?)");
// sql.setInt(1,0);
sql.setString(1, user);
sql.setString(2, password);
sql.executeUpdate();
return true;
} else {
return false;
}
}
public void Delete(String user, String password) throws SQLException, ClassNotFoundException {
connection = this.getConnection();
boolean result = this.Select(user, password);
if (!result) {
sql = connection.prepareStatement("delete from library.db_账户 where username=?");
sql.setString(1, user);
// sql.setString(2,password);
sql.executeUpdate();
}
}
//更新数据库用户信息
public void UpData(String user, String passWord) throws SQLException, ClassNotFoundException {
connection = this.getConnection();
sql = connection.prepareStatement("update library.db_账户 set username=? where password=?");
sql.setString(1, user);
sql.setString(2, passWord);
sql.executeUpdate();
}
public String AddBook(String bookName, String bookAuthor, String bookTime, int number) throws SQLException, ClassNotFoundException {
connection = this.getConnection();
sql = connection.prepareStatement("insert into library.db_book(BookName, BookAuthor, BookTime, BookNumber) values(?,?,?,?)");
sql.setString(1, bookName);
sql.setString(2, bookAuthor);
sql.setString(3, bookTime);
sql.setInt(4, number);
sql.executeUpdate();
return "添加书籍成功";
}
public boolean SelectBook(String bookName) throws SQLException, ClassNotFoundException {
connection = this.getConnection();
sql = connection.prepareStatement("select *from library.db_book ");
resultSet = sql.executeQuery();
int flag = 0;
while (resultSet.next()) {
String book = resultSet.getString("BookName");
if (bookName.equals(book))
flag = 1;
}
if (flag == 1)
return true;
else
return false;
}
public void DeleteBook(String bookName) throws SQLException, ClassNotFoundException {
connection = this.getConnection();
sql = connection.prepareStatement("delete from library.db_book where BookName=?");
sql.setString(1, bookName);
// sql.setString(2,password);
sql.executeUpdate();
}
}
菜单界面
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Menu {
public static void main(String[] args) {
new MenuJFrame();
}
}
class MenuJFrame extends JFrame implements ActionListener {
private JButton jButton1 = new JButton("增加图书");
private JFrame jFrame = new JFrame("图书菜单窗口");
private JButton jButton2 = new JButton("删除图书");
public MenuJFrame() {
// jFrame.setSize(600, 600);
jFrame.setLocationRelativeTo(null);
ImageIcon imageIcon = new ImageIcon("E:\\java\\Menu.gif");
JLabel label = new JLabel(imageIcon);
label.setBounds(0, 0, imageIcon.getIconWidth(), imageIcon.getIconHeight());
jFrame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
JPanel panel = (JPanel) jFrame.getContentPane();
panel.setOpaque(false);
jFrame.setSize(imageIcon.getIconWidth(), imageIcon.getIconHeight());
jFrame.setResizable(false);
jFrame.setLocation(300,300);
// jFrame.setLayout(null);
JPanel jPanel1 = new JPanel(new BorderLayout());
//创建面板
jPanel1.setOpaque(false);
JTabbedPane tabbedPane = new JTabbedPane();
JToolBar toolBar1 = new JToolBar();
toolBar1.setOpaque(false);
toolBar1.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 10));
JToolBar jToolBar2 = new JToolBar();
jToolBar2.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 10));
jToolBar2.setOpaque(false);
JToolBar jToolBar3 = new JToolBar();
jToolBar3.setOpaque(false);
jToolBar3.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 10));
JToolBar jToolBar4 = new JToolBar();
jToolBar4.setOpaque(false);
jToolBar4.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 10));
tabbedPane.add("图书管理", toolBar1);
tabbedPane.setOpaque(false);
jButton1.setOpaque(false);
jButton1.setFont(new Font("宋体", Font.BOLD, 16));
jButton2.setFont(new Font("宋体", Font.BOLD, 16));
// JButton jButton1 = new JButton("增加图书");
// JButton jButton2 = new JButton("删除图书");
toolBar1.add(jButton1);
jButton1.addActionListener(this);
jButton2.addActionListener(this);
toolBar1.add(jButton2);
tabbedPane.add("借书", jToolBar2);
JButton jButton3 = new JButton("借书");
jButton3.setFont(new Font("宋体", Font.BOLD, 16));
jButton3.addActionListener(this);
JButton jButton4 = new JButton("查看书籍目录");
jButton4.setFont(new Font("宋体", Font.BOLD, 16));
jButton4.addActionListener(this);
jToolBar2.add(jButton3);
jToolBar2.add(jButton4);
tabbedPane.add("还书", jToolBar3);
JButton jButton5 = new JButton("还书");
jButton5.setFont(new Font("宋体", Font.BOLD, 16));
jButton5.addActionListener(this);
JButton jButton6 = new JButton("选项6");
jToolBar3.add(jButton5);
jToolBar3.add(jButton6);
tabbedPane.add("用户信息管理", jToolBar4);
JButton jButton7 = new JButton("个人信息管理");
jButton7.setFont(new Font("宋体", Font.BOLD, 16));
JButton jButton8 = new JButton("完善用户其它信息");
jButton8.setFont(new Font("宋体", Font.BOLD, 16));
jToolBar4.add(jButton7);
jToolBar4.add(jButton8);
jFrame.getContentPane().add(tabbedPane, BorderLayout.NORTH);
jFrame.getContentPane().add(jPanel1, BorderLayout.CENTER);
tabbedPane.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
System.out.println("当前选项卡:" + tabbedPane.getSelectedIndex());
}
});
tabbedPane.setSelectedIndex(1);
jFrame.setVisible(true);
//借书按钮监听器
jButton3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jFrame.setVisible(false);
dispose();
BorrowBook borrowBook = new BorrowBook();
borrowBook.Remind();
}
});
//还书按钮监听器
jButton5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jFrame.setVisible(false);
dispose();
ReturnBook returnBook = new ReturnBook();
returnBook.returnBook();
}
});
//个人 用户管理监听器
jButton7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jFrame.setVisible(false);
dispose();
UserManage userManage = new UserManage();
userManage.User();
}
});
//完善用户信息监听器
jButton8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jFrame.setVisible(false);
dispose();
UserInformation userInformation = new UserInformation();
userInformation.Information();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jButton1) {
jFrame.setVisible(false);
dispose();
// dispose();
// JFrame jFrame1 = new JFrame("新窗口");
// jFrame1.setVisible(true);
BookManege book = new BookManege();
book.addBook();
} else if (e.getSource() == jButton2) {
jFrame.setVisible(false);
dispose();
BookManege book = new BookManege();
book.DeleteBook();
}
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DataBase1 extends DataBaseConnect {
private DataBaseConnect connect = new DataBaseConnect();
//判断书籍是否存在
public boolean Select(String bookName) throws SQLException, ClassNotFoundException {
connect.getConnection();
PreparedStatement sql;
Connection connection = this.getConnection();
sql = connection.prepareStatement("select *from library.db_book");
ResultSet resultSet;
resultSet = sql.executeQuery();
int flag = 0;
while (resultSet.next()) {
String book = resultSet.getString("BookName");
if (bookName.equals(book)) {
flag = 1;
}
}
if (flag == 1)
return true;
else
return false;
}
//查找书籍剩余情况
public int Select(String bookName, int flag) throws SQLException, ClassNotFoundException {
connect.getConnection();
PreparedStatement sql;
Connection connection = this.getConnection();
sql = connection.prepareStatement("select *from library.db_book ");
ResultSet resultSet;
resultSet = sql.executeQuery();
int number = 0;
while (resultSet.next()) {
String book = resultSet.getString("BookName");
int bookNumber = resultSet.getInt("BookNumber");
if (bookName.equals(book))
number = bookNumber;
}
return number;
}
//更新图书馆书籍数目
public void UpDate(String bookName, int flag) throws SQLException, ClassNotFoundException {
connect.getConnection();
PreparedStatement sql;
Connection connection = this.getConnection();
sql = connection.prepareStatement("update library.db_book set BookNumber=? where BookName=?");
String number;
number = String.valueOf(Select(bookName, flag));
if (flag == 1)
sql.setInt(1, Integer.parseInt(number) - 1);
else if (flag == 2)
sql.setInt(1, Integer.parseInt(number) + 1);
sql.setString(2, bookName);
sql.executeUpdate();
}
//记得更新借书时间
public void addBorrowName(String bookName, String borrowName, String borrowTime, String returnTime) throws SQLException, ClassNotFoundException {
Connection connection = connect.getConnection();
PreparedStatement sql;
sql = connection.prepareStatement("insert into library.db_borrow(BookName, BorrowName,BorrowTime,ReturnTime)values (?,?,?,?)");
sql.setString(1, bookName);
sql.setString(2, borrowName);
sql.setString(3, borrowTime);
sql.setString(4, returnTime);
sql.executeUpdate();
}
//还书记录
public void returnBook(String borrowName) throws SQLException, ClassNotFoundException {
Connection connection = connect.getConnection();
PreparedStatement sql;
sql = connection.prepareStatement("delete from library.db_borrow where BorrowName=?");
sql.setString(1, borrowName);
sql.executeUpdate();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class BookManege extends JFrame implements ActionListener {
private JButton jButton1 = new JButton("增加");
private JButton jButton2 = new JButton("返回上一级目录");
private JTextField jTextField1, jTextField2, jTextField3;
void addBook() {
this.setTitle("图书管理");
// JButton jButton1 = new JButton("增加");
jButton1.addActionListener(this);
// JButton jButton2 = new JButton("返回上一级目录");
jButton2.addActionListener(this);
JLabel jLabel1 = new JLabel("图书名称");
jTextField1 = new JTextField(15);
JLabel jLabel2 = new JLabel("图书作者");
jTextField2 = new JTextField(15);
JLabel jLabel3 = new JLabel("入库时间");
jTextField3 = new JTextField(15);
JPanel jPanelNorth = new JPanel();
JPanel jPanelCenter1 = new JPanel();
JPanel jPanelCenter2 = new JPanel();
JPanel jPanelCenter = new JPanel();
JPanel jPanelSouth = new JPanel();
// jPanelNorth.setLayout(new BorderLayout());
jPanelNorth.add(jLabel1);
jPanelNorth.add(jTextField1);
jPanelCenter1.add(jLabel2);
jPanelCenter1.add(jTextField2);
jPanelCenter2.add(jLabel3);
jPanelCenter2.add(jTextField3);
jPanelCenter.add(jPanelCenter1, BorderLayout.CENTER);
jPanelCenter.add(jPanelCenter2, BorderLayout.SOUTH);
// this.add(jPanelNorth,BorderLayout.NORTH);
jPanelSouth.add(jButton1);
jPanelSouth.add(jButton2);
this.add(jPanelNorth, BorderLayout.NORTH);
this.add(jPanelCenter, BorderLayout.CENTER);
this.add(jPanelSouth, BorderLayout.SOUTH);
this.setSize(400, 200);
this.setVisible(true);
}
void DeleteBook() {
JFrame jFrame = new JFrame("删除图书");
JButton jButton = new JButton("删除");
jButton.addActionListener(this);
JButton jButton1 = new JButton("返回上一级目录");
jButton1.addActionListener(this);
JLabel jLabel = new JLabel("书籍名称");
JTextField jTextField = new JTextField(15);
JPanel jPanel = new JPanel();
JPanel jPanel1 = new JPanel();
jPanel1.add(jButton);
jPanel1.add(jButton1);
jPanel.add(jLabel);
jPanel.add(jTextField);
this.add(jPanel, BorderLayout.NORTH);
this.add(jPanel1, BorderLayout.CENTER);
setVisible(true);
setSize(500, 400);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String bookName;
bookName = jTextField.getText();
DataBaseConnect connect = new DataBaseConnect();
try {
boolean result = connect.SelectBook(bookName);
if (result) {
connect.DeleteBook(bookName);
JOptionPane.showMessageDialog(jFrame, "删除成功");
} else {
JOptionPane.showMessageDialog(jFrame, "图书馆没有书籍信息");
}
} catch (SQLException | ClassNotFoundException ex) {
ex.printStackTrace();
}
}
});
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
new MenuJFrame();
}
});
}
//增加图书监听器
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jButton1) {
//增加书到数据库db_Book
String bookName, bookAuthor, bookTime;
bookName = jTextField1.getText();
bookAuthor = jTextField2.getText();
bookTime = jTextField3.getText();
DataBaseConnect connect = new DataBaseConnect();
try {
String s = connect.AddBook(bookName, bookAuthor, bookTime, 1);
JOptionPane.showMessageDialog(this, s);
} catch (SQLException | ClassNotFoundException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == jButton2) {
setVisible(false);
dispose();
new MenuJFrame();
}
}
// public boolean Select(String bookName) {
//
// return true;
// }
public static void main(String[] args) {
BookManege book = new BookManege();
// book.addBook();
book.DeleteBook();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class ReturnBook extends JFrame implements ActionListener {
public void returnBook() {
JFrame returnJFrame = new JFrame("还书");
JPanel jPanelNorth = new JPanel(), jPanelCenter = new JPanel(), jPanelSouth = new JPanel(), jPanel = new JPanel();
JPanel jPanelCenter1 = new JPanel(), jPanel1Center2 = new JPanel();
JButton jButton = new JButton("还书");
JButton jButton1 = new JButton("返回上一级目录");
JLabel jLabel1 = new JLabel("书籍名称:");
JTextField jTextField1 = new JTextField(15);
jPanelNorth.add(jLabel1);
jPanelNorth.add(jTextField1);
JLabel jLabel2 = new JLabel("还书人:");
JTextField jTextField2 = new JTextField(15);
jPanelCenter1.add(jLabel2);
jPanelCenter1.add(jTextField2);
JLabel jLabel3 = new JLabel("归还时间:");
JTextField jTextField3 = new JTextField(15);
jPanel1Center2.add(jLabel3);
jPanel1Center2.add(jTextField3);
jPanelCenter.add(jPanelCenter1, BorderLayout.NORTH);
jPanelCenter.add(jPanel1Center2, BorderLayout.CENTER);
jPanelSouth.add(jButton);
jPanelSouth.add(jButton1);
returnJFrame.add(jPanelNorth, BorderLayout.NORTH);
returnJFrame.add(jPanelCenter, BorderLayout.CENTER);
returnJFrame.add(jPanelSouth, BorderLayout.SOUTH);
returnJFrame.setSize(500, 200);
returnJFrame.setLocation(500, 500);
returnJFrame.setResizable(false);
returnJFrame.setVisible(true);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String bookName = jTextField1.getText();
String borrowName = jTextField2.getText();
String borrowTime = jTextField3.getText();
DataBase1 dataBase1 = new DataBase1();
try {
dataBase1.returnBook(borrowName);
dataBase1.UpDate(bookName, 2);
JOptionPane.showMessageDialog(returnJFrame, "还书成功!");
} catch (SQLException | ClassNotFoundException ex) {
ex.printStackTrace();
}
}
});
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
returnJFrame.setVisible(false);
dispose();
new MenuJFrame();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class userDateBase extends DataBaseConnect {
DataBaseConnect dataBaseConnect = new DataBaseConnect();
private Connection connection;
private PreparedStatement sql;
private ResultSet resultSet;
public boolean Select(String username) throws SQLException, ClassNotFoundException {
connection = this.getConnection();
sql = connection.prepareStatement("select *from library.db_账户");
int flag = 0;
try {
resultSet = sql.executeQuery();
while (resultSet.next()) {
String user = resultSet.getString("username");
if (user.equals(username))
flag = 1;
}
} catch (Exception e) {
e.printStackTrace();
}
if (flag == 0)
return true;
else
return false;
}
public boolean Insert(String user, String date, String email) throws SQLException, ClassNotFoundException {
connection = this.getConnection();
boolean result = Select(user);
if (result) {
sql = connection.prepareStatement("insert into library.db_userinformation(username, birthday, Email) values (?,?,?)");
sql.setString(1, user);
sql.setString(2, date);
sql.setString(3, email);
sql.executeUpdate();
return true;
} else {
return false;
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class UserInformation extends JFrame implements ActionListener {
Login login = new Login();
String user = login.getUser();
String password = login.getPassWord();
private JFrame userJFrame = new JFrame("个人信息");
private JLabel jLabel1 = new JLabel("用户名:");
private JTextField jTextField1 = new JTextField(user, 15);
private JLabel jLabel2 = new JLabel("出生日期:");
private JTextField jTextField2 = new JTextField(15);
private JLabel jLabel3 = new JLabel("电子邮件:");
private JTextField jTextField3 = new JTextField(15);
private JPanel jPanel;
private JButton jButton1 = new JButton("提交");
private JButton jButton2 = new JButton("返回主菜单");
// Login login=new Login();
public void Information() {
Init();
// String user = login.getUser();
// String password = login.getPassWord();
JPanel jPanelNorth = new JPanel();
jPanelNorth.add(jLabel1);
jPanelNorth.add(jTextField1);
userJFrame.add(jPanelNorth, BorderLayout.NORTH);
JPanel jPanelCenter1 = new JPanel();
jPanelCenter1.add(jLabel2);
jPanelCenter1.add(jTextField2);
JPanel jPanelCenter = new JPanel();
jPanelCenter.add(jPanelCenter1, BorderLayout.NORTH);
JPanel jPanelCenter2 = new JPanel();
jPanelCenter2.add(jLabel3);
jPanelCenter2.add(jTextField3);
jPanelCenter.add(jPanelCenter2, BorderLayout.SOUTH);
userJFrame.add(jPanelCenter, BorderLayout.CENTER);
JPanel jPanelSouth = new JPanel();
jPanelSouth.add(jButton1);
jPanelSouth.add(jButton2);
userJFrame.add(jPanelSouth, BorderLayout.SOUTH);
jButton1.addActionListener((ActionListener) this);
jButton2.addActionListener((ActionListener) this);
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = jTextField1.getText();
String data = jTextField2.getText();
String email = jTextField3.getText();
userDateBase userDateBase = new userDateBase();
try {
boolean result = userDateBase.Insert(username, data, email);
if (result) {
JOptionPane.showMessageDialog(userJFrame, "修改成功");
} else {
JOptionPane.showMessageDialog(userJFrame, "该用户名已经存在,请修改用户名");
}
} catch (SQLException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}
});
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
userJFrame.setVisible(false);
dispose();
MenuJFrame menuJFrame = new MenuJFrame();
}
});
}
public void Init() {
userJFrame.setSize(400, 200);
userJFrame.setLocation(500, 500);
userJFrame.setVisible(true);
userJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
UserInformation userInformation = new UserInformation();
userInformation.Information();
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
也不知道有没有贴完,因为是个联系所以烂尾了