本项目为Java swing项目,在工作环境中基本使用不到,但是很多学校把这个当做编程入门的项目来做,故分享出本项目供初学者参考。

CSDN赞助下载:https://download.csdn.net/download/weixin_44893902/20367467

一、效果演示:

主要功能:

①基本数据维护:
图书类别管理 >> 图书类别添加、图书类别维护
图书管理 >> 图书添加、图书维护
②关于我们

1、登录界面

2、主界面:

3、图书类别维护

4、图书类别添加

5、图书维护

6、图书添加

7、关于我们


可全部缩小到左下角

二、核心代码:

1、Util包 【存放数据库连接工具】

① DBTool(数据库连接工具类)

package cn.ac.azure.util;import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;/*** 数据库连接工具类* @author **/
public class DBTool {private static String driver;  //数据库驱动private static String url;  //数据库连接地址private static String user; //数据库连接用户private static String password;  //数据库连接密码static{//新建一个properties,用于读取db.properties配置文件Properties p=new Properties();//新建一个字符串,保存配置文件的路径String path="cn//ac//azure//util//db.properties";try {//调用Properties.load通过类加载获得配置文件的输入流p.load(DBTool.class.getClassLoader().getResourceAsStream(path));//读取配置文件中的配置参数driver=p.getProperty("driver");  //获取驱动url=p.getProperty("url");  //获取数据库连接地址user=p.getProperty("user");  //获取数据库用户password=p.getProperty("password");  //获取数据库密码try {//加载数据库驱动类到程序中Class.forName(driver);} catch (ClassNotFoundException e) {e.printStackTrace();throw new RuntimeException("加载驱动失败",e);}} catch (IOException e) {e.printStackTrace();throw new RuntimeException("找不到配置文件",e);}}/*** 获取数据库连接* @return 数据库连接对象* @throws SQLException 提醒调用者捕获异常,并在finally中关闭关闭异常*/public static Connection getConnetion() throws SQLException{//通过DriverManager获得数据库连接return DriverManager.getConnection(url, user, password);}/*** 关闭数据库连接* @param con*/public static void close(Connection con){if(con!=null){ //如果数据连接不为空try {//关闭数据库连接con.close();} catch (SQLException e) {e.printStackTrace();throw new RuntimeException("数据库关闭失败",e);}}}
//  /**
//   * 测试数据库连接工具是否可用
//   * @param args
//   */
//  public static void main(String[] args) {
//      Connection con=null;
//      try {
//          con=DBTool.getConnetion();
//          System.out.println("数据库连接成功!");
//      } catch (SQLException e) {
//          System.out.println("数据库连接失败!");
//          e.printStackTrace();
//      }finally{
//          DBTool.close(con);
//      }
//  }
}

② db.properties(配置文件)

2、model包 【存放实体类】

① Book(图书实体类)

 package cn.ac.azure.model;
/*** 图书实体* @author **/
public class Book {private Integer id;  //图书idprivate String bookName;  //图书名称private String author;  //图书作者private String sex;     //作者性别private Float price;    //图书价格private Integer bookTypeId;  //图书类别IDprivate String bookTypeName;  //图书类别名称private String bookDesc;  //图书描述public Book() {super();}public Book(Integer id, String bookName, String author, String sex, Float price, Integer bookTypeId,String bookTypeName, String bookDesc) {super();this.id = id;this.bookName = bookName;this.author = author;this.sex = sex;this.price = price;this.bookTypeId = bookTypeId;this.bookTypeName = bookTypeName;this.bookDesc = bookDesc;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getBookName() {return bookName;}public void setBookName(String bookName) {this.bookName = bookName;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Float getPrice() {return price;}public void setPrice(Float price) {this.price = price;}public Integer getBookTypeId() {return bookTypeId;}public void setBookTypeId(Integer bookTypeId) {this.bookTypeId = bookTypeId;}public String getBookTypeName() {return bookTypeName;}public void setBookTypeName(String bookTypeName) {this.bookTypeName = bookTypeName;}public String getBookDesc() {return bookDesc;}public void setBookDesc(String bookDesc) {this.bookDesc = bookDesc;}@Overridepublic String toString() {return "Book [测试=" + id + ", bookName=" + bookName + ", author=" + author + ", sex=" + sex + ", price=" + price+ ", bookTypeId=" + bookTypeId + ", bookTypeName=" + bookTypeName + ", bookDesc=" + bookDesc + "]";}}

② BookType(图书类别实体类)

package cn.ac.azure.model;
/*** 图书类别实体* @author **/
public class BookType {private int id;  //定义IDprivate String bookTypeName;  //定义图书类别名称private String bookTypeDesc;  //定义图书类别描述//无参构造器public BookType() {}//有参构造函数public BookType(String bookTypeName, String bookTypeDesc) {super();this.bookTypeName = bookTypeName;this.bookTypeDesc = bookTypeDesc;}public BookType(int id, String bookTypeName, String bookTypeDesc) {super();this.id = id;this.bookTypeName = bookTypeName;this.bookTypeDesc = bookTypeDesc;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getBookTypeName() {return bookTypeName;}public void setBookTypeName(String bookTypeName) {this.bookTypeName = bookTypeName;}public String getBookTypeDesc() {return bookTypeDesc;}public void setBookTypeDesc(String bookTypeDesc) {this.bookTypeDesc = bookTypeDesc;}@Overridepublic String toString() {return "BookType [id=" + id + ", bookTypeName=" + bookTypeName + ", bookTypeDesc=" + bookTypeDesc + "]";}
}

③ User(用户实体类)

package cn.ac.azure.model;
/*** 用户实体* @author **/
public class User {private int id;              //用户idprivate String username;     //用户名称private String password;     //用户密码public User() {}@Overridepublic String toString() {return "User [id=" + id + ", username=" + username + ", password=" + password + "]";}public User(String username, String password) {super();this.username = username;this.password = password;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}

3、Dao包 【数据库访问层】

① BookDao(图书dao类)

package cn.ac.azure.dao;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import cn.ac.azure.model.Book;/*** 图书dao类* @author **/
public class BookDao {/*** 图书添加* @param con 数据库库连接对象* @param book 添加的图书对象* @return 返回添加操作的数据库记录数* @throws SQLException 抛出数据库异常*/public int add(Connection con,Book book) throws SQLException{//拼写sql插入语句String sql="insert into t_book values(null,'"+book.getBookName()+"','"+book.getAuthor()+"','"+book.getSex()+"','"+book.getPrice()+"','"+book.getBookTypeId()+"','"+book.getBookTypeName()+"','"+book.getBookDesc()+"')";System.out.println(sql);//获得预编译对象psPreparedStatement ps=con.prepareStatement(sql);//设置ps参数/*ps.setString(1,book.getBookName());  //设置图书名称ps.setString(2,book.getAuthor());    //设置图书作者ps.setString(3, book.getSex());      //设置作者性别ps.setFloat(4, book.getPrice());     //设置图书价格ps.setInt(5, book.getBookTypeId());  //设置图书类别IDps.setString(6, book.getBookDesc()); //设置图书描述
*/      //执行sql语句,并返回插入的记录数return ps.executeUpdate();}/*** 图书查询* @param con 数据库连接对象* @param book 图书对象* @return 查询的结果集* @throws SQLException 抛出数据库异常*/public ResultSet search(Connection con,Book book) throws SQLException{//定义图书名称String bookName=null;//定义图书作者String author=null;//定义图书类别名称String bookTypeName=null;//如果图书不为空的话,就为前三个字段赋值,按照条件查询if(book!=null){bookName=book.getBookName();author=book.getAuthor();bookTypeName=book.getBookTypeName();}//定义一个字符串缓冲对象类存储查询添加StringBuilder sb=new StringBuilder("select * from t_book b , t_bookType bt where b.bookTypeId=bt.id ");//查询条件有图书名称if(!(bookName==null || "".equals(bookName.trim()))){sb.append("and b.bookName like '%"+bookName+"%' ");}//查询条件有图书作者if(!(author==null || "".equals(author.trim()))){sb.append("and b.author like '%"+author+"%' ");}//查询条件有图书类别名称if(!(bookTypeName==null || "".equals(bookTypeName.trim()))){sb.append("and bt.bookTypeName like '%"+bookTypeName+"%' ");}//从sb生成sql语句String sql=sb.toString();//获取预处理对象PreparedStatement ps=con.prepareStatement(sql);//执行查询,并返回结果集return ps.executeQuery();}/*** 图书修改* @param con 数据库连接对象* @param book 修改的图书对象* @return 返回修改改变的记录数* @throws SQLException 抛出数据库异常,同时提醒调用者关闭数据库*/public int update(Connection con,Book book) throws SQLException{//编写sql语句String sql="update t_book set bookName=?,author=?,sex=?,"+ "price=?,bookTypeId=?,bookDesc=? where id=?";//获取预编译对象psPreparedStatement ps=con.prepareStatement(sql);//设置ps对象 ps.setString(1, book.getBookName()); //图书名称ps.setString(2, book.getAuthor());   //图书作者ps.setString(3,book.getSex()); //作者性别ps.setFloat(4, book.getPrice()); //图书价格ps.setInt(5, book.getBookTypeId());  //图书类型Idps.setString(6, book.getBookDesc()); //图书描述ps.setInt(7, book.getId());//执行修改并返回改变的记录数return ps.executeUpdate(); }/*** 图书删除* @param con 数据库连接对象* @param id 删除图书的id* @return 返回删除的记录数* @throws SQLException 抛出数据库异常,同时提醒调用者关闭数据库*/public int delete(Connection con,int id) throws SQLException{//编写sql语句String sql="delete from t_book where id=?";//获取预编译对象psPreparedStatement ps=con.prepareStatement(sql);//设置ps参数ps.setInt(1, id);//执行DML删除语句并返回删除的记录数return ps.executeUpdate();}
}

② BookTypeDao(图书类别dao类)

package cn.ac.azure.dao;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import cn.ac.azure.model.BookType;/*** 图书类别dao类* @author **/
public class BookTypeDao {/*** 图书类别添加* @param con 数据库连接对象* @param bookType 要添加的图书类别* @return 返回数据库操作的记录数* @throws SQLException */public int add(Connection con,BookType bookType) throws SQLException{//拼写sql插入语句String sql="insert into t_bookType values(null,?,?)";//创建预编译对象psPreparedStatement ps=con.prepareStatement(sql);//设置ps参数ps.setString(1, bookType.getBookTypeName()); //设置bookTypeNameps.setString(2, bookType.getBookTypeDesc()); //设置bookTypeDesc//返回数据库操作的记录数return ps.executeUpdate();}/*** 图书类别查询* @param con 数据库连接对象* @param bookType 查询的图书类别* @return 返回查询的结果集* @throws SQLException 抛出数据库异常 */public ResultSet search(Connection con,BookType bookType) throws SQLException{/** 思路:当jdbc查询数据库有多个条件从外部输入时,这是最好创建一个字符串缓冲类来添加条件到sql语句中。* 同时,不知道有哪些条件是第一条件,无法确定where关键字的所在,于是添加条件都用(and 条件)* 最后字符串转换成字符串时在将第一个and替换成where*///定义一个图书类别名称String bookTypeName=null;if(bookType!=null){ //如果类别对象不为空的话,就获取它的类别名称bookTypeName=bookType.getBookTypeName();}//创建一个字符串缓冲类StringBuilder sb=new StringBuilder("select * from t_bookType");//添加查询语句的条件(and + 条件)if(!(bookTypeName==null || "".equals(bookTypeName.trim()))){//jdbc中,数据库字符串要用单引号括起来sb.append(" and bookTypeName like '%"+bookType.getBookTypeName()+"%'");}//将字符串缓冲对象转换成字符串,同时把第一个and替换成whereString sql=sb.toString().replaceFirst("and", "where");   //获取预编译对象PreparedStatement ps=con.prepareStatement(sql);//返回ps执行查询之后的结果集return ps.executeQuery();}/*** 图书类别修改* @param con 数据路连接对象* @param bookType 要修改的图书类别* @return 返回数据库更新的记录数* @throws SQLException 抛出数据库异常*/public int update(Connection con,BookType bookType) throws SQLException{//拼写sql更新语句String sql="update t_bookType set bookTypeName=? , bookTypeDesc=? where id=?";//获取预编译对象psPreparedStatement ps=con.prepareStatement(sql);//设置ps参数ps.setString(1,bookType.getBookTypeName());ps.setString(2,bookType.getBookTypeDesc());ps.setInt(3, bookType.getId());//赶回ps更新数据库的记录数return ps.executeUpdate();}/*** 删除数据库记录* @param con 数据库连接对象* @param id 传入删除记录的id* @return 返回删除的记录数* @throws SQLException 抛出数据库异常*/public int delete(Connection con,String id) throws SQLException{//拼写sql删除语句String sql="delete from t_bookType where id=?";//获取预编译对象psPreparedStatement ps=con.prepareStatement(sql);//设置ps参数ps.setString(1, id);//执行ps更行操作,并返回改变的数据记录条数return ps.executeUpdate();}
}

③ UserDao(用户数据访问对象)

package cn.ac.azure.dao;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import cn.ac.azure.model.User;/*** 用户数据访问对象* @author **/
public class UserDao {/*** 登录验证* @param con 数据库连接对象* @param user 登录的账户* @return 返回一个用户对象,为null,则登录失败;反之,则登录成功* @throws Exception 让调用者处理异常*/public User login(Connection con,User user) throws SQLException{//定义一个返回用户对象User resultUser=null;//拼写sql查询语句String sql="select * from t_user where username=? and password=?";//获取sql语句预编译对象PreparedStatement ps=con.prepareStatement(sql);//设置ps参数ps.setString(1, user.getUsername());ps.setString(2, user.getPassword());//ps执行sql查询语句返回结果集ResultSet rs=ps.executeQuery();//遍历结果集while(rs.next()){//初始化返回用户对象resultUser=new User(); resultUser.setId(rs.getInt("id"));    //设置用户idresultUser.setUsername(rs.getString("username"));  //设置用户名称resultUser.setPassword(rs.getString("password"));  //设置用户密码}//返回用户对象return resultUser;}
}

4、View包 【视图层】

① LoginFrame(登录界面)

package cn.ac.azure.view;import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
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.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;import cn.ac.azure.dao.UserDao;
import cn.ac.azure.model.User;
import cn.ac.azure.util.DBTool;public class LoginFrame extends JFrame {static {try {// 这里是皮肤包可以随意切换
//           javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.mcwin.McWinLookAndFeel");javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.smart.SmartLookAndFeel");
//           javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.luna.LunaLookAndFeel");
//           javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.aluminium.AluminiumLookAndFeel");
//           javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.bernstein.BernsteinLookAndFeel");
//           javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.hifi.HiFiLookAndFeel");
//           javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.aero.AeroLookAndFeel");
//           javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.mint.MintLookAndFeel");} catch (ClassNotFoundException | InstantiationException | IllegalAccessException| UnsupportedLookAndFeelException e) {e.printStackTrace();}}private JPanel contentPane;private JTextField usernameText;private JPasswordField passwordText;private JButton loginBtn;private JButton resetBtn;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {LoginFrame frame = new LoginFrame();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public LoginFrame() {//改变系统默认字体Font font = new Font("Dialog", Font.PLAIN, 12);java.util.Enumeration<Object> keys = UIManager.getDefaults().keys();while (keys.hasMoreElements()) {Object key = keys.nextElement();Object value = UIManager.get(key);if (value instanceof javax.swing.plaf.FontUIResource) {UIManager.put(key, font);}}setIconImage(Toolkit.getDefaultToolkit().getImage(LoginFrame.class.getResource("/images/logo.png")));setResizable(false);setTitle("管理员登录");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 450, 300);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);JLabel lblNewLabel = new JLabel("图书管理系统");lblNewLabel.setFont(new Font("宋体", Font.BOLD, 22));lblNewLabel.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/logo.png")));JLabel lblNewLabel_1 = new JLabel("用户名 :");lblNewLabel_1.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/userName.png")));usernameText = new JTextField();usernameText.setColumns(10);JLabel lblNewLabel_2 = new JLabel("密  码 :");lblNewLabel_2.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/password.png")));passwordText = new JPasswordField();//添加登陆按钮loginBtn = new JButton("登录");loginBtn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {loginActionPerformed(e);}});loginBtn.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/login.png")));//添加重置按钮resetBtn = new JButton("重置");resetBtn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {resetActionPerformed(e);}});resetBtn.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/reset.png")));GroupLayout gl_contentPane = new GroupLayout(contentPane);gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPane.createSequentialGroup().addGap(106).addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addComponent(lblNewLabel).addGroup(gl_contentPane.createSequentialGroup().addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPane.createSequentialGroup().addPreferredGap(ComponentPlacement.RELATED).addComponent(lblNewLabel_1).addPreferredGap(ComponentPlacement.RELATED).addComponent(usernameText, GroupLayout.PREFERRED_SIZE, 142, GroupLayout.PREFERRED_SIZE)).addGroup(gl_contentPane.createSequentialGroup().addComponent(lblNewLabel_2).addPreferredGap(ComponentPlacement.UNRELATED).addComponent(passwordText, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE)).addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup().addGap(16).addComponent(loginBtn).addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(resetBtn))).addPreferredGap(ComponentPlacement.RELATED))).addContainerGap(105, GroupLayout.PREFERRED_SIZE)));gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPane.createSequentialGroup().addGap(25).addComponent(lblNewLabel).addGap(18).addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_1).addComponent(usernameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGap(18).addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_2).addComponent(passwordText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGap(29).addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(loginBtn).addComponent(resetBtn)).addContainerGap()));contentPane.setLayout(gl_contentPane);//设置窗口居中this.setLocationRelativeTo(null);}/*** 登录事件处理* @param evt*/private void loginActionPerformed(ActionEvent evt) {//从输入框获取用户名String username=usernameText.getText().trim();//从输入框获取密码String password=passwordText.getText().trim();//用户名不能为空或空字符串,否则结束事件处理if(username==null || "".equals(username)){JOptionPane.showMessageDialog(null, "用户名不能为空");return;}//用户名不能为空或空字符串否则结束事件处理if(password==null || "".equals(password)){JOptionPane.showMessageDialog(null, "密码不能为空");return;}//将从输入框获得信息新建一个对象User user=new User(username, password);//定义数据库连接Connection con=null;try {//利用数据库工具类获取数据库连接con=DBTool.getConnetion();//新建一个用户数据访问对象UserDao userDao=new UserDao();//调用其登录验证方法获取一个用户对象User currUser=userDao.login(con, user);//判断返回的用户对象if(currUser!=null){//不为空,表示登录成功//进入主界面,并设置可见new MainFrame().setVisible(true);//释放当前窗口资源dispose();}else{ //为空,表示登录不成功JOptionPane.showMessageDialog(null, "登录失败(u_u)");}} catch (SQLException e) {e.printStackTrace();throw new RuntimeException("登录失败",e);}finally{//关闭数据库连接DBTool.close(con);}}/*** 重置事件处理* @param evt*/private void resetActionPerformed(ActionEvent evt) {usernameText.setText("");passwordText.setText("");}
}

② MainFrame(主界面)

package cn.ac.azure.view;import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;public class MainFrame extends JFrame {static {try {// 这里是皮肤包可以随意切换
//           javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.mcwin.McWinLookAndFeel");javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.smart.SmartLookAndFeel");
//           javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.luna.LunaLookAndFeel");
//           javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.aluminium.AluminiumLookAndFeel");
//           javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.bernstein.BernsteinLookAndFeel");
//           javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.hifi.HiFiLookAndFeel");
//           javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.aero.AeroLookAndFeel");
//           javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.mint.MintLookAndFeel");} catch (ClassNotFoundException | InstantiationException | IllegalAccessException| UnsupportedLookAndFeelException e) {e.printStackTrace();}}private static final long serialVersionUID = 1L;//定义内容窗格private JPanel contentPane;//定义桌面窗格private JDesktopPane table;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {MainFrame frame = new MainFrame();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public MainFrame() {//改变系统默认字体Font font = new Font("Dialog", Font.PLAIN, 12);java.util.Enumeration<Object> keys = UIManager.getDefaults().keys();while (keys.hasMoreElements()) {Object key = keys.nextElement();Object value = UIManager.get(key);if (value instanceof javax.swing.plaf.FontUIResource) {UIManager.put(key, font);}}setTitle("图书管理系统主界面");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 450, 300);JMenuBar menuBar = new JMenuBar();menuBar.setToolTipText("");setJMenuBar(menuBar);JMenu menu = new JMenu("基本数据维护 ");menu.setIcon(new ImageIcon(MainFrame.class.getResource("/images/base.png")));menuBar.add(menu);JMenu mnNewMenu = new JMenu("图书类别管理 ");mnNewMenu.setIcon(new ImageIcon(MainFrame.class.getResource("/images/bookTypeManager.png")));menu.add(mnNewMenu);//图书类别添加JMenuItem menuItem = new JMenuItem("图书类别添加");menuItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {BookTypeAddInterFrame bookTypeAddInterFrame=new BookTypeAddInterFrame();bookTypeAddInterFrame.setVisible(true);table.add(bookTypeAddInterFrame);}});menuItem.setIcon(new ImageIcon(MainFrame.class.getResource("/images/add.png")));mnNewMenu.add(menuItem);//图书类别维护JMenuItem menuItem_1 = new JMenuItem("图书类别维护");menuItem_1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {BookTypeManageInterFrame bookTypeManageInterFrame=new BookTypeManageInterFrame();bookTypeManageInterFrame.setVisible(true);table.add(bookTypeManageInterFrame);}});menuItem_1.setIcon(new ImageIcon(MainFrame.class.getResource("/images/edit.png")));mnNewMenu.add(menuItem_1);JMenu menu_1 = new JMenu("图书管理");menu_1.setIcon(new ImageIcon(MainFrame.class.getResource("/images/bookManager.png")));menu.add(menu_1);//图书添加JMenuItem menuItem_2 = new JMenuItem("图书添加");menuItem_2.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {BookAddInterFrame bookAddInterFrame=new BookAddInterFrame();bookAddInterFrame.setVisible(true);table.add(bookAddInterFrame);}});menuItem_2.setIcon(new ImageIcon(MainFrame.class.getResource("/images/add.png")));menu_1.add(menuItem_2);//图书维护JMenuItem menuItem_3 = new JMenuItem("图书维护");menuItem_3.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {BookManageInterFrame bookManageInterFrame=new BookManageInterFrame();bookManageInterFrame.setVisible(true);table.add(bookManageInterFrame);}});menuItem_3.setIcon(new ImageIcon(MainFrame.class.getResource("/images/edit.png")));menu_1.add(menuItem_3);//安全退出JMenuItem menuItem_4 = new JMenuItem("安全退出");menuItem_4.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {//弹出退出确认提示框int res=JOptionPane.showConfirmDialog(null, "确定要退出吗?");//确定退出if(res==JOptionPane.OK_OPTION){dispose();} //否则继续留在该界面}});menuItem_4.setIcon(new ImageIcon(MainFrame.class.getResource("/images/exit.png")));menu.add(menuItem_4);JMenu menu_2 = new JMenu("关于我们");menu_2.setIcon(new ImageIcon(MainFrame.class.getResource("/images/about.png")));menuBar.add(menu_2);//关于我们JMenuItem menuItem_5 = new JMenuItem("关于我们");menuItem_5.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {//新建一个图书内部窗体LibraryInterFrame libraryInnerFrame=new LibraryInterFrame();//显示图书内部窗体libraryInnerFrame.setVisible(true);//将图书内部窗体显示到主界面桌面窗格中table.add(libraryInnerFrame);}});menuItem_5.setIcon(new ImageIcon(MainFrame.class.getResource("/images/about.png")));menu_2.add(menuItem_5);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));contentPane.setLayout(new BorderLayout(0, 0));setContentPane(contentPane);//定义主界面桌面窗格界面,用于装载内部窗体table = new JDesktopPane();table.setBackground(Color.LIGHT_GRAY);contentPane.add(table, BorderLayout.CENTER);//设置窗口最大化setExtendedState(JFrame.MAXIMIZED_BOTH);}
}

③ BookTypeManageInterFrame(图书类别管理界面)

package cn.ac.azure.view;import java.awt.EventQueue;
import java.awt.Font;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;import cn.ac.azure.dao.BookTypeDao;
import cn.ac.azure.model.BookType;
import cn.ac.azure.util.DBTool;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JTextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;public class BookTypeManageInterFrame extends JInternalFrame {private JTextField s_bookTypeNameText;private JTable bookTypeTable;private BookTypeDao bookTypeDao=new BookTypeDao();private JTextField idText;private JTextField bookTypeNameText;private JTextArea bookTypeDescText;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {BookTypeManageInterFrame frame = new BookTypeManageInterFrame();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public BookTypeManageInterFrame() {//改变系统默认字体Font font = new Font("Dialog", Font.PLAIN, 12);java.util.Enumeration keys = UIManager.getDefaults().keys();while (keys.hasMoreElements()) {Object key = keys.nextElement();Object value = UIManager.get(key);if (value instanceof javax.swing.plaf.FontUIResource) {UIManager.put(key, font);}}setIconifiable(true);setClosable(true);setTitle("图书类别管理");setBounds(400, 100, 535, 489);JScrollPane scrollPane = new JScrollPane();JLabel label = new JLabel("图书类别名称:");s_bookTypeNameText = new JTextField();s_bookTypeNameText.setColumns(10);//查询按钮JButton searchBtn = new JButton("查询");searchBtn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {searchActionPerformed(e);}});searchBtn.setIcon(new ImageIcon(BookTypeManageInterFrame.class.getResource("/images/search.png")));JPanel panel = new JPanel();panel.setBorder(new TitledBorder(null, "表单操作", TitledBorder.LEADING, TitledBorder.TOP, null, null));GroupLayout groupLayout = new GroupLayout(getContentPane());groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addGap(56).addComponent(label).addPreferredGap(ComponentPlacement.UNRELATED).addComponent(s_bookTypeNameText, GroupLayout.PREFERRED_SIZE, 167, GroupLayout.PREFERRED_SIZE).addPreferredGap(ComponentPlacement.RELATED, 54, Short.MAX_VALUE).addComponent(searchBtn).addGap(71)).addGroup(groupLayout.createSequentialGroup().addGap(36).addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false).addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(scrollPane, Alignment.LEADING)).addContainerGap(31, Short.MAX_VALUE)));groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addGap(27).addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(searchBtn).addComponent(label).addComponent(s_bookTypeNameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGap(18).addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 167, GroupLayout.PREFERRED_SIZE).addGap(18).addComponent(panel, GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE).addContainerGap()));JLabel label_1 = new JLabel("编号:");idText = new JTextField();idText.setEditable(false);idText.setColumns(10);JLabel label_2 = new JLabel("图书类别名称:");bookTypeNameText = new JTextField();bookTypeNameText.setColumns(10);JLabel label_3 = new JLabel("描述:");bookTypeDescText = new JTextArea();//修改按钮JButton modifyBtn = new JButton("修改");modifyBtn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {bookTypeUpdateActionPerformed(e);}});modifyBtn.setIcon(new ImageIcon(BookTypeManageInterFrame.class.getResource("/images/modify.png")));//删除按钮JButton deleteBtn = new JButton("删除");deleteBtn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {bookTypeDeleteActionPerformed(e);}});deleteBtn.setIcon(new ImageIcon(BookTypeManageInterFrame.class.getResource("/images/delete.png")));GroupLayout gl_panel = new GroupLayout(panel);gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(Alignment.LEADING).addGroup(gl_panel.createSequentialGroup().addGap(19).addGroup(gl_panel.createParallelGroup(Alignment.TRAILING).addGroup(Alignment.LEADING, gl_panel.createSequentialGroup().addComponent(label_1).addPreferredGap(ComponentPlacement.RELATED).addComponent(idText, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE).addGap(18).addComponent(label_2).addPreferredGap(ComponentPlacement.UNRELATED).addComponent(bookTypeNameText, GroupLayout.PREFERRED_SIZE, 166, GroupLayout.PREFERRED_SIZE)).addGroup(Alignment.LEADING, gl_panel.createSequentialGroup().addComponent(label_3).addPreferredGap(ComponentPlacement.RELATED).addComponent(bookTypeDescText)).addGroup(gl_panel.createSequentialGroup().addComponent(modifyBtn).addGap(54).addComponent(deleteBtn).addGap(64))).addContainerGap(56, GroupLayout.PREFERRED_SIZE)));gl_panel.setVerticalGroup(gl_panel.createParallelGroup(Alignment.LEADING).addGroup(gl_panel.createSequentialGroup().addContainerGap().addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(label_1).addComponent(idText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(label_2).addComponent(bookTypeNameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGap(27).addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(label_3).addComponent(bookTypeDescText, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)).addGap(18).addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(deleteBtn).addComponent(modifyBtn)).addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));panel.setLayout(gl_panel);//表格bookTypeTable = new JTable();//表格鼠标点击事件bookTypeTable.addMouseListener(new MouseAdapter() {@Overridepublic void mousePressed(MouseEvent e) {bookTypeTableMousePressed(e);}});bookTypeTable.setModel(new DefaultTableModel(new Object[][] {},new String[] {"编号", "图书类别名称", "图书类别描述"}) {boolean[] columnEditables = new boolean[] {false, false, false};public boolean isCellEditable(int row, int column) {return columnEditables[column];}});bookTypeTable.getColumnModel().getColumn(1).setPreferredWidth(96);bookTypeTable.getColumnModel().getColumn(2).setPreferredWidth(185);scrollPane.setViewportView(bookTypeTable);getContentPane().setLayout(groupLayout);//设置文本域边框bookTypeDescText.setBorder(new LineBorder(new java.awt.Color(127,157,185), 1, false));//构造函数中调用填充表格数据函数,全部图书类别显示在表格中fillTable(new BookType());}/*** 图书类别删除事件处理* @param evt*/private void bookTypeDeleteActionPerformed(ActionEvent evt) {//获得表单中编号的值idString id=idText.getText();//判断表单有没有选中的图书类别记录if(id==null || "".equals(id.trim())){JOptionPane.showMessageDialog(null, "请选择要修改的记录!");return;}//弹出确认框,是否要删除图书类别记录int res=JOptionPane.showConfirmDialog(null, "你确定要删除该条记录吗?");if(res!=0){ //否return; //结束该事件处理函数}//定义数据库连接Connection con=null;try {//获取数据路连接con=DBTool.getConnetion();int row=bookTypeDao.delete(con, id);if(row==1){//删除成功,弹出提示框JOptionPane.showMessageDialog(null, "修改数据成功(n_n)");//清空表单数据resetValue();//刷新表格记录显示fillTable(new BookType());}else{//删除失败,弹出提示框JOptionPane.showMessageDialog(null, "修改数据失败(u_u)");}} catch (SQLException e) {//记录日志e.printStackTrace();throw new RuntimeException("删除记录失败!",e);}finally{//关闭数据库DBTool.close(con);}}/*** 图书类别修改事件处理* @param evt*/private void bookTypeUpdateActionPerformed(ActionEvent evt) {//获取表单操作各个文本框的值String id=idText.getText();String bookTypeName=bookTypeNameText.getText();String bookTypeDesc=bookTypeDescText.getText();//判断表单有没有选中的图书类别记录if(id==null || "".equals(id.trim())){JOptionPane.showMessageDialog(null, "请选择要修改的记录!");return;}//图书类别名称不能为空if(bookTypeName==null || "".equals(bookTypeName.trim())){JOptionPane.showMessageDialog(null, "图书类别名称不能为空!");return;}//利用表单的数据新建一个图书类别对象BookType bookType=new BookType(Integer.parseInt(id), bookTypeName, bookTypeDesc);//定义数据库连接对象Connection con=null;try {//获取数据库连接con=DBTool.getConnetion();//执行图书类别dao类的修改记录方法int res=bookTypeDao.update(con, bookType);if(res==1){//修改成功,弹出提示框JOptionPane.showMessageDialog(null, "修改数据成功(n_n)");//清空表单数据resetValue();//刷新表格记录显示fillTable(new BookType());}else{//修改失败,弹出提示框JOptionPane.showMessageDialog(null, "修改数据失败(u_u)");}} catch (SQLException e) {//记录日志e.printStackTrace();throw new RuntimeException("修改图书类别失败",e);}finally{//关闭数据路连接DBTool.close(con);}}/*** 表格鼠标点击事件处理* @param e */private void bookTypeTableMousePressed(MouseEvent e) {//获取表格选中的行int row=bookTypeTable.getSelectedRow();//获取表中选中行的第一列的值并显示在idText框中idText.setText(String.valueOf(bookTypeTable.getValueAt(row, 0)));//获取表中选中行的第二列的值并显示在bookTypeNameText框中bookTypeNameText.setText((String)bookTypeTable.getValueAt(row, 1));//获取表中选中行的第三列的值并显示在bookTypeDescText框中bookTypeDescText.setText((String)bookTypeTable.getValueAt(row, 2));}/*** 图书类别查询事件处理* @param evt*/private void searchActionPerformed(ActionEvent evt) {//获取图书类别输入框里的内容String s_bookTypeName=s_bookTypeNameText.getText();//新建一个图书类别并初始化BookType bookType=new BookType();//将输入框的内容设置成新建图书类别的图书类别名称bookType.setBookTypeName(s_bookTypeName);//根据图书类别查询图书类别fillTable(bookType);}/*** 在表格中填充数据* @param bookType 传入bookType对象*/private void fillTable(BookType bookType){//获取表格的模型DefaultTableModel dtm=(DefaultTableModel) bookTypeTable.getModel();//清空表格dtm.setRowCount(0); //定义数据库连接Connection con=null;try {//获取数据库连接con=DBTool.getConnetion();//调用BookTyPeDao的查询方法,并获得其查询的结果集ResultSet rs=bookTypeDao.search(con, bookType);//遍历结果集while(rs.next()){//新建一个vector并初始化Vector v=new Vector(); v.add(rs.getInt("id"));  //向vector中添加idv.add(rs.getString("bookTypeName")); //向vector中添加bookTypeNamev.add(rs.getString("bookTypeDesc"));  //向vector中添加bookTypeDesc//将vector中的数据显示到表格中dtm.addRow(v);}} catch (SQLException e) {//记录日志e.printStackTrace();throw new RuntimeException("查询失败");}finally{//关闭数据库DBTool.close(con);}}/*** 清空表单数据*/private void resetValue(){idText.setText("");bookTypeNameText.setText("");bookTypeDescText.setText("");s_bookTypeNameText.setText("");}
}

④ BookTypeAddInterFrame(图书类别添加界面)

package cn.ac.azure.view;import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;import cn.ac.azure.dao.BookTypeDao;
import cn.ac.azure.model.BookType;
import cn.ac.azure.util.DBTool;
/*** 图书类别内部添加窗体* @author green**/
public class BookTypeAddInterFrame extends JInternalFrame {//图书类别名称输入框private JTextField bookTypeNameText;//图书类别描述输入框private JTextArea bookTypeDescText;//重置按钮private JButton resetBtn;//添加按钮private JButton addBtn;//图书类别数据库访问对象private BookTypeDao bookTypeDao;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {BookTypeAddInterFrame frame = new BookTypeAddInterFrame();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public BookTypeAddInterFrame() {//改变系统默认字体Font font = new Font("Dialog", Font.PLAIN, 12);java.util.Enumeration keys = UIManager.getDefaults().keys();while (keys.hasMoreElements()) {Object key = keys.nextElement();Object value = UIManager.get(key);if (value instanceof javax.swing.plaf.FontUIResource) {UIManager.put(key, font);}}setClosable(true);setIconifiable(true);setTitle("图书类别添加");setBounds(100, 100, 487, 342);JLabel label = new JLabel("图书类别名称:");bookTypeNameText = new JTextField();bookTypeNameText.setColumns(10);JLabel label_1 = new JLabel("图书类别描述:");bookTypeDescText = new JTextArea();//添加按钮addBtn = new JButton("添加");addBtn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {addActionPerformed(e);}});//重置按钮resetBtn = new JButton("重置");resetBtn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {resetActionPerformed(e);}});GroupLayout groupLayout = new GroupLayout(getContentPane());groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addGap(128).addComponent(addBtn).addGap(91).addComponent(resetBtn)).addGroup(groupLayout.createSequentialGroup().addGap(89).addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addComponent(bookTypeDescText).addGroup(groupLayout.createSequentialGroup().addComponent(label).addPreferredGap(ComponentPlacement.RELATED).addComponent(bookTypeNameText, GroupLayout.PREFERRED_SIZE, 189, GroupLayout.PREFERRED_SIZE)).addComponent(label_1)))).addContainerGap(105, Short.MAX_VALUE)));groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addGap(49).addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(label).addComponent(bookTypeNameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGap(18).addComponent(label_1).addGap(10).addComponent(bookTypeDescText, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE).addGap(41).addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(resetBtn).addComponent(addBtn)).addContainerGap()));getContentPane().setLayout(groupLayout);//设置文本域边框bookTypeDescText.setBorder(new LineBorder(new java.awt.Color(127,157,185), 1, false));}/*** 添加事件处理* @param evt*/private void addActionPerformed(ActionEvent evt) {//获取输入框的值String bookTypeName=bookTypeNameText.getText();String bookTypeDesc=bookTypeDescText.getText();if(bookTypeName==null || "".equals(bookTypeName.trim())){JOptionPane.showMessageDialog(null,"图书类别不能为空!");return;}//新建图书类别实体对象BookType bookType=new BookType(bookTypeName, bookTypeDesc);//定义数据库连接Connection con=null;try {//获取数据库连接con=DBTool.getConnetion();//初始化图书类别对象BookTypeDaobookTypeDao=new BookTypeDao();//调用图书类别dao对象的添加方法int res=bookTypeDao.add(con, bookType);if(res!=0){//提示添加成功JOptionPane.showMessageDialog(null, "图书添加成功(n_n)");//清空输入框bookTypeNameText.setText("");bookTypeDescText.setText("");}else{//提示添加失败JOptionPane.showMessageDialog(null,"图书添加失败(u_u)");}} catch (SQLException e) {//记录日志e.printStackTrace();throw new RuntimeException("添加图书失败",e);}finally{//关闭数据库DBTool.close(con);}}/*** 重置事件处理* @param evt*/private void resetActionPerformed(ActionEvent evt) {//置空图书类别名称输入框bookTypeNameText.setText("");//置空图书类别描述输入框bookTypeDescText.setText("");}
}

⑤ BookManageInterFrame(图书管理界面)

package cn.ac.azure.view;import java.awt.EventQueue;
import java.awt.Font;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;import cn.ac.azure.dao.BookDao;
import cn.ac.azure.dao.BookTypeDao;
import cn.ac.azure.model.Book;
import cn.ac.azure.model.BookType;
import cn.ac.azure.util.DBTool;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;public class BookManageInterFrame extends JInternalFrame {private JTextField s_bookNameText;private JTextField s_authorText;private JTable bookTable;private JComboBox s_bookTypecomboBox;private BookTypeDao bookTypeDao;private BookDao bookDao;private JTextField idText;private JTextField bookNameText;private JTextField priceText;private JTextField authorText;private JTextField bookDescText;private final ButtonGroup buttonGroup = new ButtonGroup();private JComboBox bookTypeComboBox;private JRadioButton maleBtn;private JRadioButton femaleBtn;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {BookManageInterFrame frame = new BookManageInterFrame();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public BookManageInterFrame() {//改变系统默认字体Font font = new Font("Dialog", Font.PLAIN, 12);java.util.Enumeration keys = UIManager.getDefaults().keys();while (keys.hasMoreElements()) {Object key = keys.nextElement();Object value = UIManager.get(key);if (value instanceof javax.swing.plaf.FontUIResource) {UIManager.put(key, font);}}setIconifiable(true);setClosable(true);setTitle("图书管理 ");setBounds(100, 100, 767, 528);JPanel panel = new JPanel();panel.setBorder(new TitledBorder(null, "搜索条件", TitledBorder.LEADING, TitledBorder.TOP, null, null));JScrollPane scrollPane = new JScrollPane();JPanel panel_1 = new JPanel();panel_1.setBorder(new TitledBorder(null, "表单操作", TitledBorder.LEADING, TitledBorder.TOP, null, null));GroupLayout groupLayout = new GroupLayout(getContentPane());groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup().addGap(29).addGroup(groupLayout.createParallelGroup(Alignment.TRAILING).addComponent(panel_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 661, Short.MAX_VALUE).addComponent(panel, GroupLayout.DEFAULT_SIZE, 661, Short.MAX_VALUE)).addGap(38)));groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addGap(29).addComponent(panel, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE).addPreferredGap(ComponentPlacement.UNRELATED).addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE).addPreferredGap(ComponentPlacement.UNRELATED).addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 217, GroupLayout.PREFERRED_SIZE).addContainerGap(20, Short.MAX_VALUE)));JLabel label_2 = new JLabel("编号:");idText = new JTextField();idText.setColumns(10);JLabel label_3 = new JLabel("图书名称:");bookNameText = new JTextField();bookNameText.setColumns(10);JLabel label_4 = new JLabel("作者性别:");maleBtn = new JRadioButton("男");buttonGroup.add(maleBtn);femaleBtn = new JRadioButton("女");buttonGroup.add(femaleBtn);JLabel label_5 = new JLabel("价格:");priceText = new JTextField();priceText.setColumns(10);JLabel label_6 = new JLabel("图书作者:");authorText = new JTextField();authorText.setColumns(10);JLabel label_7 = new JLabel("图书类别:");bookTypeComboBox = new JComboBox();JLabel label_8 = new JLabel("图书描述:");bookDescText = new JTextField();bookDescText.setColumns(10);//修改按钮JButton modifyBtn = new JButton("修改");modifyBtn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {modifyBookActionPerformed(e);}});modifyBtn.setIcon(new ImageIcon(BookManageInterFrame.class.getResource("/images/modify.png")));//删除按钮JButton deleteBtn = new JButton("删除");deleteBtn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {deleteBookActionPerformed(e);}});deleteBtn.setIcon(new ImageIcon(BookManageInterFrame.class.getResource("/images/delete.png")));GroupLayout gl_panel_1 = new GroupLayout(panel_1);gl_panel_1.setHorizontalGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING).addGroup(gl_panel_1.createSequentialGroup().addGap(44).addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING, false).addGroup(gl_panel_1.createSequentialGroup().addComponent(label_8).addPreferredGap(ComponentPlacement.RELATED).addComponent(bookDescText)).addGroup(gl_panel_1.createSequentialGroup().addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING).addComponent(label_2).addComponent(label_5)).addPreferredGap(ComponentPlacement.UNRELATED).addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING, false).addComponent(priceText).addComponent(idText, GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)).addGap(37).addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING, false).addGroup(gl_panel_1.createSequentialGroup().addComponent(label_3).addPreferredGap(ComponentPlacement.RELATED).addComponent(bookNameText, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)).addGroup(gl_panel_1.createSequentialGroup().addComponent(label_6).addPreferredGap(ComponentPlacement.RELATED).addComponent(authorText))).addGap(35).addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING).addGroup(gl_panel_1.createSequentialGroup().addComponent(label_4).addPreferredGap(ComponentPlacement.UNRELATED).addComponent(maleBtn).addGap(18).addComponent(femaleBtn)).addGroup(gl_panel_1.createSequentialGroup().addComponent(label_7).addPreferredGap(ComponentPlacement.UNRELATED).addComponent(bookTypeComboBox, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE))))).addContainerGap(34, Short.MAX_VALUE)).addGroup(gl_panel_1.createSequentialGroup().addContainerGap(201, Short.MAX_VALUE).addComponent(modifyBtn).addGap(104).addComponent(deleteBtn).addGap(190)));gl_panel_1.setVerticalGroup(gl_panel_1.createParallelGroup(Alignment.LEADING).addGroup(gl_panel_1.createSequentialGroup().addContainerGap().addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE).addComponent(maleBtn).addComponent(bookNameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(label_3).addComponent(label_2).addComponent(idText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(femaleBtn).addComponent(label_4)).addGap(18).addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE).addComponent(priceText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(label_5).addComponent(label_6).addComponent(authorText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(bookTypeComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(label_7)).addGap(18).addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE).addComponent(label_8).addComponent(bookDescText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGap(27).addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE).addComponent(modifyBtn).addComponent(deleteBtn)).addContainerGap()));panel_1.setLayout(gl_panel_1);//表格bookTable = new JTable();//表格鼠标按下事件bookTable.addMouseListener(new MouseAdapter() {@Overridepublic void mousePressed(MouseEvent e) {tableMousePressed(e);}});bookTable.setModel(new DefaultTableModel(new Object[][] {},new String[] {"编号", "图书名称", "图书作者", "图书性别", "图书价格", "图书类别", "图书描述"}) {boolean[] columnEditables = new boolean[] {false, false, false, false, false, false, false};public boolean isCellEditable(int row, int column) {return columnEditables[column];}});bookTable.getColumnModel().getColumn(0).setPreferredWidth(56);bookTable.getColumnModel().getColumn(1).setPreferredWidth(100);bookTable.getColumnModel().getColumn(2).setPreferredWidth(63);bookTable.getColumnModel().getColumn(3).setPreferredWidth(63);bookTable.getColumnModel().getColumn(4).setPreferredWidth(61);bookTable.getColumnModel().getColumn(5).setPreferredWidth(94);bookTable.getColumnModel().getColumn(6).setPreferredWidth(163);scrollPane.setViewportView(bookTable);JLabel lblL = new JLabel("图书名称:");s_bookNameText = new JTextField();s_bookNameText.setColumns(10);JLabel label = new JLabel("图书作者:");s_authorText = new JTextField();s_authorText.setColumns(10);JLabel label_1 = new JLabel("图书类别:");s_bookTypecomboBox = new JComboBox();//图书查询按钮JButton s_searchBtn = new JButton("查询");s_searchBtn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {searchActionPerformed(e);}});s_searchBtn.setIcon(new ImageIcon(BookManageInterFrame.class.getResource("/images/search.png")));GroupLayout gl_panel = new GroupLayout(panel);gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(Alignment.LEADING).addGroup(gl_panel.createSequentialGroup().addGap(20).addComponent(lblL).addPreferredGap(ComponentPlacement.RELATED).addComponent(s_bookNameText, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE).addGap(18).addComponent(label).addPreferredGap(ComponentPlacement.RELATED).addComponent(s_authorText, GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE).addGap(18).addComponent(label_1).addPreferredGap(ComponentPlacement.UNRELATED).addComponent(s_bookTypecomboBox, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).addGap(18).addComponent(s_searchBtn).addGap(29)));gl_panel.setVerticalGroup(gl_panel.createParallelGroup(Alignment.LEADING).addGroup(gl_panel.createSequentialGroup().addContainerGap().addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(lblL).addComponent(s_bookNameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(label).addComponent(s_authorText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(label_1).addComponent(s_bookTypecomboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(s_searchBtn)).addContainerGap(19, Short.MAX_VALUE)));panel.setLayout(gl_panel);getContentPane().setLayout(groupLayout);//初始化搜索栏图书类别下拉框fillBookTypeComboBox("search");//初始化操作栏图书类别下拉框fillBookTypeComboBox("modify");//初始化表格显示,显示所有的书籍fillBookTable(new Book());}/*** 图书修改事件* @param evt*/private void modifyBookActionPerformed(ActionEvent evt) {//获取图书idString id=idText.getText();//获取图书名称String bookName=bookNameText.getText();//获取图书作者String author=authorText.getText();//或者作者性别String sex="男";if(femaleBtn.isSelected()){sex="女";}//获取图书价格String price=priceText.getText();//获取图书idBookType bookType=(BookType)bookTypeComboBox.getSelectedItem();Integer bookTypeId=bookType.getId();//获取图书描述String bookDesc=bookDescText.getText();//判断是否id是否为空if(id==null || "".equals(id)){ //为空JOptionPane.showMessageDialog(null, "请选中要删除的行!");  //给用户提示return;}//判断图书名称是否为空if(bookName==null || "".equals(bookName)){ //为空JOptionPane.showMessageDialog(null, "图书名称不能为空!");  //给用户提示return;}//判断图书作者是否为空if(author==null || "".equals(author)){ //为空JOptionPane.showMessageDialog(null, "图书作者不能为空!");  //给用户提示return;}//判断图书价格是否为空if(price==null || "".equals(price)){ //为空JOptionPane.showMessageDialog(null, "图书价格不能为空!");  //给用户提示return;}//从获取的图书信息创建图书对象Book book=new Book(Integer.parseInt(id),bookName, author, sex, Float.parseFloat(price), bookTypeId, bookDesc, null);System.out.println("从获取的图书信息创建图书对象:"+book);//定义数据库连接Connection con=null;try {//获取数据库连接con=DBTool.getConnetion();//初始化图书数据访问对象bookDao=new BookDao();//执行图书访问对象的修改方法,并获得修改的记录数int res=bookDao.update(con, book);if(res==1){ //为1JOptionPane.showMessageDialog(null,"图书修改成功n_n");//刷新图书表格显示fillBookTable(new Book());//重置操作栏resetValue();}else{ //为0JOptionPane.showMessageDialog(null,"图书修改失败u_u");}} catch (SQLException e) {//记录日志e.printStackTrace();throw new RuntimeException("修改图书失败",e);}finally{//关闭数据库连接DBTool.close(con);}}/*** 图书删除事件* @param evt*/private void deleteBookActionPerformed(ActionEvent evt) {//获取图书idString id=idText.getText();//判断是否id是否为空if(id==null || "".equals(id)){ //为空JOptionPane.showMessageDialog(null, "请选中要删除的行!");  //给用户提示return;}//定义数据库连接对象Connection con=null;try {//初始化数据库连接对象con=DBTool.getConnetion(); //初始化图书数据访问对象bookDao=new BookDao();//执行图书访问对象的删除方法并返回删除的记录数int res=bookDao.delete(con, Integer.parseInt(id));if(res==1){ //为1JOptionPane.showMessageDialog(null, "图书删除成功n_n");//刷新图书表格显示fillBookTable(new Book());//重置操作栏resetValue();}else{ //为其他JOptionPane.showMessageDialog(null, "图书删除失败u_u");}} catch (SQLException e) {//记录日志e.printStackTrace();throw new RuntimeException("删除图书失败",e);}finally{//记得关闭数据库(******)DBTool.close(con);}}/*** 重置操作栏的所有值*/private void resetValue(){idText.setText("");bookNameText.setText("");authorText.setText("");maleBtn.setSelected(true);priceText.setText("");fillBookTypeComboBox("modify");bookDescText.setText("");}/*** 表格鼠标按下事件处理* @param evt*/private void tableMousePressed(MouseEvent evt) {//获取图书表格选中的行的行号int row=bookTable.getSelectedRow();//获取选中行第一个数据并设置显示在操作栏的id框idText.setText((Integer)bookTable.getValueAt(row,0)+"");//获取选中行第二个数据并设置显示在操作栏的图书名称框bookNameText.setText((String)bookTable.getValueAt(row, 1));//获取选中行第三个数据并设置显示在操作栏的图书作者框authorText.setText((String)bookTable.getValueAt(row, 2));//获取选中行第四个数据并设置显示在操作栏的作者性别单选框String sex=(String)bookTable.getValueAt(row, 3);if("男".equals(sex)){maleBtn.setSelected(true);}else{femaleBtn.setSelected(true);}//获取选中行第五个数据并设置显示在操作栏的图书价格框priceText.setText((Float)bookTable.getValueAt(row, 4)+"");//获取选中行第六个数据并设置显示在操作栏的图书类别下拉框中String bookTypeName=(String)bookTable.getValueAt(row, 5);int rows=bookTypeComboBox.getItemCount();  //获取下拉框总共的选项for(int i=0;i<rows;i++){ //遍历下拉框BookType item=(BookType) bookTypeComboBox.getItemAt(i); //获取每一个选项并强转图书类别对象if(item.getBookTypeName().equals(bookTypeName)){  //将获取的图书类别和下拉框中的图书类别比较,若相同bookTypeComboBox.setSelectedIndex(i); //则该下拉框选项被选中}}//获取选中行第七个数据并设置显示在操作栏的图书描述框bookDescText.setText((String)bookTable.getValueAt(row, 6));}/*** 图书查询事件处理* @param evt 事件对象*/private void searchActionPerformed(ActionEvent evt) {//获取查询的条件String bookName=s_bookNameText.getText();  //图书名称String author=s_authorText.getText();  //图书作者String bookTypeName=s_bookTypecomboBox.getSelectedItem().toString(); //图书类别//对图书类别"请选择..."换成""if("请选择...".equals(bookTypeName)){bookTypeName="";}//生成带有条件的图书对象Book book=new Book();book.setBookName(bookName);  //设置图书名称条件book.setAuthor(author);   //设置图书作者条件book.setBookTypeName(bookTypeName);  //设置图书类别条件//调用table填充函数,根据查询结果重新填充表格fillBookTable(book);  }/***  初始化图书类别下拉框* @param type 根据不同的参数填充不同的下拉框*/private void fillBookTypeComboBox(String type){//定义一个图书类别,用于存储查询的图书类别BookType s_bookType=null;//定义一个数据库连接Connection con=null;try {//获取数据库连接con=DBTool.getConnetion();//初始化图书类别访问数据对象bookTypeDao=new BookTypeDao();//查询图书类别,得到结果集ResultSet rs=bookTypeDao.search(con, new BookType());if("search".equals(type)){BookType bookType=new BookType();bookType.setBookTypeName("请选择...");bookType.setId(-1);s_bookTypecomboBox.addItem(bookType);}//遍历结果集while(rs.next()){ //如果有数据的话//初始化接受查询的图书类别s_bookType=new BookType();//根据查询结果设置ids_bookType.setId(rs.getInt("id"));//根据查询结果设置图书类别名称s_bookType.setBookTypeName(rs.getString("bookTypeName"));if("search".equals(type)){//将查询的图书类别添加到下拉框中s_bookTypecomboBox.addItem(s_bookType);}if("modify".equals(type)){//将查询的图书类别添加到表单操作下拉框中bookTypeComboBox.addItem(s_bookType);}}} catch (SQLException e) {//记录日志e.printStackTrace();throw new RuntimeException("初始化下拉框失败",e);}finally{//关闭数据库连接DBTool.close(con);}}/*** 初始化表格,列出所有的书籍* @param book*/private void fillBookTable(Book book){//获取表格的模型DefaultTableModel dtm=(DefaultTableModel) bookTable.getModel();//填充表格时设置成0行(相当于归零处理)dtm.setRowCount(0);//定义数据库连接Connection con=null;try {//获取数据库连接con=DBTool.getConnetion();//初始化图书数据访问对象bookDao=new BookDao();//按条件查询图书(这里没有条件,查出所有书籍)ResultSet rs=bookDao.search(con, book);//遍历查询结果while(rs.next()){//定义一个集合,由于存储图书信息Vector v=new Vector();v.add(rs.getInt("id"));  //添加编号v.add(rs.getString("bookName"));  //添加图书名称v.add(rs.getString("author"));  //添加图书作者v.add(rs.getString("sex"));     //添加作者性别v.add(rs.getFloat("price"));    //添加图书价格v.add(rs.getString("bookTypeName"));  //添加图书类别v.add(rs.getString("bookDesc"));//添加表格新行dtm.addRow(v);}} catch (SQLException e) {//记录日志e.printStackTrace();throw new RuntimeException("初始化表格失败",e);}finally{//关闭数据库连接DBTool.close(con);}}
}

⑥ BookAddInterFrame(图书添加界面)

package cn.ac.azure.view;import java.awt.EventQueue;
import java.awt.Font;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;import cn.ac.azure.dao.BookDao;
import cn.ac.azure.dao.BookTypeDao;
import cn.ac.azure.model.Book;
import cn.ac.azure.model.BookType;
import cn.ac.azure.util.DBTool;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;public class BookAddInterFrame extends JInternalFrame {private JTextField bookNameText;private JTextField authorText;private final ButtonGroup buttonGroup = new ButtonGroup();private JTextField priceText;private JComboBox bookTypeComboBox;private JRadioButton maleBtn;private JRadioButton femaleBtn;private JTextArea bookDescText;private BookTypeDao bookTypeDao;private BookDao bookDao;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {BookAddInterFrame frame = new BookAddInterFrame();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public BookAddInterFrame() {setIconifiable(true);setClosable(true);// 改变系统默认字体Font font = new Font("Dialog", Font.PLAIN, 12);java.util.Enumeration keys = UIManager.getDefaults().keys();while (keys.hasMoreElements()) {Object key = keys.nextElement();Object value = UIManager.get(key);if (value instanceof javax.swing.plaf.FontUIResource) {UIManager.put(key, font);}}setTitle("图书添加 ");setBounds(100, 100, 699, 449);JLabel label = new JLabel("图书名称:");bookNameText = new JTextField();bookNameText.setColumns(10);JLabel label_1 = new JLabel("图书作者:");authorText = new JTextField();authorText.setColumns(10);JLabel label_2 = new JLabel("作者性别:");maleBtn = new JRadioButton("男");buttonGroup.add(maleBtn);femaleBtn = new JRadioButton("女");buttonGroup.add(femaleBtn);JLabel label_3 = new JLabel("图书价格:");priceText = new JTextField();priceText.setColumns(10);JLabel label_4 = new JLabel("图书类别:");// 图书类别下拉框bookTypeComboBox = new JComboBox();JLabel label_5 = new JLabel("图书描述:");bookDescText = new JTextArea();// 图书添加按钮JButton addBtn = new JButton("添加");addBtn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {// 图书添加按钮事件处理bookAddActionPerformed(e);}});addBtn.setIcon(new ImageIcon(BookAddInterFrame.class.getResource("/images/add.png")));// 图书重置按钮JButton resetBtn = new JButton("重置");resetBtn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {bookResetActionPerformed(e);}});resetBtn.setIcon(new ImageIcon(BookAddInterFrame.class.getResource("/images/reset.png")));GroupLayout groupLayout = new GroupLayout(getContentPane());groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addGap(38).addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addGap(6).addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false).addGroup(groupLayout.createSequentialGroup().addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false).addGroup(groupLayout.createSequentialGroup().addComponent(label_4).addPreferredGap(ComponentPlacement.RELATED).addComponent(bookTypeComboBox, 0,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addGroup(groupLayout.createSequentialGroup().addComponent(label).addPreferredGap(ComponentPlacement.RELATED).addComponent(bookNameText, GroupLayout.PREFERRED_SIZE,116, GroupLayout.PREFERRED_SIZE)).addGroup(groupLayout.createSequentialGroup().addComponent(label_2).addPreferredGap(ComponentPlacement.RELATED).addComponent(maleBtn).addPreferredGap(ComponentPlacement.UNRELATED).addComponent(femaleBtn))).addGap(44).addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false).addGroup(groupLayout.createSequentialGroup().addComponent(label_3).addPreferredGap(ComponentPlacement.UNRELATED).addComponent(priceText)).addGroup(groupLayout.createSequentialGroup().addComponent(label_1).addPreferredGap(ComponentPlacement.RELATED).addComponent(authorText,GroupLayout.PREFERRED_SIZE, 128,GroupLayout.PREFERRED_SIZE)))).addGroup(groupLayout.createSequentialGroup().addComponent(label_5).addPreferredGap(ComponentPlacement.RELATED).addComponent(bookDescText))).addPreferredGap(ComponentPlacement.RELATED, 164, Short.MAX_VALUE)).addGroup(groupLayout.createSequentialGroup().addGap(94).addComponent(addBtn).addGap(96).addComponent(resetBtn))).addContainerGap()));groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addGap(32).addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(label).addComponent(bookNameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE).addComponent(label_1).addComponent(authorText, GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGap(31).addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(label_2).addComponent(maleBtn).addComponent(femaleBtn).addComponent(label_3).addComponent(priceText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE)).addGap(37).addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(label_4).addComponent(bookTypeComboBox, GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGap(30).addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(label_5).addComponent(bookDescText, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)).addGap(38).addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(addBtn).addComponent(resetBtn)).addContainerGap(45, Short.MAX_VALUE)));getContentPane().setLayout(groupLayout);// 设置文本域边框bookDescText.setBorder(new LineBorder(new java.awt.Color(127, 157, 185), 1, false));// 在构造函数中调用图书类别下拉框初始化方法fillBookTypeName();// 在构造函数中初始化性别。默认为男maleBtn.setSelected(true);}/*** 重置按钮事件处理* * @param evt*            重置按钮事件对象*/private void bookResetActionPerformed(ActionEvent evt) {reset();}/*** 图书添加界面信息重置*/private void reset() {bookNameText.setText("");authorText.setText("");maleBtn.setSelected(true);priceText.setText("");bookTypeComboBox.setSelectedIndex(0);bookDescText.setText("");}/*** 图书添加按钮事件处理* * @param evt*            添加事件对象*/private void bookAddActionPerformed(ActionEvent evt) {String bookName = bookNameText.getText(); // 获取图书名称if (bookName == null || "".equals(bookName.trim())) {JOptionPane.showMessageDialog(null, "图书名称不能为空!");return;}String author = authorText.getText(); // 获取图书作者String sex = null; // 获取图书作者性别if (maleBtn.isSelected()) {sex = "男";} else {sex = "女";}String prices = priceText.getText(); // 获取图书价格if (prices == null || "".equals(prices.trim())) {JOptionPane.showMessageDialog(null, "图书价格不能为空!");return;}float price = Float.parseFloat(prices);BookType bookType = (BookType) bookTypeComboBox.getSelectedItem(); // 获取图书类别int bookTypeId = bookType.getId(); // 获取图书类别idSystem.out.println("ID="+bookTypeId);String bookDesc = bookDescText.getText(); // 获取图书描述// 根据获取的添加图书界面获取的信息创建图书对象Book book = new Book(null, bookName, author, sex, price, bookTypeId, bookName, bookDesc);System.out.println("实体类:"+book);// 定义数据库连接Connection con = null;try {// 获取数据库连接con = DBTool.getConnetion();// 初始化图书数据访问对象bookDao = new BookDao();// 调用添加方法,向数据库添加书籍System.out.println("5555"+book);int num = bookDao.add(con, book);// 根据返回值判断图书是否添加成功if (num > 0) {JOptionPane.showMessageDialog(null, "图书添加成功n_n");// 添加成功之后重置界面reset();} else {JOptionPane.showMessageDialog(null, "图书添加成功u_u");}} catch (SQLException e) {// 记录日志e.printStackTrace();throw new RuntimeException("添加图书失败", e);} finally {// 关闭数据库连接DBTool.close(con);}}// 填充图书类别名称private void fillBookTypeName() {// 定义数据库连接对象Connection con = null;// 定义图书类别,用于查询和储存查询的书籍BookType bookType = null;try {// 获取数据库连接con = DBTool.getConnetion();// 初始化图书类别访问对象bookTypeDao = new BookTypeDao();// 查询t_bookType中含有的图书类别ResultSet rs = bookTypeDao.search(con, bookType);// 遍历查询结果while (rs.next()) {// 出事化图书类别bookType = new BookType();// 设置图书的idbookType.setId(rs.getInt("id"));// 设置图书的名称bookType.setBookTypeName(rs.getString("bookTypeName"));// 将图书类别对象添加到下拉框中(这里添加对象,便于获得id)bookTypeComboBox.addItem(bookType.getBookTypeName());}} catch (SQLException e) {// 记录日志e.printStackTrace();throw new RuntimeException("初始化列表失败", e);} finally {// 关闭数据路连接DBTool.close(con);}}
}

⑦ LibraryInterFrame(关于我们界面)

package cn.ac.azure.view;import java.awt.EventQueue;import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.ImageIcon;
import java.awt.Font;
import java.awt.Color;public class LibraryInterFrame extends JInternalFrame {private static final long serialVersionUID = 1L;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {LibraryInterFrame frame = new LibraryInterFrame();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public LibraryInterFrame() {//改变系统默认字体Font font = new Font("Dialog", Font.PLAIN, 12);java.util.Enumeration<Object> keys = UIManager.getDefaults().keys();while (keys.hasMoreElements()) {Object key = keys.nextElement();Object value = UIManager.get(key);if (value instanceof javax.swing.plaf.FontUIResource) {UIManager.put(key, font);}}setClosable(true);setIconifiable(true);setBounds(450, 150, 503, 300);JLabel label = new JLabel("");label.setIcon(new ImageIcon(LibraryInterFrame.class.getResource("/images/library.png")));JLabel label_1 = new JLabel("欢迎使用图书管理系统");label_1.setForeground(Color.GREEN);label_1.setBackground(Color.GREEN);label_1.setFont(new Font("宋体", Font.PLAIN, 20));GroupLayout groupLayout = new GroupLayout(getContentPane());groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addContainerGap(140, Short.MAX_VALUE).addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup().addComponent(label).addGap(175)).addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup().addComponent(label_1).addGap(137)))));groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addGap(39).addComponent(label).addGap(28).addComponent(label_1).addContainerGap(51, Short.MAX_VALUE)));getContentPane().setLayout(groupLayout);}
}

5、数据库【db_book】

/*Navicat Premium Data TransferSource Server         : 127.0.0.1Source Server Type    : MySQLSource Server Version : 50733Source Host           : localhost:3306Source Schema         : db_bookTarget Server Type    : MySQLTarget Server Version : 50733File Encoding         : 65001Date: 19/07/2021 17:34:44
*/SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;-- ----------------------------
-- Table structure for t_book
-- ----------------------------
DROP TABLE IF EXISTS `t_book`;
CREATE TABLE `t_book`  (`id` int(50) NOT NULL AUTO_INCREMENT,`bookName` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`author` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`sex` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`price` double(50, 2) NULL DEFAULT NULL,`bookTypeId` int(50) NULL DEFAULT NULL,`bookTypeName` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`bookDesc` varchar(5000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE,INDEX `fk_booktype`(`bookTypeId`) USING BTREE,CONSTRAINT `fk_booktype` FOREIGN KEY (`bookTypeId`) REFERENCES `t_booktype` (`id`) ON DELETE SET NULL ON UPDATE SET NULL
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of t_book
-- ----------------------------
INSERT INTO `t_book` VALUES (1, '《人间失格》', '(日)太宰治', '男', 66.00, 1, '小说', '(日本小说家太宰治代表作,一个对村上春树影响至深的绝望凄美故事)');
INSERT INTO `t_book` VALUES (2, '《三体》', '刘慈欣', '女', 55.80, 1, '小说', '刘慈欣代表作,亚洲首部“雨果奖”获奖作品!
《三体》第73届世界科幻雨果奖获奖作品,银河奖特别奖,《三体3》轨迹奖长篇科幻小说!2017年世界雨果奖提名作品。');
INSERT INTO `t_book` VALUES (3, '《人生海海》', '麦家', '男', 55.00, 2, '文化科学', '麦家重磅力作,莫言、董卿盛赞,连续两年高居各大畅销榜,发行量超180万册,罗一舟同款书)
上校赢了所有的仗,却败给一个不足道的秘密。茅盾文学奖得主麦家暌违8年,打磨5年,挑战常人不敢落笔之处,解密人性的荒唐与高尚。人生海海,何必在意一时沉浮!');
INSERT INTO `t_book` VALUES (4, '《大国崛起》', '唐晋', '男', 50.40, 2, '历史', '以历史的眼光和全球的视野解读15世纪以来9个世界性大国崛起的历史,中国能否成为第十个崛起的大国?');
INSERT INTO `t_book` VALUES (5, '《中华人民共和国民法典》', '法律出版社', '男', 8.10, 2, '哲学、社会', '民法典是新中国首部以“法典”命名的法律,是新时代我国社会主义法治建设的重大成果,是为百姓生活量身定制的权利宝典。自2021年1月1日起施行。');-- ----------------------------
-- Table structure for t_booktype
-- ----------------------------
DROP TABLE IF EXISTS `t_booktype`;
CREATE TABLE `t_booktype`  (`id` int(50) NOT NULL AUTO_INCREMENT,`bookTypeName` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`bookTypeDesc` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 25 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of t_booktype
-- ----------------------------
INSERT INTO `t_booktype` VALUES (1, 'A 马克思主义、列宁主义、毛泽东思想、邓小平理论', 'A 马克思主义、列宁主义、毛泽东思想、邓小平理论');
INSERT INTO `t_booktype` VALUES (2, 'B 哲学、宗教', 'B 哲学、宗教');
INSERT INTO `t_booktype` VALUES (3, 'C 社会科学总论', 'C 社会科学总论');
INSERT INTO `t_booktype` VALUES (4, 'D 政治、法律', 'D 政治、法律');
INSERT INTO `t_booktype` VALUES (5, 'F 经济', 'F 经济');
INSERT INTO `t_booktype` VALUES (6, 'G 文化、科学、教育、体育', 'G 文化、科学、教育、体育');
INSERT INTO `t_booktype` VALUES (7, 'H 语言、文字', 'H 语言、文字');
INSERT INTO `t_booktype` VALUES (8, 'I 文学', 'I 文学');
INSERT INTO `t_booktype` VALUES (9, 'J 艺术', 'J 艺术');
INSERT INTO `t_booktype` VALUES (10, 'K 历史、地理', 'K 历史、地理');
INSERT INTO `t_booktype` VALUES (11, 'N 自然科学总论', 'N 自然科学总论');
INSERT INTO `t_booktype` VALUES (12, 'O 数理科学和化学', 'O 数理科学和化学');
INSERT INTO `t_booktype` VALUES (13, 'Q 生物科学', 'Q 生物科学');
INSERT INTO `t_booktype` VALUES (14, 'R 医药、卫生  ', 'R 医药、卫生');
INSERT INTO `t_booktype` VALUES (15, 'S 农业科学', 'S 农业科学');
INSERT INTO `t_booktype` VALUES (16, 'T-TN 工业技术', 'T-TN 工业技术');
INSERT INTO `t_booktype` VALUES (17, 'TP 自动化技术、计算机技术', 'TP 自动化技术、计算机技术');
INSERT INTO `t_booktype` VALUES (18, 'TQ 化学工业', 'TQ 化学工业');
INSERT INTO `t_booktype` VALUES (19, 'TU 建筑科学', 'TU 建筑科学');
INSERT INTO `t_booktype` VALUES (20, 'TV 水利工程', 'TV 水利工程');
INSERT INTO `t_booktype` VALUES (21, 'U 交通运输', 'U 交通运输');
INSERT INTO `t_booktype` VALUES (22, 'V 航空、航天', 'V 航空、航天');
INSERT INTO `t_booktype` VALUES (23, 'X 环境科学、安全科学', 'X 环境科学、安全科学');
INSERT INTO `t_booktype` VALUES (24, 'Z 综合性图书', 'Z 综合性图书');-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user`  (`id` int(50) NOT NULL AUTO_INCREMENT,`username` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`password` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of t_user
-- ----------------------------
INSERT INTO `t_user` VALUES (1, '11', '123456');SET FOREIGN_KEY_CHECKS = 1;

三、项目地址:

CSDN赞助下载:

https://download.csdn.net/download/weixin_44893902/20367467

先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

基于Java swing+mysql+eclipse的【图书管理系统】相关推荐

  1. 基于Java+Swing+mysql餐厅点餐管理系统

    基于Java+Swing+mysql餐厅点餐管理系统 一.系统介绍 二.功能展示 1.用户登陆 2.用户注册(顾客) 3.顾客可以点餐 4.顾客可以查看订单信息 5.顾客可以修改个人信息 6.新增套餐 ...

  2. 基于Java+Swing+Mysql实现汽车信息管理系统

    基于Java+Swing+Mysql实现汽车信息管理系统 一.系统介绍 二.功能展示 1.登陆 2.车辆信息 3.车辆入库 4.车辆出库 5.车辆查询 6.车辆信息修改 三.数据库 四.其它 1.其他 ...

  3. 【Java课程设计】基于Java Swing+MySQL的学生基本信息管理系统----附git仓库地址

    一.项目简介 功能描述: 基于Java Swing+MySQL的学生基本信息管理系统,支持对学院.班级.学生信息的增删改查. 参考git地址或博客地址: https://www.bilibili.co ...

  4. 基于Java Swing+mysql的学生信息管理系统

    学生信息管理系统 学生管理系统目录 学生信息管理系统 一.前期工作 ①下载eclipse.mysql.navicat ②建立navicat与mysql的连接 二.明确项目的具体实现思路 ★系统功能分析 ...

  5. (解析+源码)基于JAVA Swing+MySQL实现学生信息管理系统(增、删、改、查)数据库/文件存储

    根据学校对学生信息日常管理需要,学生信息管理系统包括以下功能: 登录系统: 新建学生信息:添加学生信息: 删除学生信息:对指定学生信息进行删除: 修改学生信息:对指定学生信息进行修改 查找学生信息:输 ...

  6. 基于JAVA+Swing+MYSQL的水果超市管理系统

    项目功能: 销售员端: 登录注册 销售系统 个人信息 留言系统 退出系统 管理员端: 注册审核 货物管理 员工信息 留言管理 销售统计 退出系统 页面效果:

  7. 基于JAVA+Swing+MYSQL的在线订餐管理系统

    项目功能: 登录注册 在线点餐 订单查询 取消订单 菜单管理 订单管理 页面效果:

  8. 基于JAVA+Swing+MYSQL的电影院购票管理系统

    项目功能: 使用三层架构实现电影购票系统,分用户和管理员,用户功能:展示电影,查找电影(模糊查询),查看电影详情,查找场次,购买影票,订制座位,退订影票等功能,界面美观漂亮,逻辑严谨,附加电影评论功能 ...

  9. 基于Java+Swing+mysql图书管理系统

    基于Java+Swing+mysql图书管理系统 一.系统介绍 二.功能展示 1.用户登陆 2.图书管理 3.图书添加 4.图书类别管理 5.图书类别添加 三.数据库 四.其它 1.其他系统实现 五. ...

  10. 基于Java+Swing+Mysql员工信息管理系统

    基于Java+Swing+Mysql员工信息管理系统 一.系统介绍 二.功能展示 1.主页 2.查询员工信息 3.删除员工信息 三.数据库 四.其他系统实现 五.获取源码 一.系统介绍 该系统实现了查 ...

最新文章

  1. Spring|AOP
  2. AOV网拓扑排序(c/c++)
  3. Spring Boot 是什么,有什么用。
  4. qcow2 磁盘在线扩容方法
  5. postgresql-int,bigint,numeric效率测试
  6. node历史版本下载
  7. 东农计算机应用与技术,东农16春《计算机应用与技术》在线作业.doc
  8. 现实世界的Windows Azure:采访Transparencia Sp. z o.o的Grzegorz Skowron-Moszkowicz
  9. win98/win95
  10. 全新版大学英语综合教程第二册学习笔记(原文及全文翻译)——1 - Learning, Chinese-Style(中国式的学习风格)
  11. Metaverse 元宇宙入门-06-Interchange Tools + Standards and the Metaverse 交换工具+标准和元界
  12. mysql系统找不到指定文件_mysql安装常见问题(系统找不到指定的文件、发生系统错误......
  13. 什么是Window【What Is a Window?】
  14. #2阴阳师首页模块模拟
  15. 数学计算机 分数乘法,分数乘法的计算方法
  16. 数据结构之图(三)——邻接表
  17. 《数据库系统概论》:DBA的职责有些
  18. Win7有多条隧道适配器(isatap、teredo、6to4)的原因及关闭方法(转)
  19. 2021-2025年中国InGaAs光电二极管及阵列行业市场供需与战略研究报告
  20. vue+springboot

热门文章

  1. php微信公众号采集器,WordPress微信公众号采集插件
  2. 动态物体检测(python)
  3. 一文理解 Windows 身份验证原理
  4. iOS两个强制旋转屏幕的方法
  5. 韩昊 20190919-1 每周例行报告
  6. 两招快速教会你们PDF怎么转图片jpg格式
  7. wps多出来的页面怎么办?wps怎么删除不要的页
  8. 架构师主要做些什么,你知道吗?
  9. Android特效专辑(九)——仿微信雷达搜索好友特效,逻辑清晰实现简单
  10. beetl模板使用场景_Beetl使用指南