求教,java连接SQLserver,并用三种不同的身份的登录问题

qq_28638679 2015-05-31 11:52:59
请教:
我设置登录窗口有:教师,学生,管理员。怎么通过数据库验证帐号密码,并登录相应的窗口。我现在遇到的问题是,不知道怎么通过那三个窗口,通过验证密码登录。我只能直接点击身份并打开对应的界面
下面是我写的代码
package Login;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import Manger.Manger_data;
import Student_data.Student_data_2;
import Teacher_data.Teacher_data;

import java.util.*;

public class Login_2 extends JFrame implements ActionListener, ItemListener {
JLabel user = new JLabel("用 户 名:");
JLabel password = new JLabel("密 码:");
JLabel usertype = new JLabel("用户类型:");
JButton login = new JButton("登陆");
JButton cancel = new JButton("取消");

ButtonGroup buttongroup = new ButtonGroup();
JRadioButton mang = new JRadioButton("管理员");
JRadioButton stu = new JRadioButton("学生");
JRadioButton tea = new JRadioButton("老师");

JTextField text1 = new JTextField(18);
JPasswordField text2 = new JPasswordField(18);

public Login_2() {
super("登陆窗口");
setSize(300, 200);
setVisible(true);
setResizable(false);
setLayout(new FlowLayout(FlowLayout.CENTER, 15, 10));

add(user);
add(text1);
add(password);
add(text2);
add(usertype);
add(mang);
add(stu);
add(tea);

buttongroup.add(mang);
buttongroup.add(stu);
buttongroup.add(tea);
add(login);
add(cancel);

mang.addItemListener(this);//添加事件监听
stu.addItemListener(this);
tea.addItemListener(this);
login.addActionListener(this);
cancel.addActionListener(this);

Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width - 300) / 2, (screen.height - 220) / 2);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}

public void actionPerformed(ActionEvent e) {//登录按钮
if (e.getSource() == login) {
;
} else if (e.getSource() == cancel) {//退出按钮
dispose();
System.exit(0);
}
}

public void itemStateChanged(ItemEvent e) {//三种身份按钮
if (e.getSource() == mang) {
new Manger_data();//管理员窗口
setVisible(false);
} else if (e.getSource() == stu) {
new Student_data();//学生窗口
setVisible(false);
} else if
(e.getSource() == tea)
{
new Teacher_data();//教师窗口
setVisible(false);
}
}

public static void main(String args[]) {
new Login_2();
}
}

希望大家可以给点建议,谢谢大家
...全文
645 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Geilymm 2016-06-02
  • 打赏
  • 举报
回复
数据库每个角色都有对应的一张表啊,然后点击登录的时候验证的时候不应该只是验证account这张表而已 String sql = "select * from account where username='" + username + "'"; 一一对应嘛
女娲补bug 2016-06-02
  • 打赏
  • 举报
回复
引用 8 楼 qq_21747999 的回复:
[quote=引用 6 楼 finemi 的回复:] [quote=引用 4 楼 qq_21747999 的回复:] [quote=引用 1 楼 finemi 的回复:] 点登陆时查询数据库呀,根据用户选择的不同身份查询然后显示对应窗口
请问 怎么通过
引用 1 楼 finemi 的回复:
点登陆时查询数据库呀,根据用户选择的不同身份查询然后显示对应窗口
可以这样吗 设置一个变量flag ,然后判断选择了哪个按钮,分别赋值1,2,3 然后验证密码,登入 if(密码和数据库一致){ if(flag==1) 打开学生界面; if(flag==2) 打开老师界面; if(flag==3) 打开管理员界面; }[/quote] 可以啊[/quote] 我写了一个界面 ,已经成功验证SQL账号密码了,但是要怎么获取点击的“学生”“老师”“管理员”按钮,然后登入相应的界面呢
package Login;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class Login extends JFrame {

	private Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;
	private final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=dedo";
	private final String USER = "sa";
	private final String PASSWORD = "notshy123";

	public Login() {
		try {
			jbInit();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Login javalogin = new Login();
	}

	private void jbInit() throws Exception {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		this.getContentPane().setLayout(null);

		jButton1.setBounds(new Rectangle(42, 167, 128, 45));
		jButton1.setText("登陆");
		jButton1.addActionListener(new ha_jButton1_actionAdapter(this));
		//当jButton被按下的时候,所需要发生的事情定义在new ha_jButton1_actionAdapter(this)里。

		JUser.setBounds(new Rectangle(129, 36, 223, 25));
		jLabel2.setText("密码:");
		jLabel2.setBounds(new Rectangle(58, 78, 37, 51));
		jLabel1.setText("用户名:");
		jLabel1.setBounds(new Rectangle(48, 27, 46, 36));

		jLabel3.setFont(new java.awt.Font("黑体", Font.PLAIN, 12));
		jLabel3.setForeground(Color.red);
		//jLabel3.setText("数据库用户登陆初始状态:用户名:han密码:110 ");
		jLabel3.setBounds(new Rectangle(13, 229, 387, 40));
		JPass.setBounds(new Rectangle(128, 90, 223, 29));
		
		//当jButton被按下的时候,所需要发生的事情定义在new ha_jButton2_actionAdapter(this)里。
		jButton2.addActionListener(new ha_jButton2_actionAdapter(this));
		this.getContentPane().add(jLabel3);// 添加事件监听
		this.getContentPane().add(jButton1);
		this.getContentPane().add(jButton2);
		this.getContentPane().add(jLabel1);
		this.getContentPane().add(jLabel2);
		this.getContentPane().add(JUser);
		this.getContentPane().add(JPass);

		jButton2.setBounds(new Rectangle(231, 168, 133, 44));
		jButton2.setText("退出");

		this.setBounds(300, 200, 430, 330);
		this.setVisible(true);

	}

	JButton jButton1 = new JButton();
	JButton jButton2 = new JButton();
	JLabel jLabel1 = new JLabel();
	JLabel jLabel2 = new JLabel();
	JTextField JUser = new JTextField();
	JPasswordField JPass = new JPasswordField();
	JLabel jLabel3 = new JLabel();

	public void jButton1_actionPerformed(ActionEvent actionEvent) {
		String username = JUser.getText().trim();// 获取账号,trim()从当前 String
													// 对象移除所有前导空白字符和尾部空白字符。
		char[] s = JPass.getPassword();// 获取密码
		String password = new String(s);
		if (username.equals("") || password.equals(""))// 如果没输用户名或密码,则提示对不起,请输入用户名或密码
		{
			JOptionPane.showMessageDialog(this, "对不起,请输入用户名或密码.", "错误!",
					JOptionPane.ERROR_MESSAGE);// 如果未输入用户名密码,弹窗提示
		} else// 如果都有数据了就开始连接数据库验证
		{
			try {
				Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
				conn = DriverManager.getConnection(URL, USER, PASSWORD);// 这里就是连接数据库了
				String sql = "select * from account where username='" + username
						+ "'";

				// 执行的sql语句,在数据库里查找我们输的用户名
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				if (rs.next())// 如果存在,就验证密码
				{
					if (rs.getString("password").equals(password))// 如果密码正确就提示,反之
					{
						 //new ErsBlocksGame("aaa");
						//这里验证成功,登入不同的界面
					} else {
						JOptionPane.showMessageDialog(this, "对不起,密码错误,请重新输入",
								"登陆失败", JOptionPane.ERROR_MESSAGE);
					}
				} else {// 如果没有查找到用户名就提示
					JOptionPane.showMessageDialog(this, "用户名不存在,请重新输入", "错误!",
							JOptionPane.ERROR_MESSAGE);
				}

			} catch (ClassNotFoundException ex) {// 这后面是抛出异常
				ex.printStackTrace();

			}

			catch (SQLException ex) {
				ex.printStackTrace();
			}

			finally {// 关闭连接
				try {
					if (rs != null)
						rs.close();
					if (stmt != null)
						stmt.close();
					if (conn != null)
						conn.close();
				} catch (SQLException ex)// 当 SQL Server 返回警告或错误时引发的异常。
				{
					ex.printStackTrace();
				}
			}
		}
	}

	public void jButton2_actionPerformed(ActionEvent actionEvent) {
		System.exit(0);// 这是退出按纽
	}
}

class ha_jButton2_actionAdapter implements ActionListener {//定义的ha_jButton2_actionAdapter事件
	private Login adaptee;

	ha_jButton2_actionAdapter(Login adaptee) {
		this.adaptee = adaptee;
	}

	public void actionPerformed(ActionEvent actionEvent) {
		adaptee.jButton2_actionPerformed(actionEvent);
	}
}

class ha_jButton1_actionAdapter implements ActionListener {//定义的ha_jButton1_actionAdapter事件
	private Login adaptee;

	ha_jButton1_actionAdapter(Login adaptee) {
		this.adaptee = adaptee;
	}

	public void actionPerformed(ActionEvent actionEvent) {
		adaptee.jButton1_actionPerformed(actionEvent);
	}
}
[/quote]能让我看看数据库怎么建的吗?
finemi 2015-06-03
  • 打赏
  • 举报
回复
以你贴的第一个代码为例,你使用的JRadioButtion

//你的代码片段
    JRadioButton mang = new JRadioButton("管理员");
    JRadioButton stu = new JRadioButton("学生");
    JRadioButton tea = new JRadioButton("老师");

   public void jButton1_actionPerformed(ActionEvent actionEvent) {
     //....................
      if (rs.getString("password").equals(password))
     {
           if(mang.isSelected()){
             new 管理员窗口();
           }else if(stu.isSelected()){
              new 学生窗口();
           }else{
              new 老师窗口();
           }
       }    
   //......................
    }
hhhhhhh_____ 2015-06-03
  • 打赏
  • 举报
回复
引用 6 楼 finemi 的回复:
[quote=引用 4 楼 qq_21747999 的回复:] [quote=引用 1 楼 finemi 的回复:] 点登陆时查询数据库呀,根据用户选择的不同身份查询然后显示对应窗口
请问 怎么通过
引用 1 楼 finemi 的回复:
点登陆时查询数据库呀,根据用户选择的不同身份查询然后显示对应窗口
可以这样吗 设置一个变量flag ,然后判断选择了哪个按钮,分别赋值1,2,3 然后验证密码,登入 if(密码和数据库一致){ if(flag==1) 打开学生界面; if(flag==2) 打开老师界面; if(flag==3) 打开管理员界面; }[/quote] 可以啊[/quote] 你好 ,我已经实现了验证密码并登陆不同的界面,但是我这里还有一个问题,就是不同的账号可以登陆不属于它的界面,请问怎么才能让管理员只能登陆管理员界面,老师登陆老师界面,学生登陆学生界面,请指教一下思路 谢谢
/*
 * 学生班级管理系统登录界面
 */
package Login;

import javax.swing.*;

import Manger.Manger_data;
import Student_data.Student_data_2;
import Teacher_data.Teacher_data;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class Login extends JFrame {

//界面
	JLabel user = new JLabel("用 户 名:");
	JLabel password = new JLabel("密      码:");
	JLabel usertype = new JLabel("用户类型:");
	JButton login = new JButton("登陆");
	JButton cancel = new JButton("退出");
	
	ButtonGroup buttongroup = new ButtonGroup();
	JRadioButton mang = new JRadioButton("管理员");
	JRadioButton stu = new JRadioButton("学  生");
	JRadioButton tea = new JRadioButton("老  师");
	
	JTextField JUser = new JTextField(18);
	JPasswordField JPass = new JPasswordField(18);

//连接数据库属性
	private Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;
	private final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=dedo";
	private final String USER = "sa";
	private final String PASSWORD = "ddme123";

	public Login() {
		try {
			jbInit();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

//程序主入口
	public static void main(String[] args) {
		Login javalogin = new Login();
	}

//创建窗口
	private void jbInit() throws Exception {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);//使界面不能改变大小
		this.setSize(300, 200);
		this.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 15, 10));//布局管理器
		this.setTitle("登录界面");//界面标题

		add(user);
		add(JUser);
		add(password);
		add(JPass);
		add(usertype);
		add(mang);
		add(stu);
		add(tea);
		
		buttongroup.add(mang);
		buttongroup.add(stu);
		buttongroup.add(tea);
		add(login);
		add(cancel);
		
		//JPass.setBounds(new Rectangle(128, 90, 223, 29));
		login.addActionListener(new ha_jButton1_actionAdapter(this));
		//当jButton被按下的时候,所需要发生的事情定义在new ha_jButton1_actionAdapter(this)里。
		// 添加事件监听
		cancel.addActionListener(new ha_jButton2_actionAdapter(this));
		//当jButton被按下的时候,所需要发生的事情定义在new ha_jButton2_actionAdapter(this)里。
		// 添加事件监听
		
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((screen.width - 300) / 2, (screen.height - 220) / 2);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose();
				System.exit(0);
			}
		});

		this.setVisible(true);//显示界面

	}
	
	public void jButton1_actionPerformed(ActionEvent actionEvent) {
		String username = JUser.getText().trim();// 获取账号,trim()从当前 String
													// 对象移除所有前导空白字符和尾部空白字符。
		char[] s = JPass.getPassword();// 获取密码
		String password = new String(s);
		if (username.equals("") || password.equals(""))// 如果没输用户名或密码,则提示对不起,请输入用户名或密码
		{
			JOptionPane.showMessageDialog(this, "对不起,请输入用户名或密码.", "错误!",
					JOptionPane.ERROR_MESSAGE);// 如果未输入用户名密码,弹窗提示
		} else// 如果都有数据了就开始连接数据库验证
		{
			try {
				Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
				conn = DriverManager.getConnection(URL, USER, PASSWORD);// 这里就是连接数据库了
				String sql = "select * from account where username='" + username
						+ "'";

				// 执行的sql语句,在数据库里查找我们输的用户名
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				if (rs.next())// 如果存在,就验证密码
				{
					if (rs.getString("password").equals(password))// 如果密码正确就提示,反之
					{
						 //new ErsBlocksGame("aaa");
						if (mang.isSelected())//如果选择了管理员,则弹出管理员窗口
						{
							new Manger_data();
							setVisible(false);
						} else if (stu.isSelected())//如果选择了学生窗口。则弹出学生窗口
						{
							new Student_data_2();
							setVisible(false);
						} else if (tea.isSelected())//如果选择了老师窗口,则弹出教师窗口
						{
							new Teacher_data();
							setVisible(false);
						}
						
					} else {
						JOptionPane.showMessageDialog(this, "对不起,密码错误,请重新输入",
								"登陆失败", JOptionPane.ERROR_MESSAGE);//调出消息对话框
					}
				} else {// 如果没有查找到用户名就提示
					JOptionPane.showMessageDialog(this, "用户名不存在,请重新输入", "错误!",
							JOptionPane.ERROR_MESSAGE);
				}

			} catch (ClassNotFoundException ex) {// 这后面是抛出异常
				ex.printStackTrace();

			}

			catch (SQLException ex) {
				ex.printStackTrace();
			}

			finally {// 关闭连接
				try {
					if (rs != null)
						rs.close();
					if (stmt != null)
						stmt.close();
					if (conn != null)
						conn.close();
				} catch (SQLException ex)// 当 SQL Server 返回警告或错误时引发的异常。
				{
					ex.printStackTrace();
				}
			}
		}
	}

	public void jButton2_actionPerformed(ActionEvent actionEvent) {
		System.exit(0);// 这是退出按纽
	}
}

//定义的ha_jButton2_actionAdapter事件
class ha_jButton2_actionAdapter implements ActionListener {
	private Login adaptee;

	ha_jButton2_actionAdapter(Login adaptee) {
		this.adaptee = adaptee;
	}

	public void actionPerformed(ActionEvent actionEvent) {
		adaptee.jButton2_actionPerformed(actionEvent);
	}
}

//定义的ha_jButton1_actionAdapter事件
class ha_jButton1_actionAdapter implements ActionListener {
	private Login adaptee;

	ha_jButton1_actionAdapter(Login adaptee) {
		this.adaptee = adaptee;
	}

	public void actionPerformed(ActionEvent actionEvent) {
		adaptee.jButton1_actionPerformed(actionEvent);
	}
}
hhhhhhh_____ 2015-06-03
  • 打赏
  • 举报
回复
引用 6 楼 finemi 的回复:
[quote=引用 4 楼 qq_21747999 的回复:] [quote=引用 1 楼 finemi 的回复:] 点登陆时查询数据库呀,根据用户选择的不同身份查询然后显示对应窗口
请问 怎么通过
引用 1 楼 finemi 的回复:
点登陆时查询数据库呀,根据用户选择的不同身份查询然后显示对应窗口
可以这样吗 设置一个变量flag ,然后判断选择了哪个按钮,分别赋值1,2,3 然后验证密码,登入 if(密码和数据库一致){ if(flag==1) 打开学生界面; if(flag==2) 打开老师界面; if(flag==3) 打开管理员界面; }[/quote] 可以啊[/quote] 我写了一个界面 ,已经成功验证SQL账号密码了,但是要怎么获取点击的“学生”“老师”“管理员”按钮,然后登入相应的界面呢
package Login;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class Login extends JFrame {

	private Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;
	private final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=dedo";
	private final String USER = "sa";
	private final String PASSWORD = "notshy123";

	public Login() {
		try {
			jbInit();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Login javalogin = new Login();
	}

	private void jbInit() throws Exception {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		this.getContentPane().setLayout(null);

		jButton1.setBounds(new Rectangle(42, 167, 128, 45));
		jButton1.setText("登陆");
		jButton1.addActionListener(new ha_jButton1_actionAdapter(this));
		//当jButton被按下的时候,所需要发生的事情定义在new ha_jButton1_actionAdapter(this)里。

		JUser.setBounds(new Rectangle(129, 36, 223, 25));
		jLabel2.setText("密码:");
		jLabel2.setBounds(new Rectangle(58, 78, 37, 51));
		jLabel1.setText("用户名:");
		jLabel1.setBounds(new Rectangle(48, 27, 46, 36));

		jLabel3.setFont(new java.awt.Font("黑体", Font.PLAIN, 12));
		jLabel3.setForeground(Color.red);
		//jLabel3.setText("数据库用户登陆初始状态:用户名:han密码:110 ");
		jLabel3.setBounds(new Rectangle(13, 229, 387, 40));
		JPass.setBounds(new Rectangle(128, 90, 223, 29));
		
		//当jButton被按下的时候,所需要发生的事情定义在new ha_jButton2_actionAdapter(this)里。
		jButton2.addActionListener(new ha_jButton2_actionAdapter(this));
		this.getContentPane().add(jLabel3);// 添加事件监听
		this.getContentPane().add(jButton1);
		this.getContentPane().add(jButton2);
		this.getContentPane().add(jLabel1);
		this.getContentPane().add(jLabel2);
		this.getContentPane().add(JUser);
		this.getContentPane().add(JPass);

		jButton2.setBounds(new Rectangle(231, 168, 133, 44));
		jButton2.setText("退出");

		this.setBounds(300, 200, 430, 330);
		this.setVisible(true);

	}

	JButton jButton1 = new JButton();
	JButton jButton2 = new JButton();
	JLabel jLabel1 = new JLabel();
	JLabel jLabel2 = new JLabel();
	JTextField JUser = new JTextField();
	JPasswordField JPass = new JPasswordField();
	JLabel jLabel3 = new JLabel();

	public void jButton1_actionPerformed(ActionEvent actionEvent) {
		String username = JUser.getText().trim();// 获取账号,trim()从当前 String
													// 对象移除所有前导空白字符和尾部空白字符。
		char[] s = JPass.getPassword();// 获取密码
		String password = new String(s);
		if (username.equals("") || password.equals(""))// 如果没输用户名或密码,则提示对不起,请输入用户名或密码
		{
			JOptionPane.showMessageDialog(this, "对不起,请输入用户名或密码.", "错误!",
					JOptionPane.ERROR_MESSAGE);// 如果未输入用户名密码,弹窗提示
		} else// 如果都有数据了就开始连接数据库验证
		{
			try {
				Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
				conn = DriverManager.getConnection(URL, USER, PASSWORD);// 这里就是连接数据库了
				String sql = "select * from account where username='" + username
						+ "'";

				// 执行的sql语句,在数据库里查找我们输的用户名
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				if (rs.next())// 如果存在,就验证密码
				{
					if (rs.getString("password").equals(password))// 如果密码正确就提示,反之
					{
						 //new ErsBlocksGame("aaa");
						//这里验证成功,登入不同的界面
					} else {
						JOptionPane.showMessageDialog(this, "对不起,密码错误,请重新输入",
								"登陆失败", JOptionPane.ERROR_MESSAGE);
					}
				} else {// 如果没有查找到用户名就提示
					JOptionPane.showMessageDialog(this, "用户名不存在,请重新输入", "错误!",
							JOptionPane.ERROR_MESSAGE);
				}

			} catch (ClassNotFoundException ex) {// 这后面是抛出异常
				ex.printStackTrace();

			}

			catch (SQLException ex) {
				ex.printStackTrace();
			}

			finally {// 关闭连接
				try {
					if (rs != null)
						rs.close();
					if (stmt != null)
						stmt.close();
					if (conn != null)
						conn.close();
				} catch (SQLException ex)// 当 SQL Server 返回警告或错误时引发的异常。
				{
					ex.printStackTrace();
				}
			}
		}
	}

	public void jButton2_actionPerformed(ActionEvent actionEvent) {
		System.exit(0);// 这是退出按纽
	}
}

class ha_jButton2_actionAdapter implements ActionListener {//定义的ha_jButton2_actionAdapter事件
	private Login adaptee;

	ha_jButton2_actionAdapter(Login adaptee) {
		this.adaptee = adaptee;
	}

	public void actionPerformed(ActionEvent actionEvent) {
		adaptee.jButton2_actionPerformed(actionEvent);
	}
}

class ha_jButton1_actionAdapter implements ActionListener {//定义的ha_jButton1_actionAdapter事件
	private Login adaptee;

	ha_jButton1_actionAdapter(Login adaptee) {
		this.adaptee = adaptee;
	}

	public void actionPerformed(ActionEvent actionEvent) {
		adaptee.jButton1_actionPerformed(actionEvent);
	}
}
hhhhhhh_____ 2015-06-03
  • 打赏
  • 举报
回复
引用 5 楼 qq_21747999 的回复:
[quote=引用 2 楼 ylovep 的回复:] 搞这么多不是多次一举 搞个下拉框用户类型选择不就可以了
那怎么判断下拉框里面的身份来打开不同的界面呢[/quote] 我写了一个界面 ,已经成功验证SQL账号密码了,但是要怎么获取点击的“学生”“老师”“管理员”按钮,然后登入相应的界面呢
package Login;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class Login extends JFrame {

	private Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;
	private final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=dedo";
	private final String USER = "sa";
	private final String PASSWORD = "notshy123";

	public Login() {
		try {
			jbInit();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Login javalogin = new Login();
	}

	private void jbInit() throws Exception {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		this.getContentPane().setLayout(null);

		jButton1.setBounds(new Rectangle(42, 167, 128, 45));
		jButton1.setText("登陆");
		jButton1.addActionListener(new ha_jButton1_actionAdapter(this));
		//当jButton被按下的时候,所需要发生的事情定义在new ha_jButton1_actionAdapter(this)里。

		JUser.setBounds(new Rectangle(129, 36, 223, 25));
		jLabel2.setText("密码:");
		jLabel2.setBounds(new Rectangle(58, 78, 37, 51));
		jLabel1.setText("用户名:");
		jLabel1.setBounds(new Rectangle(48, 27, 46, 36));

		jLabel3.setFont(new java.awt.Font("黑体", Font.PLAIN, 12));
		jLabel3.setForeground(Color.red);
		//jLabel3.setText("数据库用户登陆初始状态:用户名:han密码:110 ");
		jLabel3.setBounds(new Rectangle(13, 229, 387, 40));
		JPass.setBounds(new Rectangle(128, 90, 223, 29));
		
		//当jButton被按下的时候,所需要发生的事情定义在new ha_jButton2_actionAdapter(this)里。
		jButton2.addActionListener(new ha_jButton2_actionAdapter(this));
		this.getContentPane().add(jLabel3);// 添加事件监听
		this.getContentPane().add(jButton1);
		this.getContentPane().add(jButton2);
		this.getContentPane().add(jLabel1);
		this.getContentPane().add(jLabel2);
		this.getContentPane().add(JUser);
		this.getContentPane().add(JPass);

		jButton2.setBounds(new Rectangle(231, 168, 133, 44));
		jButton2.setText("退出");

		this.setBounds(300, 200, 430, 330);
		this.setVisible(true);

	}

	JButton jButton1 = new JButton();
	JButton jButton2 = new JButton();
	JLabel jLabel1 = new JLabel();
	JLabel jLabel2 = new JLabel();
	JTextField JUser = new JTextField();
	JPasswordField JPass = new JPasswordField();
	JLabel jLabel3 = new JLabel();

	public void jButton1_actionPerformed(ActionEvent actionEvent) {
		String username = JUser.getText().trim();// 获取账号,trim()从当前 String
													// 对象移除所有前导空白字符和尾部空白字符。
		char[] s = JPass.getPassword();// 获取密码
		String password = new String(s);
		if (username.equals("") || password.equals(""))// 如果没输用户名或密码,则提示对不起,请输入用户名或密码
		{
			JOptionPane.showMessageDialog(this, "对不起,请输入用户名或密码.", "错误!",
					JOptionPane.ERROR_MESSAGE);// 如果未输入用户名密码,弹窗提示
		} else// 如果都有数据了就开始连接数据库验证
		{
			try {
				Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
				conn = DriverManager.getConnection(URL, USER, PASSWORD);// 这里就是连接数据库了
				String sql = "select * from account where username='" + username
						+ "'";

				// 执行的sql语句,在数据库里查找我们输的用户名
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				if (rs.next())// 如果存在,就验证密码
				{
					if (rs.getString("password").equals(password))// 如果密码正确就提示,反之
					{
						 //new ErsBlocksGame("aaa");
						//这里验证成功,登入不同的界面
					} else {
						JOptionPane.showMessageDialog(this, "对不起,密码错误,请重新输入",
								"登陆失败", JOptionPane.ERROR_MESSAGE);
					}
				} else {// 如果没有查找到用户名就提示
					JOptionPane.showMessageDialog(this, "用户名不存在,请重新输入", "错误!",
							JOptionPane.ERROR_MESSAGE);
				}

			} catch (ClassNotFoundException ex) {// 这后面是抛出异常
				ex.printStackTrace();

			}

			catch (SQLException ex) {
				ex.printStackTrace();
			}

			finally {// 关闭连接
				try {
					if (rs != null)
						rs.close();
					if (stmt != null)
						stmt.close();
					if (conn != null)
						conn.close();
				} catch (SQLException ex)// 当 SQL Server 返回警告或错误时引发的异常。
				{
					ex.printStackTrace();
				}
			}
		}
	}

	public void jButton2_actionPerformed(ActionEvent actionEvent) {
		System.exit(0);// 这是退出按纽
	}
}

class ha_jButton2_actionAdapter implements ActionListener {//定义的ha_jButton2_actionAdapter事件
	private Login adaptee;

	ha_jButton2_actionAdapter(Login adaptee) {
		this.adaptee = adaptee;
	}

	public void actionPerformed(ActionEvent actionEvent) {
		adaptee.jButton2_actionPerformed(actionEvent);
	}
}

class ha_jButton1_actionAdapter implements ActionListener {//定义的ha_jButton1_actionAdapter事件
	private Login adaptee;

	ha_jButton1_actionAdapter(Login adaptee) {
		this.adaptee = adaptee;
	}

	public void actionPerformed(ActionEvent actionEvent) {
		adaptee.jButton1_actionPerformed(actionEvent);
	}
}
hhhhhhh_____ 2015-06-02
  • 打赏
  • 举报
回复
引用 2 楼 ylovep 的回复:
搞这么多不是多次一举 搞个下拉框用户类型选择不就可以了
那怎么判断下拉框里面的身份来打开不同的界面呢
hhhhhhh_____ 2015-06-02
  • 打赏
  • 举报
回复
引用 1 楼 finemi 的回复:
点登陆时查询数据库呀,根据用户选择的不同身份查询然后显示对应窗口
请问 怎么通过
引用 1 楼 finemi 的回复:
点登陆时查询数据库呀,根据用户选择的不同身份查询然后显示对应窗口
可以这样吗 设置一个变量flag ,然后判断选择了哪个按钮,分别赋值1,2,3 然后验证密码,登入 if(密码和数据库一致){ if(flag==1) 打开学生界面; if(flag==2) 打开老师界面; if(flag==3) 打开管理员界面; }
finemi 2015-06-02
  • 打赏
  • 举报
回复
引用 4 楼 qq_21747999 的回复:
[quote=引用 1 楼 finemi 的回复:] 点登陆时查询数据库呀,根据用户选择的不同身份查询然后显示对应窗口
请问 怎么通过
引用 1 楼 finemi 的回复:
点登陆时查询数据库呀,根据用户选择的不同身份查询然后显示对应窗口
可以这样吗 设置一个变量flag ,然后判断选择了哪个按钮,分别赋值1,2,3 然后验证密码,登入 if(密码和数据库一致){ if(flag==1) 打开学生界面; if(flag==2) 打开老师界面; if(flag==3) 打开管理员界面; }[/quote] 可以啊
finemi 2015-06-01
  • 打赏
  • 举报
回复
点登陆时查询数据库呀,根据用户选择的不同身份查询然后显示对应窗口
javacankillyou 2015-06-01
  • 打赏
  • 举报
回复
楼上都可以解决楼主的问题哦。用个单选框也行。
ylovep 2015-06-01
  • 打赏
  • 举报
回复
搞这么多不是多次一举 搞个下拉框用户类型选择不就可以了

67,516

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧