medicine_information.java~52~

来自「使用Java语言开发的数据库课程设计——医药管理信息系统。」· JAVA~52~ 代码 · 共 363 行

JAVA~52~
363
字号
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.Cursor;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Dimension;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.event.WindowAdapter;
import java.awt.Color;
import java.awt.event.WindowEvent;
import java.awt.FlowLayout;


public class Medicine_information extends JFrame
{
    DefaultTableModel tableModel1 = null;
    String Driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
    String conURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=CASY";
    String Username = "sa";
    String Password = "111111";
    String sql = null;

    Vector header;
    Vector data;
    String header_name[] = {"药品编号","药品名称","通用名称","药品类别","有效期","单位","剂型",
                            "包装数量","产地","零售价格","进货价格","批发价格","库存量"};
    JPanel jPanel1 = new JPanel();
    BorderLayout borderLayout2 = new BorderLayout();
    JScrollPane jScrollPane1 = new JScrollPane();
    JTable jTable1 = new JTable();
    JButton jButton1 = new JButton();
    JLabel jLabel1 = new JLabel();
    JButton jButton2 = new JButton();
    JButton jButton3 = new JButton();
    JButton jButton4 = new JButton();
    BorderLayout borderLayout1 = new BorderLayout();
    JPanel jPanel2 = new JPanel();
    FlowLayout flowLayout1 = new FlowLayout();
    JButton jButton5 = new JButton();
    JButton jButton6 = new JButton();
    JButton jButton7 = new JButton();
    public Medicine_information()
    {
        this.setSize(600,350);
        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);

        try
        {
            jbInit();
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
       new Load_DB_Driver();
       sql = "SELECT * FROM Medicine_Data";
       add_table_data(sql);
        this.setVisible(true);
    }


    private void jbInit() throws Exception
    {
        getContentPane().setLayout(borderLayout1);
        jPanel1.setLayout(borderLayout2);
        jTable1.setFont(new java.awt.Font("宋体", Font.PLAIN, 14));
        jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        jTable1.setRowHeight(18);
        jTable1.setRowMargin(2);
        jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.
                                                  HORIZONTAL_SCROLLBAR_ALWAYS);
        jButton1.setFont(new java.awt.Font("宋体", Font.PLAIN, 14));
        jButton1.setPreferredSize(new Dimension(90, 35));
        jButton1.setText("药品编号");
        jButton1.addActionListener(new
                                   Medicine_information_jButton1_actionAdapter(this));
        jLabel1.setFont(new java.awt.Font("宋体", Font.BOLD, 16));
        jLabel1.setText("排序:");
        jPanel1.setBackground(new Color(236, 233, 173));
        jButton2.setFont(new java.awt.Font("宋体", Font.PLAIN, 14));
        jButton2.setPreferredSize(new Dimension(90, 35));
        jButton2.setText("药品名称");
        jButton2.addActionListener(new
                                   Medicine_information_jButton2_actionAdapter(this));
        jButton3.setFont(new java.awt.Font("宋体", Font.PLAIN, 14));
        jButton3.setPreferredSize(new Dimension(90, 35));
        jButton3.setText("通用名称");
        jButton3.addActionListener(new
                                   Medicine_information_jButton3_actionAdapter(this));
        jButton4.setFont(new java.awt.Font("宋体", Font.PLAIN, 14));
        jButton4.setPreferredSize(new Dimension(90, 35));
        jButton4.setText("药品类别");
        jButton4.addActionListener(new
                                   Medicine_information_jButton4_actionAdapter(this));
        jPanel2.setMinimumSize(new Dimension(800, 50));
        jPanel2.setPreferredSize(new Dimension(800, 50));
        jButton5.setFont(new java.awt.Font("宋体", Font.PLAIN, 14));
        jButton5.setPreferredSize(new Dimension(90, 35));
        jButton5.setText("零售价格");
        jButton5.addActionListener(new
                                   Medicine_information_jButton5_actionAdapter(this));
        jButton6.setFont(new java.awt.Font("宋体", Font.PLAIN, 14));
        jButton6.setPreferredSize(new Dimension(90, 35));
        jButton6.setText("进货价格");
        jButton6.addActionListener(new
                                   Medicine_information_jButton6_actionAdapter(this));
        jButton7.setFont(new java.awt.Font("宋体", Font.PLAIN, 14));
        jButton7.setPreferredSize(new Dimension(90, 35));
        jButton7.setText("库存量");
        jButton1.setCursor(new Cursor(Cursor.HAND_CURSOR));
        jButton2.setCursor(new Cursor(Cursor.HAND_CURSOR));
        jButton3.setCursor(new Cursor(Cursor.HAND_CURSOR));
        jButton4.setCursor(new Cursor(Cursor.HAND_CURSOR));
        jButton5.setCursor(new Cursor(Cursor.HAND_CURSOR));
        jButton6.setCursor(new Cursor(Cursor.HAND_CURSOR));
        jButton7.setCursor(new Cursor(Cursor.HAND_CURSOR));
        jButton7.addActionListener(new
                                   Medicine_information_jButton7_actionAdapter(this));
        this.getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);
        jPanel2.setBackground(new Color(236, 233, 173));
        jPanel2.setLayout(flowLayout1);
        this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);
        jScrollPane1.getViewport().add(jTable1);
        jPanel2.add(jLabel1, null);
        jPanel2.add(jButton1, null);
        jPanel2.add(jButton2, null);
        jPanel2.add(jButton3, null);
        jPanel2.add(jButton4, null);
        jPanel2.add(jButton5);
        jPanel2.add(jButton6);
        jPanel2.add(jButton7);
        header = new Vector();
        for(int i=0;i<header_name.length;i++)
        {
            header.addElement(header_name[i]);
        }
        tableModel1 = new DefaultTableModel(header,0);
        jTable1.setModel(tableModel1);
        this.validate();
    }

    public void init_model()
    {
        header = new Vector();
        for(int i=0;i<header_name.length;i++)
        {
            header.addElement(header_name[i]);
        }
        tableModel1 = new DefaultTableModel(header,0);
        jTable1.setModel(tableModel1);
    }

    public void add_table_data(String sql)
    {
        try
           {
               Connection con = DriverManager.getConnection(conURL, Username,
                       Password);
               Statement s = con.createStatement();
               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));
                   data.addElement(rs.getString(8));
                   data.addElement(rs.getString(9));
                   data.addElement(rs.getString(10));
                   data.addElement(rs.getString(11));
                   data.addElement(rs.getString(12));
                   data.addElement(rs.getString(13));
                   tableModel1.addRow(data);
               }
               jTable1.setModel(tableModel1);
               rs.close();
               con.close();
           }
           catch(SQLException ex)
           {
               JOptionPane.showMessageDialog(null,"SQLException:" + ex.getMessage(),"警告!",JOptionPane.WARNING_MESSAGE);
           }
    }


    public static void main(String[] args)
    {
        new Medicine_information();
    }

    public void jButton1_actionPerformed(ActionEvent e)
    {
        init_model();
        sql="select * from Medicine_Data order by 药品编号";
        add_table_data(sql);
    }

    public void jButton2_actionPerformed(ActionEvent e)
    {
        init_model();
        sql="select * from Medicine_Data order by 药品名称";
        add_table_data(sql);
    }

    public void jButton3_actionPerformed(ActionEvent e)
    {
        init_model();
        sql="select * from Medicine_Data order by 通用名称";
        add_table_data(sql);
    }

    public void jButton4_actionPerformed(ActionEvent e)
    {
        init_model();
        sql="select * from Medicine_Data order by 药品类别";
        add_table_data(sql);
    }

    public void jButton5_actionPerformed(ActionEvent e)
    {
        init_model();
        sql="select * from Medicine_Data order by 零售价格";
        add_table_data(sql);
    }

    public void jButton6_actionPerformed(ActionEvent e)
    {
        init_model();
        sql="select * from Medicine_Data order by 进货价格";
        add_table_data(sql);
    }

    public void jButton7_actionPerformed(ActionEvent e)
    {
        init_model();
        sql="select * from Medicine_Data order by 库存量";
        add_table_data(sql);
    }
}


class Medicine_information_jButton7_actionAdapter implements ActionListener {
    private Medicine_information adaptee;
    Medicine_information_jButton7_actionAdapter(Medicine_information adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton7_actionPerformed(e);
    }
}


class Medicine_information_jButton5_actionAdapter implements ActionListener {
    private Medicine_information adaptee;
    Medicine_information_jButton5_actionAdapter(Medicine_information adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton5_actionPerformed(e);
    }
}


class Medicine_information_jButton6_actionAdapter implements ActionListener {
    private Medicine_information adaptee;
    Medicine_information_jButton6_actionAdapter(Medicine_information adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton6_actionPerformed(e);
    }
}


class Medicine_information_jButton4_actionAdapter implements ActionListener {
    private Medicine_information adaptee;
    Medicine_information_jButton4_actionAdapter(Medicine_information adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton4_actionPerformed(e);
    }
}


class Medicine_information_jButton3_actionAdapter implements ActionListener {
    private Medicine_information adaptee;
    Medicine_information_jButton3_actionAdapter(Medicine_information adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton3_actionPerformed(e);
    }
}


class Medicine_information_jButton1_actionAdapter implements ActionListener {
    private Medicine_information adaptee;
    Medicine_information_jButton1_actionAdapter(Medicine_information adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
    }
}


class Medicine_information_jButton2_actionAdapter implements ActionListener {
    private Medicine_information adaptee;
    Medicine_information_jButton2_actionAdapter(Medicine_information adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton2_actionPerformed(e);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?