import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.ResultSet;
//import java.beans.Statement;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.border.EmptyBorder;
public class DengLu extends JFrame implements ActionListener
{
private JPanel contentPane;
private JPanel imagePanel;
private ImageIcon background;
private JTextField textField_1;
private JPasswordField textField_2;
private JRadioButton rdb1= new JRadioButton("\u7BA1\u7406\u5458");//管理员
private JRadioButton rdb2= new JRadioButton("\u9500\u552E\u5458");//销售员
private JRadioButton rdb3= new JRadioButton("\u4ED3\u5E93\u5458");//仓库员
private JButton button = new JButton("\u767B\u5F55");//登录
private JButton button_1 = new JButton("\u6CE8\u518C");//注册
String id;
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
DengLu frame = new DengLu();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
public DengLu()
{
//图形界面
super("登录");
setForeground(UIManager.getColor("Button.disabledForeground"));
setBackground(Color.LIGHT_GRAY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 400);
contentPane = new JPanel();
contentPane.setBackground(Color.LIGHT_GRAY);
contentPane.setForeground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
Box box0=Box.createVerticalBox();
box0.add(Box.createVerticalStrut(20));
Box box1=Box.createHorizontalBox();
JLabel label = new JLabel("\u5DE5\u827A\u82B1\u5E97\u7BA1\u7406\u7CFB\u7EDF");
label.setForeground(new Color(0, 0, 0));
label.setFont(new Font("楷体_GB2312", Font.BOLD, 40));
box1.add(label);
box0.add(box1);
box0.add(Box.createVerticalStrut(30));
Box box2=Box.createHorizontalBox();
rdb1.setBackground(Color.WHITE);
rdb1.setFont(new Font("宋体", Font.BOLD, 20));
box2.add(rdb1);
box2.add(Box.createRigidArea(new Dimension(40,0)));
rdb2.setBackground(Color.WHITE);
rdb2.setFont(new Font("宋体", Font.BOLD, 20));
box2.add(rdb2);
box2.add(Box.createRigidArea(new Dimension(40,0)));
rdb3.setBackground(Color.WHITE);
rdb3.setFont(new Font("宋体", Font.BOLD, 20));
box2.add(rdb3);
ButtonGroup bg=new ButtonGroup();
bg.add(rdb1);
bg.add(rdb2);
bg.add(rdb3);
box0.add(box2);
box0.add(Box.createVerticalStrut(20));
Box box3=Box.createHorizontalBox();
box3.add(Box.createRigidArea(new Dimension(40,0)));
JLabel label1 = new JLabel("ID:");
label1.setFont(new Font("宋体", Font.BOLD, 30));
box3.add(label1);
textField_1 = new JTextField();
textField_1.setFont(new Font("宋体", Font.BOLD, 30));
box3.add(textField_1);
textField_1.setColumns(10);
box3.add(textField_1);
box3.add(Box.createRigidArea(new Dimension(10,0)));
JLabel label2 = new JLabel("password:");
label2.setFont(new Font("宋体", Font.BOLD, 30));
box3.add(label2);
textField_2 = new JPasswordField(5);
textField_2.setFont(new Font("宋体", Font.PLAIN, 30));
textField_2.setEchoChar('*');
box3.add(textField_2);
textField_2.setColumns(10);
box3.add(Box.createRigidArea(new Dimension(40,0)));
box0.add(box3);
box0.add(Box.createVerticalStrut(20));
Box box4=Box.createHorizontalBox();
button.setFont(new Font("宋体", Font.BOLD, 20));
box4.add(button);
box4.add(Box.createRigidArea(new Dimension(40,0)));
button_1.setFont(new Font("宋体", Font.BOLD, 20));
box4.add(button_1);
box0.add(box4);
box0.add(Box.createVerticalStrut(40));
getContentPane().add(box0);
//按钮监听
button.addActionListener(this);
button_1.addActionListener(this);
rdb1.addActionListener(this);
rdb2.addActionListener(this);
rdb3.addActionListener(this);
//设置背景
background = new ImageIcon("H:\\090801214陈海霞 工艺花店管理系统\\鲜花\\背景2.jpg");
JLabel l = new JLabel(background);// 把背景图片显示在一个标签里面
// 把标签的大小位置设置为图片刚好填充整个面板
l.setBounds(0, 0, background.getIconWidth(),background.getIconHeight());
// 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
imagePanel = (JPanel) this.getContentPane();
imagePanel.setOpaque(false);
// 内容窗格默认的布局管理器为BorderLayout
//imagePanel.setLayout(new FlowLayout());
this.getLayeredPane().setLayout(null);
// 把背景图片添加到分层窗格的最底层作为背景
this.getLayeredPane().add(l, new Integer(Integer.MIN_VALUE));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(background.getIconWidth(), background.getIconHeight());
this.setResizable(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
try
{
//连接数据库
Connection con;
String user = "sa";
String password = "";
String url = "jdbc:sqlserver://localhost:1433;databaseName=flowers";
con = DriverManager.getConnection(url, user, password);
//ID
ResultSet rs;
String sql;
Statement st;
st =con.createStatement();
sql = "select ID from users" ;
rs = st.executeQuery(sql);
//password
ResultSet rs1;
String sql1;
Statement st1;
st1 = con.createStatement();
sql1 = "select password from users" ;
rs1 = st1.executeQuery(sql1);
//post
ResultSet rs2;
String sql2;
Statement st2;
st2 = con.createStatement();
sql2 = "select post from users" ;
rs2 = st2.executeQuery(sql2);
if(e.getActionCommand().compareTo("登录")==0)
{
int i=1;
//管理员登录
if(rdb1.isSelected())
{
while(rs2.next()&&rs1.next()&&rs.next())
{
if(rs.getString(1).trim().equals(textField_1.getText().trim())&&rs1.getString(1).trim().equals(textField_2.getText().trim())&&rs2.getString(1).trim().equals("管理员"))
{
id=textField_1.getText();
dispose();
setVisible(false);
GuanLiYuan g=new GuanLiYuan(id);
g.setVisible(true);
i--;
break;
}
}
}
//销售员登录
if(rdb2.isSelected())
{
while(rs2.next()&&rs1.next()&&rs.next())
{
if(rs.getString(1).trim().equals(textField_1.getText().trim())&&rs1.getString(1).trim().equals(textField_2.getText().trim())&&rs2.getString(1).trim().equals("销售员"))
{
id=textField_1.getText();
dispose();
setVisible(false);
XiaoShouGuanLi x=new XiaoShouGuanLi(id);
x.setVisible(true);
i--;
break;
}
}
}
//仓库员登录
if(rdb3.isSelected())
{
while(rs2.next()&&rs1.next()&&rs.next())
{
if(rs.getString(1).trim().equals(textField_1.getText().trim())&&rs1.getString(1).trim().equals(textField_2.getText().trim())&&rs2.getString(1).trim().equals("仓库员"))
{
id=textField_1.getText();
dispose();
setVisible(false);
CangKuGuanLi c=new CangKuGuanLi(id);
c.setVisible(true);
i--;
break;
}
}
}