📄 xuanke.java
字号:
package xuankexitong;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.lang.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;
import java.net.*;
import javax.swing.table.*;
public class Xuanke extends JFrame implements ActionListener{
String name="",password="",right="";
JTextField t1;
JPasswordField t2;
JRadioButton rb1, rb2, rb3;
JButton button;
ButtonGroup bg=new ButtonGroup();
Connection con;
Statement stmt;
ResultSet res=null;
int count=0;
public Xuanke(){
addConnection();
setTitle("选课查询系统");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Container content=getContentPane();
content.setLayout(new BorderLayout());
JPanel p1=new JPanel(),p2=new JPanel(),p3=new JPanel(),p4=new JPanel(),p5=new JPanel();
p5.setLayout(new GridLayout(4,1));
JLabel l1=new JLabel("用户名"),l2=new JLabel("口 令");
t1=new JTextField(20);
t2=new JPasswordField(20);
button=new JButton("确定");
button.addActionListener(this);
p4.add(button);
rb1=new JRadioButton("管理员");
rb1.addActionListener(this);
bg.add(rb1);
rb2=new JRadioButton("教师");
rb2.addActionListener(this);
bg.add(rb2);
rb3=new JRadioButton("学生");
rb3.addActionListener(this);
bg.add(rb3);
p1.add(l1);
p1.add(t1);
p2.add(l2);
p2.add(t2);
p3.add(rb1);
p3.add(rb2);
p3.add(rb3);
p5.add(p3);
p5.add(p1);
p5.add(p2);
p5.add(p4);
content.add(p5,BorderLayout.CENTER);
Toolkit tk=getToolkit();
Dimension screenSize=tk.getScreenSize();
int frameWidth=400;
int frameHeight=250;
setBounds((screenSize.width-frameWidth)/2,(screenSize.height-frameHeight)/2,frameWidth,frameHeight);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String c=e.getActionCommand();
if(c.equals("管理员")){
right=c;
}else if(c.equals("教师")){
right=c;
}else if(c.equals("学生")){
right=c;
}else if(c.equals("确定")){
if(right.equals(""))
JOptionPane.showMessageDialog(null,"请选择身份");
else{
if(count==3&&right.equals("管理员")){
JOptionPane.showMessageDialog(null,"错误次数超过三次,不能取得管理员权限");
}
else{
String string=null;
String s1=t1.getText(),s2=t2.getText();
String sql="SELECT password FROM user WHERE username='"+s1+"' AND right='"+right+"'";
try{
res=stmt.executeQuery(sql);
while(res.next()){
string=res.getString("password");
}
if(string==null||!string.equals(s2)){
JOptionPane.showMessageDialog(null,"用户名或密码错误,请重新输入");
if(right.equals("管理员"))
count++;
}
else if(string.equals(s2))
xinjian(right,s1);
}
catch(SQLException se){
se.printStackTrace(System.out);
}
}
}
}
}
public void xinjian(String right,String username){
if(right.equals("教师"))
new Teacher(username);
if(right.equals("学生"))
new Student(username);
if(right.equals("管理员"))
new Administrator(username);
}
public void addConnection(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException g){
JOptionPane.showMessageDialog(null,"驱动器加载失败,无法建立数据库连接");
return;
}
try{
con=DriverManager.getConnection("jdbc:odbc:stu","","");
stmt=con.createStatement();
}
catch(SQLException se){
se.printStackTrace(System.out);
}
}
public static void main(String[] args){
Xuanke xuanke=new Xuanke();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -