以下是关于这个数据库的相关信息
这四张表分别对应了售卖药品后所卖客户的信息,买药或售药后药品的存储信息,药品 的相关信息 包括生产日期 ,年限,名称等,员工信息.
之后便是JDBC,JDBC是java连接数据库的工具类 方便利用java语言实现数据库的增删查改,对于数据库操作的相关命令 不要忘了导入sqlserver的jar包,同时在url那一行不要忘记数据库的名字.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
public class JDBC {
private String name="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=first";
private Connection con;
private Statement st;
public JDBC(){
try{
Class.forName(name);
con=DriverManager.getConnection(url, "sa","95uoh16j");
}catch(Exception e){
System.out.println( e.toString());
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public Vector select(String sqls){
Vector row=new Vector<>();
Vector hang =new Vector<>();
int sum=0;
try {
st=con.createStatement();
ResultSet rs=st.executeQuery(sqls);
while(rs.next()){
hang.add(rs.getString(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
hang.add(rs.getDate(4));
hang.add(rs.getInt(5));
hang.add(rs.getString(6));
hang.add(rs.getFloat(7));
hang.add(rs.getInt(8));
}
String s="select *from 库存信息 where 药品编号='"+hang.get(0)+"'";
rs=st.executeQuery(s);
while(rs.next()){
sum=sum+rs.getInt(3)-rs.getInt(4);
}
hang.add(7, sum);
row.add(hang);
rs.close();
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return row;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public Vector selectPassword(String sqls){
Vector row=new Vector<>();
try {
st=con.createStatement();
ResultSet rs=st.executeQuery(sqls);
rs.next();
row.add(rs.getString(1));
row.add(rs.getString(2));
row.add(rs.getString(3));
row.add(rs.getInt(4));
row.add(rs.getString(5));
row.add(rs.getString(6));
row.add(rs.getString(7));
rs.close();
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return row;
}
public void update_1(Object values[]){
String sqls="update 药品信息 set 入库数量=入库数量+"+Integer.valueOf(values[1].toString()).intValue()+" where 药品编号='"+values[0].toString()+"'";
String sql="update 库存信息 set 入库数量=入库数量+"+Integer.valueOf(values[1].toString()).intValue()+" where 药品编号='"+values[0].toString()+"'";
try {
st=con.createStatement();
st.execute(sqls);
st.execute(sql);
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void update_2(Object values[]){
String sqls="update 库存信息 set 出库数量=出库数量+"+Integer.valueOf(values[1].toString()).intValue()+" where 药品编号='"+values[0].toString()+"'";
try {
st=con.createStatement();
st.execute(sqls);
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void send_1(String sqls ,Object []values){
try{
PreparedStatement prepare=con.prepareStatement(sqls);
prepare.setString(1, (String)values[0]);
prepare.setString(2, (String)values[1]);
prepare.setString(3, (String)values[2]);
prepare.setString(4, (String)values[3]);
prepare.setString(5, (String)values[4]);
prepare.setInt(6, (int)values[5]);
prepare.execute();
prepare.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void send_2(String sqls ,Object []values){
try{
PreparedStatement prepare=con.prepareStatement(sqls);
prepare.setString(1, (String)values[0]);
prepare.setString(2, (String)values[1]);
prepare.setString(3, (String)values[2]);
prepare.setInt(4, (int)values[3]);
prepare.setString(5, (String)values[4]);
prepare.setString(6, (String)values[5]);
prepare.setString(7, (String )values[6]);
prepare.execute();
prepare.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
/**
* 本类是专门写主页面
*
* */
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class MainInterface extends JFrame implements ActionListener{
JPanel con=new JPanel();
private JLabel title;
private JLabel use;
private JLabel password;
private JTextField t_use;
private JTextField t_password;
private JButton sure;
private JButton register;
JDBC jdbc;
public MainInterface(){
con.setLayout(new BorderLayout());
JPanel jp=new JPanel(null);
use=new JLabel("用户名 : ");
use.setBounds(160,130,100,50);
t_use=new JTextField();
t_use.setBounds(220, 135,150,30);
password=new JLabel("密码 : ");
password.setBounds(160,190,100,50);
t_password=new JTextField();
t_password.setBounds(220,200,150,30);
sure=new JButton("确定");
sure.setBounds(200,250,60,30);
sure.addActionListener(this);
register=new JButton("注册");
register.setBounds(320,250,60,30);
register.addActionListener(this);
title=new JLabel("药品库");
title.setFont(new Font("华文行楷", Font.PLAIN, 40));
title.setLocation(100,50);
title.setSize(400,40);
jp.add(title);
jp.add(use);
jp.add(t_use);
jp.add(password);
jp.add(t_password);
jp.add(sure);
jp.add(register);
con.add(jp,BorderLayout.CENTER);
this.add(con);
this.setBounds(350, 100, 0, 0);
this.setName("药品进销信息系统");
this.setSize(600,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@SuppressWarnings("unchecked")
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==sure){
Vector
/**
* 此类为注册界面
*/
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class Register extends JFrame implements ActionListener{
Container con=this.getContentPane();
private JLabel num;
private JLabel name;
private JLabel sex;
private JLabel age;
private JLabel edu;
private JLabel work;
private JLabel password;
private JTextField t_num;
private JTextField t_name;
private JTextField t_sex;
private JTextField t_age;
private JTextField t_edu;
private JTextField t_work;
private JTextField t_password;
private JButton sure;
public Register(){
con.setLayout(null);
num=new JLabel("员工编号");
num.setBounds(160,50,100,30);
t_num=new JTextField();
t_num.setBounds(240,50,150,30);
name=new JLabel("姓名");
name.setBounds(160,100,100,30);
t_name=new JTextField();
t_name.setBounds(240,100,150,30);
sex=new JLabel("性别");
sex.setBounds(160,150,100,30);
t_sex=new JTextField();
t_sex.setBounds(240,150,150,30);
age=new JLabel("年龄");
age.setBounds(160,200,100,30);
t_age=new JTextField();
t_age.setBounds(240,200,150,30);
edu=new JLabel("学历");
edu.setBounds(160,250,100,30);
t_edu=new JTextField();
t_edu.setBounds(240,250,150,30);
work=new JLabel("职务");
work.setBounds(160,300,100,30);
t_work=new JTextField();
t_work.setBounds(240,300,150,30);
password=new JLabel("密码");
password.setBounds(160,350,100,30);
t_password=new JTextField();
t_password.setBounds(240,350,150,30);
sure=new JButton("┼");
sure.setBounds(290,400,50,50);
sure.addActionListener(this);
con.add(num);
con.add(t_num);
con.add(name);
con.add(t_name);
con.add(sex);
con.add(t_sex);
con.add(age);
con.add(t_age);
con.add(edu);
con.add(t_edu);
con.add(work);
con.add(t_work);
con.add(password);
con.add(t_password);
con.add(sure);
}
@Override
public void actionPerformed(ActionEvent arg0) {
Object v[]={t_num.getText(),
t_name.getText(),
t_sex.getText(),
Integer.valueOf(t_age.getText()),//返回一个 Integer对象,保存指定的值。
t_edu.getText(),
t_work.getText(),
t_password.getText()};
JDBC jdbc=new JDBC();
String sqls="insert into 员工信息 values(?,?,?,?,?,?,?)";
jdbc.send_2(sqls, v);
this.dispose();
new MainInterface();
}
}
/**
* 此类为查询界面
*/
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class SelectInterface extends JFrame implements ActionListener{
Container con=this.getContentPane();
private JLabel welcome;
private String world;
private JButton select;
private JTextField text;
public SelectInterface(String title){
con.setLayout(null);
world="欢迎您 "+title;
welcome=new JLabel(world);
welcome.setBounds(0,0,500,30);
text=new JTextField();
text.setBounds(160,140,180,30);
select=new JButton("Q");
select.setBounds(360,130,45,45);
select.addActionListener(this);
con.add(welcome);
con.add(text);
con.add(select);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==select){
String sqls,sqls1,sqls2;
char[] ss=text.getText().toCharArray();
if(ss[0]>=48&& ss[0]<=57){
sqls1="select * from 药品信息 where 药品编号='";
sqls2=text.getText().toString()+"'";
sqls=sqls1+sqls2;
}else{
sqls1="select * from 药品信息 where 药品名称='";
sqls2=text.getText().toString()+"'";
sqls=sqls1+sqls2;
}
SelectResult result=new SelectResult(sqls);
result.setBounds(350, 100, 600, 400);
result.setVisible(true);
result.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.dispose();
}
}
}
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import sun.net.www.content.image.jpeg;
@SuppressWarnings("serial")
public class SelectResult extends JFrame implements ActionListener{
JPanel con = new JPanel();
@SuppressWarnings("rawtypes")
Vector row,column;
String sql;
JDBC jdbc = new JDBC();
private JTable table;
private JButton buy;
private JButton sell;
@SuppressWarnings({ "unchecked"})
public SelectResult(String sqls){
this.sql=sqls;
column=new Vector<>();
column.add("药品编号");
column.add("药品名称");
column.add("生产厂家");
column.add("生产日期");
column.add("保质期");
column.add("用途");
column.add("价格");
column.add("数量");
try{
row=jdbc.select(sqls);
table=new JTable(row,column);
JScrollPane js=new JScrollPane(table);
JPanel jp=new JPanel();
buy=new JButton("购进");
sell=new JButton("售出");
jp.add(buy);
jp.add(sell);
buy.addActionListener(this);
sell.addActionListener(this);
con.setLayout(new BorderLayout());
con.add(jp,BorderLayout.SOUTH);
con.add(js,BorderLayout.CENTER);
this.add(con);
this.setBounds(350, 100, 600, 400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==buy){//如果最后的选择是买
BuyInterface buyInterface=new BuyInterface(row);
buyInterface.setTitle("购入表");
buyInterface.setBounds(500, 100, 0, 0);
buyInterface.setSize(600, 500);
buyInterface.setVisible(true);
buyInterface.setSql(sql);
this.dispose();
buyInterface.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}else if(e.getSource()==sell){//如果选择售出
SellInterface sellInterface=new SellInterface(sql);
sellInterface.setTitle("售出表");
sellInterface.setBounds(500, 100, 0, 0);
sellInterface.setSize(600, 500);
sellInterface.setVisible(true);
this.dispose();
sellInterface.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class SellInterface extends JFrame implements ActionListener{
Container con=this.getContentPane();
private JLabel customer;
private JLabel customerNum;
private JLabel phone;
private JLabel drug;
private JLabel drugNum;
private JLabel num;
private JTextField t_customer;
private JTextField t_customerNum;
private JTextField t_phone;
private JTextField t_drug;
private JTextField t_drugNum;
private JTextField t_num;
private JButton sure;
String sql;
public SellInterface(String sql){
this.sql=sql;
con.setLayout(null);
customer=new JLabel("客户姓名");
customer.setBounds(160,50,100,30);
t_customer=new JTextField();
t_customer.setBounds(240,50,150,30);
customerNum=new JLabel("客户编号");
customerNum.setBounds(160,100,100,30);
t_customerNum=new JTextField();
t_customerNum.setBounds(240,100,150,30);
phone=new JLabel("联系方式");
phone.setBounds(160,150,100,30);
t_phone=new JTextField();
t_phone.setBounds(240,150,150,30);
drug=new JLabel("购买药品");
drug.setBounds(160,200,100,30);
t_drug=new JTextField();
t_drug.setBounds(240,200,150,30);
drugNum=new JLabel("药品编号");
drugNum.setBounds(160,250,100,30);
t_drugNum=new JTextField();
t_drugNum.setBounds(240,250,150,30);
num=new JLabel("数量");
num.setBounds(160,300,100,30);
t_num=new JTextField();
t_num.setBounds(240,300,150,30);
sure=new JButton("┼");
sure.setBounds(290,350,50,50);
sure.addActionListener(this);
con.add(customer);
con.add(t_customer);
con.add(customerNum);
con.add(t_customerNum);
con.add(phone);
con.add(t_phone);
con.add(drug);
con.add(t_drug);
con.add(drugNum);
con.add(t_drugNum);
con.add(num);
con.add(t_num);
con.add(sure);
}
@Override
public void actionPerformed(ActionEvent e) {
Object v[]={t_customerNum.getText(),t_customer.getText(),t_phone.getText(),t_drugNum.getText(),t_drug.getText(),Integer.valueOf(t_num.getText())};
JDBC jdbc=new JDBC();
String sqls="insert into 客户信息 values(?,?,?,?,?,?)";
jdbc.send_1(sqls, v);
Object values[]={t_drugNum.getText(),Integer.valueOf(t_num.getText())};
jdbc.update_2( values);
this.dispose();
new SelectResult(sql);
}
}
/**
* 此类为买药品的界面
*/
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class BuyInterface extends JFrame implements ActionListener{
Container con=this.getContentPane();
private JLabel drug;
private JTextField t_drug;
private JLabel number;
private JTextField t_number;
private JButton sure;
public String sql;
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
JDBC jdbc;
@SuppressWarnings("rawtypes")
Vector r;
@SuppressWarnings("rawtypes")
public BuyInterface(Vector row){
r=row;
con.setLayout(null);
drug=new JLabel("药品编号");
drug.setBounds(100,50,150,100);
t_drug=new JTextField();
t_drug.setBounds(200,89,150,25);
number =new JLabel("购入数量");
number.setBounds(100, 120, 150, 100);
t_number=new JTextField();
t_number.setBounds(200, 159, 150, 25);
sure=new JButton("┼");
sure.setBounds(250, 340, 50, 50);
con.add(number);
con.add(t_number);
con.add(drug);
con.add(t_drug);
con.add(sure);
sure.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
jdbc=new JDBC();
Object values[]={t_drug.getText(),t_number.getText()};
jdbc.update_1(values);
this.dispose();
new SelectResult(sql);
}
}
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
MainInterface face=new MainInterface();
}
}
之后就可以运行了 如果有错误 希望指出 谢谢!