📄 landload.java
字号:
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import java.sql.*;
public class landload {
private JPasswordField passwordField;
private JComboBox comboBox;
private JTextField textField;
private JFrame frame;
/**
* Launch the application
* @param args
*/
public static Connection getConnection() throws SQLException,java.lang.ClassNotFoundException
{
String url="jdbc:mysql://localhost:3306/optimizesystem";
Class.forName("com.mysql.jdbc.Driver");
String userName="root";
String password="1234";
Connection con=DriverManager.getConnection(url, userName, password);
return con;
}
public static void main(String args[]) {
new landload().frame.setVisible(true);
}
/**
* Create the application
*/
public landload() {
initialize();
}
/**
* Initialize the contents of the frame
*/
private void initialize() {
frame = new JFrame("系统登陆");
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 471, 339);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel label = new JLabel();
label.setFont(new Font("", Font.BOLD, 16));
label.setText("系统用户登陆");
label.setBounds(178, 50, 109, 22);
frame.getContentPane().add(label);
final JLabel label_1 = new JLabel();
label_1.setFont(new Font("", Font.PLAIN, 14));
label_1.setText("类 别:");
label_1.setBounds(105, 110, 60, 15);
frame.getContentPane().add(label_1);
final JLabel label_2 = new JLabel();
label_2.setFont(new Font("", Font.PLAIN, 14));
label_2.setText("用户名:");
label_2.setBounds(105, 145, 60, 15);
frame.getContentPane().add(label_2);
final JLabel label_3 = new JLabel();
label_3.setFont(new Font("", Font.PLAIN, 14));
label_3.setText("密 码:");
label_3.setBounds(105, 180, 60, 15);
frame.getContentPane().add(label_3);
textField = new JTextField();
textField.setBounds(190, 145, 109, 21);
frame.getContentPane().add(textField);
String ComboBoxStr[]={"系统管理员","系统操作员","用户"};
comboBox = new JComboBox(ComboBoxStr);
comboBox.setBounds(190, 110, 109, 21);
frame.getContentPane().add(comboBox);
passwordField = new JPasswordField();
passwordField.setBounds(190, 180, 112, 21);
frame.getContentPane().add(passwordField);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent t) {
int kind=(int)comboBox.getSelectedIndex()+1;
String name=textField.getText();
String pwd=passwordField.getText();
try{
Connection con=getConnection();
Statement stmt=con.createStatement();
String query="select * from login where name='"+name+"' and password='"+pwd+"' and kind='"+kind+"'";
ResultSet rs=stmt.executeQuery(query);
if(name.equals("")&&pwd.equals(""))
{
Object[] options={"是(y)","否(n)","取消",};
String str1=new String("警告!用户名或密码不能为空,请重新输入!");
JOptionPane.showOptionDialog(null,str1,"警告窗口",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
}
else{
if(rs.next()){
int k=Integer.parseInt(rs.getString("kind"));
String n=rs.getString("name");
String p=rs.getString("password");
if((p.equals(pwd))&&(n.equals(name))&&(k==(kind))){
if(k==1)
{
managesystem window = new managesystem();
window.frame.setVisible(true);
}
if(k==2)
{
managesystem_1 window = new managesystem_1();
window.frame.setVisible(true);
}
if(k==3)
{
managesystem_2 window = new managesystem_2();
window.frame.setVisible(true);
}
else
{
return;
}
}
else{
Object[] options={"是(y)","否(n)","取消",};
String str1=new String("连接数据库失败!");
JOptionPane.showOptionDialog(null,str1,"失败窗口",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
}
}
else{
Object[] options={"是(y)","否(n)","取消",};
String str1=new String("警告!用户名或密码错误,请重新输入!");
JOptionPane.showOptionDialog(null,str1,"警告窗口",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
textField.setText(null);
passwordField.setText(null);
}
rs.close();
stmt.close();
con.close();
}
}
catch(java.lang.ClassNotFoundException e){
System.err.print(" ClassNotFoundException: ");
System.err.println(e.getMessage());
}
catch(SQLException ex){
System.err.println(" SQLException: "+ex.getMessage());
}
catch(Exception e) {
e.printStackTrace();
}
}});
button.setText("确定");
button.setBounds(150, 245, 75, 23);
frame.getContentPane().add(button);
final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
}
});
button_1.setText("取消");
button_1.setBounds(270, 245, 75, 23);
frame.getContentPane().add(button_1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -