📄 学生成绩查询.java
字号:
package classfile;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class Login extends JFrame implements ActionListener
{
/**登陆程序
*
*/
private static final long serialVersionUID = 1L;
JLabel lab1=new JLabel("欢迎您到网上成绩查询系统!");
JLabel lab2=new JLabel("用户名:");
JLabel lab3=new JLabel("密码:");
JTextField txt1=new JTextField(20);
JPasswordField txt2=new JPasswordField(20);
JButton btn1=new JButton("学生入口");
JButton btn2=new JButton("教师入口");
JButton btn3=new JButton("管理员入口");
JPanel p=new JPanel();
JPanel p1=new JPanel();
JPanel p2=new JPanel();
static String name;
static String name1;
public Login()
{
txt1.setBackground(Color.cyan);
txt2.setBackground(Color.cyan);
p.setLayout(new GridLayout(2,2));
p.add(lab2);
p.add(txt1);
p.add(lab3);
p.add(txt2);
p.setBackground(Color.pink);
p2.setLayout(new GridLayout(1,3));
p2.add(btn1);
p2.add(btn2);
p2.add(btn3);
p1.setLayout(new BorderLayout());
p1.add(lab1,BorderLayout.NORTH);
p1.add(p,BorderLayout.CENTER);
p1.add(p2,BorderLayout.SOUTH);
this.getContentPane().add(p1,BorderLayout.CENTER);
this.setBounds(100,100,400,200);
this.setVisible(true);
btn1.addActionListener(new Listener1());
btn2.addActionListener(this);
btn3.addActionListener(new Listener2());
}
class Listener1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if((txt1.getText()==" ")||(txt2.getText()==" "))
{
JOptionPane.showMessageDialog(null,"用户名或密码不能为空!");
}
else
{
name=txt1.getText().trim();
String pw=txt2.getText().trim();
String sql="select count(*) from Login_stu where user_name='"+name+"'and upassword='"+pw+"'";
try
{
dataBase db=new dataBase();
ResultSet rs=db.getResult(sql);
while(rs.next())
{
int count=rs.getInt(1);
if(count==1)
{
Student_user st=new Student_user();
sql="select name from student where sno='"+name+"'";
rs=db.getResult(sql);
rs.next();
name1=rs.getString(1);
}
else
{
JOptionPane.showMessageDialog(null,"该用户不存在或密码错误!");
}
}
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}
}
class Listener2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if((txt1.getText()==" ")||(txt2.getText()==" "))
{
JOptionPane.showMessageDialog(null,"用户名或密码不能为空!");
}
else
{
name=txt1.getText();
String pw=txt2.getText();
String sql="select count(*) from Login_man where user_name='"+name+"'and upassword='"+pw+"'";
try
{
dataBase db=new dataBase();
ResultSet rs=db.getResult(sql);
while(rs.next())
{
int count=rs.getInt(1);
if(count==1)
{
Manager_user m=new Manager_user();
}
else
{
JOptionPane.showMessageDialog(null,"该用户不存在或密码错误!");
}
}
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}
}
public void actionPerformed(ActionEvent e)
{
if((txt1.getText()==" ")||(txt2.getText()==" "))
{
JOptionPane.showMessageDialog(null,"用户名或密码不能为空!");
}
else
{
name=txt1.getText();
String pw=txt2.getText();
String sql="select count(*) from Login_tea where user_name='"+name+"'and upassword='"+pw+"'";
dataBase db=new dataBase();
ResultSet rs=db.getResult(sql);
try
{
while(rs.next())
{
int count=rs.getInt(1);
if(count==1)
{
Teacher_user t=new Teacher_user();
t.setVisible(true);
}
else
{
JOptionPane.showMessageDialog(null,"该用户不存在或密码错误!");
}
}
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}
public static void main(String args[])
{
new Login();
}
}
class dataBase
{
Connection con;
public dataBase()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection( "jdbc:odbc:stu", "system", "haohao");
}
catch(Exception ex)
{
System.err.println("SQLException: " + ex.getMessage());
ex.printStackTrace();
}
}
public ResultSet getResult(String sql)
{
try
{
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
return rs;
}
catch(Exception ex)
{
System.err.println("SQLException: " + ex.getMessage());
ex.printStackTrace();
return null;
}
}
public boolean executeSql(String sql)
{
try
{
Statement stmt = con.createStatement();
stmt.executeUpdate(sql);
return true;
}
catch(Exception ex)
{
System.err.println("SQLException: " + ex.getMessage());
ex.printStackTrace();
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -