纯干货,直接展示。
界面展示:
Java源码:
package 暑假作业1;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
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 Addframe extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
// public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// Addframe frame = new Addframe();
// frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
// }
/**
* Create the frame.
*/
public Addframe() {
setTitle("\u529E\u516C\u7528\u54C1\u7C7B\u522B");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("\u7C7B\u522B\u7F16\u7801");
lblNewLabel.setBounds(104, 61, 54, 26);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("\u7C7B\u522B\u540D\u79F0");
lblNewLabel_1.setBounds(104, 110, 54, 26);
contentPane.add(lblNewLabel_1);
textField = new JTextField();
textField.setBounds(171, 60, 155, 29);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(171, 109, 155, 29);
contentPane.add(textField_1);
textField_1.setColumns(10);
JButton btnNewButton = new JButton("\u4FDD\u5B58");// 保存
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String st1 = textField.getText();
String st2 = textField_1.getText();
if(st1.equals("")||st2.equals("")) {
JOptionPane.showConfirmDialog(null, "插入为空!", "提示", JOptionPane.PLAIN_MESSAGE);
}else {
Dao dao = new Dao();
int result = dao.Add(st1, st2);
if (result > 0) {
JOptionPane.showConfirmDialog(null, "插入成功!", "提示", JOptionPane.PLAIN_MESSAGE);
Mainframe mainframe=new Mainframe();
mainframe.setVisible(true);
Addframe.this.dispose();
} else {
JOptionPane.showConfirmDialog(null, "插入失败!", "提示", JOptionPane.PLAIN_MESSAGE);
}
}
textField.setText("");
textField_1.setText("");
}
});
btnNewButton.setBounds(152, 170, 93, 35);
contentPane.add(btnNewButton);
}
}
package 暑假作业1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class Dao {
public int Add(String st1, String st2) { // 添加
int result = 0;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/shujia?serverTimezone=GMT%2B8&useSSL=false";
Connection connection = DriverManager.getConnection(url, "root", "123456");
PreparedStatement psta = connection.prepareStatement("insert into category values(?,?)");
psta.setString(1, st1);
psta.setString(2, st2);
result = psta.executeUpdate();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
package 暑假作业1;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTable;
import java.awt.CardLayout;
import java.awt.Event;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;
public class Inqframe extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTable table_1;
DefaultTableModel model;
/**
* Launch the application.
*/
// public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// Inqframe frame = new Inqframe();
// frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
// }
/**
* Create the frame.
*/
public Inqframe() {
setTitle("\u529E\u516C\u7528\u54C1\u67E5\u8BE2");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 485, 385);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("\u529E\u516C\u7528\u54C1\u7F16\u53F7");
lblNewLabel.setBounds(44, 47, 85, 15);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("\u529E\u516C\u7528\u54C1\u540D\u79F0");
lblNewLabel_1.setBounds(44, 96, 85, 15);
contentPane.add(lblNewLabel_1);
textField = new JTextField();
textField.setBounds(139, 41, 195, 28);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(139, 90, 195, 28);
contentPane.add(textField_1);
textField_1.setColumns(10);
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(null, "\u529E\u516C\u7528\u54C1\u4FE1\u606F", TitledBorder.LEADING,
TitledBorder.TOP, null, null));
panel.setBounds(34, 171, 399, 169);
contentPane.add(panel);
panel.setLayout(new BorderLayout(0, 0));
String[] title = { "编号", "名称" };
Object[][] rowData = { { "xxx", "xxx" } };
model = new DefaultTableModel(rowData, title);
//设置不可编辑
table_1 = new JTable(model) {
public boolean isCellEditable(int rowIndex,int ColumName) {
return false;
}
};
table_1.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mousePressed(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mouseExited(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2) {
int row =table_1.rowAtPoint(e.getPoint());
int col=table_1.columnAtPoint(e.getPoint());
String x=table_1.getValueAt(row, col).toString();
Xinframe xinframe=new Xinframe();
xinframe.setVisible(true);
Inqframe.this.dispose();
xinframe.Xin(x);
}
}
});
JScrollPane jScrollPane = new JScrollPane(table_1);
// panel.add(table_1.getTableHeader(),BorderLayout.NORTH);
// panel.add(table_1, BorderLayout.CENTER);
panel.add(jScrollPane);
JButton btnNewButton = new JButton("\u67E5\u8BE2");
btnNewButton.setBounds(240, 138, 93, 28);
contentPane.add(btnNewButton);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String st1 = textField.getText();
String st2 = textField_1.getText();
model.setRowCount(0);
Inq(st1, st2);
table_1.invalidate();
textField.setText("");
textField_1.setText("");
}
});
}
public void Inq(String st1, String st2) {// 查询
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/shujia?serverTimezone=GMT%2B8&useSSL=false";
Connection connection = DriverManager.getConnection(url, "root", "123456");
if (st1.equals("") && st2.equals("")) {
PreparedStatement psta = connection.prepareStatement("select * from product");
ResultSet rs = psta.executeQuery();
while (rs.next()) {
Object[] obj = { rs.getObject("product_id"), rs.getObject("product_name") };
model.addRow(obj);
}
rs.close();
connection.close();
} else {
PreparedStatement psta = connection
.prepareStatement("select * from product where product_id=? or product_name=?");
psta.setString(1, st1);
psta.setString(2, st2);
ResultSet rs = psta.executeQuery();
if (rs.next()) {
Object[] obj = { rs.getObject("product_id"), rs.getObject("product_name") };
model.addRow(obj);
} else {
JOptionPane.showConfirmDialog(null, "查询为空!", "提示", JOptionPane.PLAIN_MESSAGE);
}
rs.close();
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package 暑假作业1;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Mainframe extends JFrame {
private JPanel contentPane;
/**
* 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() {
setTitle("\u901A\u8FBE\u529E\u516C\u81EA\u52A8\u5316\u7CFB\u7EDF");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("\u6DFB\u52A0\u529E\u516C\u7528\u54C1\u7C7B\u522B");// 添加
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Addframe addframe = new Addframe();
addframe.setVisible(true);
Mainframe.this.dispose();
}
});
btnNewButton.setBounds(122, 70, 155, 36);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("\u67E5\u8BE2\u529E\u516C\u7528\u54C1");// 查询
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Inqframe inqframe = new Inqframe();
inqframe.setVisible(true);
Mainframe.this.dispose();
}
});
btnNewButton_1.setBounds(122, 140, 155, 36);
contentPane.add(btnNewButton_1);
}
}
package 暑假作业1;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Label;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.awt.event.ActionEvent;
public class Xinframe extends JFrame {
private JPanel contentPane;
JTextPane textPane1;
JTextPane textPane2;
JTextPane textPane3;
JTextPane textPane4;
JTextPane textPane5;
/**
* Launch the application.
*/
// public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// Xinframe frame = new Xinframe();
// frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
// }
/**
* Create the frame.
*/
public Xinframe() {
setTitle("\u529E\u516C\u7528\u54C1\u8BE6\u7EC6\u4FE1\u606F");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 423, 375);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("\u529E\u516C\u7528\u54C1\u7F16\u53F7");
lblNewLabel.setBounds(39, 56, 88, 15);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("\u540D\u79F0");
lblNewLabel_1.setBounds(39, 99, 54, 15);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("\u7C7B\u522B");
lblNewLabel_2.setBounds(39, 148, 54, 15);
contentPane.add(lblNewLabel_2);
JLabel lblNewLabel_3 = new JLabel("\u6570\u91CF");
lblNewLabel_3.setBounds(39, 196, 54, 15);
contentPane.add(lblNewLabel_3);
JLabel lblNewLabel_4 = new JLabel("\u5355\u4EF7");
lblNewLabel_4.setBounds(39, 237, 54, 15);
contentPane.add(lblNewLabel_4);
textPane1 = new JTextPane();
textPane1.setEditable(false);//设置不可编辑
textPane1.setBounds(137, 56, 107, 21);
contentPane.add(textPane1);
textPane2 = new JTextPane();
textPane2.setEditable(false);
textPane2.setBounds(137, 99, 107, 21);
contentPane.add(textPane2);
textPane3 = new JTextPane();
textPane3.setEditable(false);
textPane3.setBounds(137, 148, 107, 21);
contentPane.add(textPane3);
textPane4 = new JTextPane();
textPane4.setEditable(false);
textPane4.setBounds(137, 190, 107, 21);
contentPane.add(textPane4);
textPane5 = new JTextPane();
textPane5.setEditable(false);
textPane5.setBounds(137, 231, 107, 21);
contentPane.add(textPane5);
JButton btnNewButton = new JButton("\u5173\u95ED");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Inqframe inqframe = new Inqframe();
inqframe.setVisible(true);
Xinframe.this.dispose();
}
});
btnNewButton.setBounds(225, 292, 93, 23);
contentPane.add(btnNewButton);
}
public void Xin(String x) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/shujia?serverTimezone=GMT%2B8&useSSL=false";
Connection connection = DriverManager.getConnection(url, "root", "123456");
PreparedStatement psta = connection.prepareStatement("select * from product where product_id=? or product_name=?");
psta.setString(1,x);
psta.setString(2, x);
ResultSet rs = psta.executeQuery();
if (rs.next()) {
textPane1.setText(rs.getString("product_id"));
textPane2.setText(rs.getString("product_name"));
textPane3.setText(rs.getString("category_id"));
textPane4.setText(rs.getString("product_number"));
textPane5.setText(rs.getString("product_price"));
}
rs.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
数据库源码:
create database shujia;
use shujia;
create table category(
category_id varchar(20) not null primary key,
category_name varchar(20) not null
);
create table product(
product_id varchar(20) not null primary key,
product_name varchar(20) not null,
category_id varchar(20) not null,
product_number int not null,
product_price float not null
);
insert into category values("001","文具");
insert into category values("002","耗材");
insert into category values("003","纸张");
insert into product values("001","签字笔","001","2","1.0");
insert into product values("002","2B铅笔","001","4","1.5");
insert into product values("003","订书机","001","5","25.0");