本项目为Java swing项目,在工作环境中基本使用不到,但是很多学校把这个当做编程入门的项目来做,故分享出本项目供初学者参考。
CSDN赞助下载:https://download.csdn.net/download/weixin_44893902/20367467 白嫖:加QQ68872185
一、效果演示:
主要功能:
①基本数据维护: 图书类别管理 >> 图书类别添加、图书类别维护 图书管理 >> 图书添加、图书维护 ②关于我们
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;
public class DBTool {
private static String driver;
private static String url;
private static String user;
private static String password;
static {
Properties p=new Properties ();
String path="cn//ac//azure//util//db.properties" ;
try {
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);
}
}
public static Connection getConnetion () throws SQLException{
return DriverManager.getConnection(url, user, password);
}
public static void close (Connection con) {
if (con!=null ){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException ("数据库关闭失败" ,e);
}
}
}
}
② db.properties(配置文件)
2、model包 【存放实体类】
① Book(图书实体类)
package cn.ac.azure.model;
public class Book {
private Integer id;
private String bookName;
private String author;
private String sex;
private Float price;
private Integer bookTypeId;
private 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;
}
@Override
public 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;
public class BookType {
private int id;
private 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;
}
@Override
public String toString () {
return "BookType [id=" + id + ", bookTypeName=" + bookTypeName + ", bookTypeDesc=" + bookTypeDesc + "]" ;
}
}
③ User(用户实体类)
package cn.ac.azure.model;
public class User {
private int id;
private String username;
private String password;
public User () {
}
@Override
public 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;
public class BookDao {
public int add (Connection con,Book book) throws SQLException{
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);
PreparedStatement ps=con.prepareStatement(sql);
return ps.executeUpdate();
}
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+"%' " );
}
String sql=sb.toString();
PreparedStatement ps=con.prepareStatement(sql);
return ps.executeQuery();
}
public int update (Connection con,Book book) throws SQLException{
String sql="update t_book set bookName=?,author=?,sex=?,"
+ "price=?,bookTypeId=?,bookDesc=? where id=?" ;
PreparedStatement ps=con.prepareStatement(sql);
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());
ps.setString(6 , book.getBookDesc());
ps.setInt(7 , book.getId());
return ps.executeUpdate();
}
public int delete (Connection con,int id) throws SQLException{
String sql="delete from t_book where id=?" ;
PreparedStatement ps=con.prepareStatement(sql);
ps.setInt(1 , id);
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;
public class BookTypeDao {
public int add (Connection con,BookType bookType) throws SQLException{
String sql="insert into t_bookType values(null,?,?)" ;
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1 , bookType.getBookTypeName());
ps.setString(2 , bookType.getBookTypeDesc());
return ps.executeUpdate();
}
public ResultSet search (Connection con,BookType bookType) throws SQLException{
String bookTypeName=null ;
if (bookType!=null ){
bookTypeName=bookType.getBookTypeName();
}
StringBuilder sb=new StringBuilder ("select * from t_bookType" );
if (!(bookTypeName==null || "" .equals(bookTypeName.trim()))){
sb.append(" and bookTypeName like '%" +bookType.getBookTypeName()+"%'" );
}
String sql=sb.toString().replaceFirst("and" , "where" );
PreparedStatement ps=con.prepareStatement(sql);
return ps.executeQuery();
}
public int update (Connection con,BookType bookType) throws SQLException{
String sql="update t_bookType set bookTypeName=? , bookTypeDesc=? where id=?" ;
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1 ,bookType.getBookTypeName());
ps.setString(2 ,bookType.getBookTypeDesc());
ps.setInt(3 , bookType.getId());
return ps.executeUpdate();
}
public int delete (Connection con,String id) throws SQLException{
String sql="delete from t_bookType where id=?" ;
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1 , id);
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;
public class UserDao {
public User login (Connection con,User user) throws SQLException{
User resultUser=null ;
String sql="select * from t_user where username=? and password=?" ;
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1 , user.getUsername());
ps.setString(2 , user.getPassword());
ResultSet rs=ps.executeQuery();
while (rs.next()){
resultUser=new User ();
resultUser.setId(rs.getInt("id" ));
resultUser.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.smart.SmartLookAndFeel" );
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
private JPanel contentPane;
private JTextField usernameText;
private JPasswordField passwordText;
private JButton loginBtn;
private JButton resetBtn;
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();
}
}
});
}
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 ("\u56FE\u4E66\u7BA1\u7406\u7CFB\u7EDF" );
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 );
}
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);
}
}
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.smart.SmartLookAndFeel" );
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
private static final long serialVersionUID = 1L ;
private JPanel contentPane;
private JDesktopPane table;
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();
}
}
});
}
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("\u56FE\u4E66\u7BA1\u7406\u7CFB\u7EDF\u4E3B\u754C\u9762" );
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100 , 100 , 450 , 300 );
JMenuBar menuBar = new JMenuBar ();
menuBar.setToolTipText("" );
setJMenuBar(menuBar);
JMenu menu = new JMenu ("\u57FA\u672C\u6570\u636E\u7EF4\u62A4 " );
menu.setIcon(new ImageIcon (MainFrame.class.getResource("/images/base.png" )));
menuBar.add(menu);
JMenu mnNewMenu = new JMenu ("\u56FE\u4E66\u7C7B\u522B\u7BA1\u7406 " );
mnNewMenu.setIcon(new ImageIcon (MainFrame.class.getResource("/images/bookTypeManager.png" )));
menu.add(mnNewMenu);
JMenuItem menuItem = new JMenuItem ("\u56FE\u4E66\u7C7B\u522B\u6DFB\u52A0" );
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 ("\u56FE\u4E66\u7C7B\u522B\u7EF4\u62A4" );
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 ("\u56FE\u4E66\u7BA1\u7406" );
menu_1.setIcon(new ImageIcon (MainFrame.class.getResource("/images/bookManager.png" )));
menu.add(menu_1);
JMenuItem menuItem_2 = new JMenuItem ("\u56FE\u4E66\u6DFB\u52A0" );
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 ("\u56FE\u4E66\u7EF4\u62A4" );
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 ("\u5B89\u5168\u9000\u51FA" );
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 ("\u5173\u4E8E\u6211\u4EEC" );
menu_2.setIcon(new ImageIcon (MainFrame.class.getResource("/images/about.png" )));
menuBar.add(menu_2);
JMenuItem menuItem_5 = new JMenuItem ("\u5173\u4E8E\u6211\u4EEC" );
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;
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();
}
}
});
}
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 , "\u8868\u5355\u64CD\u4F5C" , 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 () {
@Override
public 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 ());
}
private void bookTypeDeleteActionPerformed (ActionEvent evt) {
String 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);
}
}
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();
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);
}
}
private void bookTypeTableMousePressed (MouseEvent e) {
int row=bookTypeTable.getSelectedRow();
idText.setText(String.valueOf(bookTypeTable.getValueAt(row, 0 )));
bookTypeNameText.setText((String)bookTypeTable.getValueAt(row, 1 ));
bookTypeDescText.setText((String)bookTypeTable.getValueAt(row, 2 ));
}
private void searchActionPerformed (ActionEvent evt) {
String s_bookTypeName=s_bookTypeNameText.getText();
BookType bookType=new BookType ();
bookType.setBookTypeName(s_bookTypeName);
fillTable(bookType);
}
private void fillTable (BookType bookType) {
DefaultTableModel dtm=(DefaultTableModel) bookTypeTable.getModel();
dtm.setRowCount(0 );
Connection con=null ;
try {
con=DBTool.getConnetion();
ResultSet rs=bookTypeDao.search(con, bookType);
while (rs.next()){
Vector v=new Vector ();
v.add(rs.getInt("id" ));
v.add(rs.getString("bookTypeName" ));
v.add(rs.getString("bookTypeDesc" ));
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;
public class BookTypeAddInterFrame extends JInternalFrame {
private JTextField bookTypeNameText;
private JTextArea bookTypeDescText;
private JButton resetBtn;
private JButton addBtn;
private BookTypeDao bookTypeDao;
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();
}
}
});
}
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 ));
}
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();
bookTypeDao=new BookTypeDao ();
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);
}
}
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;
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();
}
}
});
}
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 () {
@Override
public 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 ());
}
private void modifyBookActionPerformed (ActionEvent evt) {
String id=idText.getText();
String bookName=bookNameText.getText();
String author=authorText.getText();
String sex="男" ;
if (femaleBtn.isSelected()){
sex="女" ;
}
String price=priceText.getText();
BookType bookType=(BookType)bookTypeComboBox.getSelectedItem();
Integer bookTypeId=bookType.getId();
String bookDesc=bookDescText.getText();
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 ){
JOptionPane.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 deleteBookActionPerformed (ActionEvent evt) {
String id=idText.getText();
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 ){
JOptionPane.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("" );
}
private void tableMousePressed (MouseEvent evt) {
int row=bookTable.getSelectedRow();
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 ));
}
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);
fillBookTable(book);
}
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 ();
s_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);
}
}
private void fillBookTable (Book book) {
DefaultTableModel dtm=(DefaultTableModel) bookTable.getModel();
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;
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();
}
}
});
}
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 );
}
private void bookResetActionPerformed (ActionEvent evt) {
reset();
}
private void reset () {
bookNameText.setText("" );
authorText.setText("" );
maleBtn.setSelected(true );
priceText.setText("" );
bookTypeComboBox.setSelectedIndex(0 );
bookDescText.setText("" );
}
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();
System.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 ();
ResultSet rs = bookTypeDao.search(con, bookType);
while (rs.next()) {
bookType = new BookType ();
bookType.setId(rs.getInt("id" ));
bookType.setBookTypeName(rs.getString("bookTypeName" ));
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 ;
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();
}
}
});
}
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】
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0 ;
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 ;
INSERT INTO `t_book` VALUES (1 , '《人间失格》' , '(日)太宰治' , '男' , 66.00 , 1 , '*说' , '(日本*说家太宰治代表作,一个对村上春树影响至深的绝望凄美故事)' );
INSERT INTO `t_book` VALUES (2 , '《三体》' , '刘慈欣' , '女' , 55.80 , 1 , '*说' , '刘慈欣代表作,亚洲首部“雨果奖”获奖作品!\r\n《三体》第73届世界科幻雨果奖获奖作品,银河奖特别奖,《三体3》轨迹奖长篇科幻*说!2017年世界雨果奖提名作品。' );
INSERT INTO `t_book` VALUES (3 , '《人生海海》' , '麦家' , '男' , 55.00 , 2 , '文化科学' , '麦家重磅力作,莫言、董卿盛赞,连续两年高居各大畅销榜,发行量超180万册,罗一舟同款书)\r\n上校赢了所有的仗,却败给一个不足道的秘密。茅盾文学奖得主麦家暌违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日起施行。' );
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 ;
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 综合性图书' );
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 ;
INSERT INTO `t_user` VALUES (1 , '11' , '123456' );
SET FOREIGN_KEY_CHECKS = 1 ;
三、项目地址:
CSDN赞助下载:
https://download.csdn.net/download/weixin_44893902/20367467