allocate_history.java~9~
来自「使用Java语言开发的数据库课程设计——医药管理信息系统。」· JAVA~9~ 代码 · 共 278 行
JAVA~9~
278 行
import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.Insets;
import java.awt.Font;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Allocate_history extends JFrame
{
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
BorderLayout borderLayout2 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTable jTable1 = new JTable();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JButton jButton3 = new JButton();
JLabel jLabel2 = new JLabel();
JButton jButton6 = new JButton();
JButton jButton2 = new JButton();
String Driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String conURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=CASY";
String Username = "sa";
String Password = "111111";
String Order = null;
Vector header;
Vector data;
DefaultTableModel tableModel1 = null;
public Allocate_history()
{
this.setSize(702, 400);
this.setResizable(false);
this.setTitle("历史开销明细");
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
new mainWin();
dispose();
}
});
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
init_table();
new Load_DB_Driver();
try
{
jbInit();
}
catch (Exception exception)
{
exception.printStackTrace();
}
this.setVisible(true);
}
private void jbInit() throws Exception
{
getContentPane().setLayout(borderLayout1);
jPanel2.setBackground(new Color(236, 233, 173));
jPanel2.setBorder(BorderFactory.createLoweredBevelBorder());
jPanel2.setPreferredSize(new Dimension(10, 45));
jPanel2.setLayout(xYLayout1);
jPanel1.setLayout(borderLayout2);
jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.
HORIZONTAL_SCROLLBAR_ALWAYS);
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jButton1.setFont(new java.awt.Font("宋体", Font.PLAIN, 14));
jButton1.setMargin(new Insets(5, 14, 5, 14));
jButton1.setText("医生名称");
jButton1.addActionListener(new Allocate_history_jButton1_actionAdapter(this));
jButton3.setFont(new java.awt.Font("宋体", Font.PLAIN, 14));
jButton3.setMargin(new Insets(5, 18, 5, 18));
jButton3.setText("流水号");
jButton3.addActionListener(new Allocate_history_jButton3_actionAdapter(this));
jLabel2.setFont(new java.awt.Font("宋体", Font.BOLD, 14));
jLabel2.setText("排序:");
jButton6.setFont(new java.awt.Font("宋体", Font.PLAIN, 14));
jButton6.setActionCommand("供应科室");
jButton6.setMargin(new Insets(5, 18, 5, 18));
jButton6.setText("药品名称");
jButton6.addActionListener(new Allocate_history_jButton6_actionAdapter(this));
jButton2.setFont(new java.awt.Font("宋体", Font.PLAIN, 14));
jButton2.setText("开单日期");
jButton2.addActionListener(new Allocate_history_jButton2_actionAdapter(this));
this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
this.getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);
jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTable1);
jPanel2.add(jLabel2, new XYConstraints(20, 12, -1, -1));
jPanel2.add(jButton3, new XYConstraints(111, 5, 98, -1));
jPanel2.add(jButton2, new XYConstraints(262, 5, 98, 31));
jPanel2.add(jButton6, new XYConstraints(412, 5, 98, -1));
jPanel2.add(jButton1, new XYConstraints(563, 5, 98, -1));
jPanel1.validate();
jPanel2.validate();
try
{
Connection con = DriverManager.getConnection(conURL,Username,Password);
Statement s= con.createStatement();
String sql = "select * from Medicine_Allocation";
ResultSet rs = s.executeQuery(sql);
while(rs.next())
{
data = new Vector();
data.addElement(rs.getString(1));
data.addElement(rs.getString(2));
data.addElement(rs.getString(3));
data.addElement(rs.getString(4));
data.addElement(rs.getString(5));
data.addElement(rs.getString(6));
data.addElement(rs.getString(7));
tableModel1.addRow(data);
jTable1.setModel(tableModel1);
}
s.close();
con.close();
}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(null,"SQLException:" + ex.getMessage(),"警告!",JOptionPane.WARNING_MESSAGE);
}
this.validate();
}
public void init_table()
{
header = new Vector();
header.addElement("流水号");
header.addElement("药品编号");
header.addElement("药品名称");
header.addElement("供应科室");
header.addElement("医生名称");
header.addElement("供应数量");
header.addElement("开单日期");
tableModel1 = new DefaultTableModel(header,0);
jTable1.setModel(tableModel1);
for(int i= 0;i<7;i++)
jTable1.getColumnModel().getColumn(i).setPreferredWidth(100);
}
public void add_table_data(String Order)
{
try
{
Connection con = DriverManager.getConnection(conURL, Username,
Password);
Statement s = con.createStatement();
ResultSet rs = s.executeQuery(Order);
while (rs.next())
{
data = new Vector();
data.addElement(rs.getString(1));
data.addElement(rs.getString(2));
data.addElement(rs.getString(3));
data.addElement(rs.getString(4));
data.addElement(rs.getString(5));
data.addElement(rs.getString(6));
data.addElement(rs.getString(7));
tableModel1.addRow(data);
}
jTable1.setModel(tableModel1);
rs.close();
con.close();
}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(null,
"SQLException:" + ex.getMessage(),
"警告!",
JOptionPane.WARNING_MESSAGE);
}
}
public void jButton3_actionPerformed(ActionEvent e)
{
init_table();
Order = "select * from Medicine_Allocation order by 流水号";
add_table_data(Order);
}
public void jButton2_actionPerformed(ActionEvent e)
{
init_table();
Order = "select * from Medicine_Allocation order by 开单日期";
add_table_data(Order);
}
public void jButton6_actionPerformed(ActionEvent e)
{
init_table();
Order = "select * from Medicine_Allocation order by 药品名称";
add_table_data(Order);
}
public void jButton1_actionPerformed(ActionEvent e)
{
init_table();
Order = "select * from Medicine_Allocation order by 医生名称";
add_table_data(Order);
}
}
class Allocate_history_jButton1_actionAdapter implements ActionListener {
private Allocate_history adaptee;
Allocate_history_jButton1_actionAdapter(Allocate_history adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class Allocate_history_jButton6_actionAdapter implements ActionListener {
private Allocate_history adaptee;
Allocate_history_jButton6_actionAdapter(Allocate_history adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton6_actionPerformed(e);
}
}
class Allocate_history_jButton2_actionAdapter implements ActionListener {
private Allocate_history adaptee;
Allocate_history_jButton2_actionAdapter(Allocate_history adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class Allocate_history_jButton3_actionAdapter implements ActionListener {
private Allocate_history adaptee;
Allocate_history_jButton3_actionAdapter(Allocate_history adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?