📄 a07899b77f50001d1fb9d6a5e675a3e6
字号:
import javax.swing.*;
import java.sql.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.sql.DriverManager;
class LoginWindow extends JFrame implements ItemListener,ActionListener
{ Box baseBox,boxV1,boxV2;
Choice choice;
TextField noText,passwordText;
Label userLabel,noLabel,passwordLabel,label,label1;
Button loginButton,resetButton;
JPanel panel=new JPanel();
LoginWindow(String s)
{ super(s);
userLabel=new Label("身份",Label.LEFT);
choice=new Choice();
choice.add("学 生");
choice.add("管理人员");
choice.addItemListener(this);
noLabel=new Label("账号",Label.LEFT);
noText=new TextField(10);
//noText.addActionListener(this);
passwordText=new TextField(10);
passwordLabel=new Label("密码",Label.LEFT);
loginButton=new Button("登陆");
resetButton=new Button("重置");
label=new Label();
label1=new Label();
boxV1=Box.createVerticalBox();
boxV1.add(userLabel);
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(noLabel);
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(passwordLabel);
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(loginButton);
loginButton.addActionListener(this);
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(label1);
boxV2=Box.createVerticalBox();
boxV2.add(choice);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(noText);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(passwordText);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(resetButton);
resetButton.addActionListener(this);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(label);
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV2);
setLayout(new FlowLayout());
add(baseBox);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setBounds(120,120,250,250);
setVisible(true);
validate();
}
public void itemStateChanged(ItemEvent e)
{
}
public void actionPerformed(ActionEvent e)
{
String name=choice.getName();
//int index=choice.getSelectedIndex();
if(e.getSource()==loginButton)
{
String s1=noText.getText();
String s2=passwordText.getText();
if(name=="学生")
{
Connection con=null;
Statement sql=null;
ResultSet rs=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException k)
{
System.out.println(""+k);
}
try
{
con=DriverManager.getConnection("jdbc:odbc:sun","","");
sql=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("SELECT * FROM 学生密码表 where no='"+s1+"'and psw='"+s2+"' ");
rs.last();
if(rs.getRow()>0)
{
dispose();
new studentButton("学生信息界面");
}
else
{
label.setText("信息错误!");
}
con.close();
}
catch(SQLException ee)
{
}
}
else
{
Connection con=null;
Statement sql=null;
ResultSet rs=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException k)
{
System.out.println(""+k);
}
try
{
con=DriverManager.getConnection("jdbc:odbc:sun","","");
sql=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("SELECT * FROM login where name='"+s1+"'and password='"+s2+"' ");
rs.last();
if(rs.getRow()>0)
{
dispose();
new Win3("管理员信息界面");
}
else
{
label.setText("信息错误!");
}
con.close();
}
catch(SQLException ee)
{
}
}
}
if(e.getSource()==resetButton)
{ noText.setText(null);
passwordText.setText(null);
label.setText(null);
}
}
}
public class Login
{ public static void main(String args[])
{ LoginWindow Win=new LoginWindow("学生信息管理系统") ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -