基于Oracle数据库的图书管理系统可以使用Java Swing实现可视化界面,下面是一个简单的示例代码。
1.创建数据库和表
首先需要创建一个数据库,例如 library
,然后创建用于存储图书信息的表 book
,包括 book_id
、book_name
、author
、publisher
、publish_date
等字段。建议使用主键来唯一标识每一本图书。
CREATE TABLE book (
book_id NUMBER(10) PRIMARY KEY,
book_name VARCHAR2(50),
author VARCHAR2(50),
publisher VARCHAR2(50),
publish_date DATE
);
2.开发Java程序
使用JDBC驱动连接Oracle数据库,并实现基本的增删改查等功能。同时,使用Java Swing实现可视化界面,方便用户输入和查看图书信息。
具体代码示例如下:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class BookManager extends JFrame {
private JPanel contentPane;
private JTable table;
private JTextField tfId;
private JTextField tfName;
private JTextField tfAuthor;
private JTextField tfPublisher;
private JTextField tfDate;
// 数据库连接参数
private static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:orcl";
private static final String DB_USER = "scott";
private static final String DB_PASSWORD = "tiger";
// 数据库连接对象、会话对象和结果集对象
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
/**
* 连接并初始化数据库
*/
public void init() {
try {
// 加载JDBC驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
// 获取数据库连接
conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
// 创建会话对象
stmt = conn.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 关闭数据库连接和会话对象
*/
public void close() {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 查询图书信息
*/
public void query() {
try {
String sql = "SELECT * FROM book";
rs = stmt.executeQuery(sql);
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.setRowCount(0); // 清空表格数据重新显示
while (rs.next()) {
int id = rs.getInt("book_id");
String name = rs.getString("book_name");
String author = rs.getString("author");
String publisher = rs.getString("publisher");
Date date = rs.getDate("publish_date");
Vector<Object> row = new Vector<Object>();
row.add(id);
row.add(name);
row.add(author);
row.add(publisher);
row.add(date);
model.addRow(row);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 添加图书信息
*/
public void add(int id, String name, String author, String publisher, Date date) {
try {
String sql = "INSERT INTO book VALUES (" + id + ", '" + name + "', '" + author + "', '" + publisher + "', '" + date + "')";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "添加成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 修改图书信息
*/
public void update(int id, String name, String author, String publisher, Date date) {
try {
String sql = "UPDATE book SET book_name = '" + name + "', author = '" + author + "', publisher = '" + publisher + "', publish_date = '" + date + "' WHERE book_id = " + id;
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "修改成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 删除图书信息
*/
public void delete(int id) {
try {
String sql = "DELETE FROM book WHERE book_id = " + id;
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "删除成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BookManager frame = new BookManager();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public BookManager() {
initComponents();
init();
query();
}
private void initComponents() {
setTitle("图书管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 10, 760, 400);
contentPane.add(scrollPane);
table = new JTable();
table.setModel(new DefaultTableModel(
new Object[][] {
},
new String[] {
"编号", "书名", "作者", "出版社", "出版日期"
}
));
scrollPane.setViewportView(table);
JLabel lblId = new JLabel("编号:");
lblId.setBounds(10, 430, 60, 25);
contentPane.add(lblId);
tfId = new JTextField();
tfId.setBounds(80, 430, 100, 25);
contentPane.add(tfId);
tfId.setColumns(10);
JLabel lblName = new JLabel("书名:");
lblName.setBounds(200, 430, 60, 25);
contentPane.add(lblName);
tfName = new JTextField();
tfName.setBounds(270, 430, 120, 25);
contentPane.add(tfName);
tfName.setColumns(10);
JLabel lblAuthor = new JLabel("作者:");
lblAuthor.setBounds(410, 430, 60, 25);
contentPane.add(lblAuthor);
tfAuthor = new JTextField();
tfAuthor.setBounds(480, 430, 120, 25);
contentPane.add(tfAuthor);
tfAuthor.setColumns(10);
JLabel lblPublisher = new JLabel("出版社:");
lblPublisher.setBounds(620, 430, 60, 25);
contentPane.add(lblPublisher);
tfPublisher = new JTextField();
tfPublisher.setBounds(690, 430, 80, 25);
contentPane.add(tfPublisher);
tfPublisher.setColumns(10);
JLabel lblDate = new JLabel("出版日期:");
lblDate.setBounds(10, 480, 80, 25);
contentPane.add(lblDate);
tfDate = new JTextField();
tfDate.setBounds(80, 480, 100, 25);
contentPane.add(tfDate);
tfDate.setColumns(10);
JButton btnAdd = new JButton("添加");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(tfId.getText());
String name = tfName.getText();
String author = tfAuthor.getText();
String publisher = tfPublisher.getText();
Date date = Date.valueOf(tfDate.getText());
add(id, name, author, publisher, date);
query();
}
});
btnAdd.setBounds(270, 480, 80, 25);
contentPane.add(btnAdd);
JButton btnUpdate = new JButton("修改");
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(tfId.getText());
String name = tfName.getText();
String author = tfAuthor.getText();
String publisher = tfPublisher.getText();
Date date = Date.valueOf(tfDate.getText());
update(id, name, author, publisher, date);
query();
}
});
btnUpdate.setBounds(410, 480, 80, 25);
contentPane.add(btnUpdate);
JButton btnDelete = new JButton("删除");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(tfId.getText());
delete(id);
query();
}
});
btnDelete.setBounds(550, 480, 80, 25);
contentPane.add(btnDelete);
}
}
以上代码演示了如何使用Java程序连接Oracle数据库,并实现基本的增删改查图书信息操作。同时,使用Java Swing实现可视化界面,方便用户输入和查看图书信息。