import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.sql.*;
public class Login extends Frame implements ActionListener
{
JLabel Labelusernumber=new JLabel("用户名:");
JLabel Labelpassword=new JLabel("密码:");
JLabel Labelshenfen=new JLabel("身份:");
JTextField TextFieldusernumber=new JTextField();
JPasswordField FieldPassword=new JPasswordField();
JButton button=new JButton("登录");
JComboBox JC=new JComboBox();
public Login()
{
setTitle("超市销售信息管理系统");
setLayout(null);
Labelusernumber.setBounds(50,40,60,20);
add(Labelusernumber);
TextFieldusernumber.setBounds(100,40,80,20);
add(TextFieldusernumber);
Labelpassword.setBounds(50,80,60,20);
add(Labelpassword);
FieldPassword.setBounds(100,80,80,20);
add(FieldPassword);
Labelshenfen.setBounds(50,120,60,20);
add(Labelshenfen);
JC.setBounds(100,120,80,20);
add(JC);
JC.addItem(new String("老板"));
JC.addItem(new String("雇员"));
button.setBounds(50,160,60,20);
add(button);
button.addActionListener(this);
setVisible(true);
setBounds(0,0,230,200);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String args[])
{
new Login();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{
String unum=TextFieldusernumber.getText();
String upassword=FieldPassword.getText();
String box=(String)JC.getSelectedItem();
String sql="select * from employee where Emnum='"+unum+"' and Emkey='"+upassword+"'";
if(box.equals("老板"))
{
if((unum!=null&&(unum.equals("sada")))&&(upassword!=null&&(upassword.equals("0231"))))
{
new ShowButton();
//new bossmanage();
}
}
else if(box.equals("雇员"))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:DBInfo","user","123456");
Statement stm=con.createStatement();
ResultSet rs=stm.executeQuery(sql);
if(rs.next())
{
new ShowButton();
//new employeemanage();
}
}
catch(Exception ee)
{
}
}
}
}
Emkey是不是字符串还是数字,如果是数字,不要引号。
我的建议是
String sql="select * from employee where Emnum='"+unum+"' and Emkey='"+upassword+"'";
在这个语句下面打印下sql,然后再在客户端中执行打印出的字符串,这样就可以知道组合之后的字符串sql是否正确
数据库语句应该没错,你的actionformed要写在addAtionListener中,你单独写actionformed,ActionEvent e无值,怎么处罚执行actionformed方法?
在构造函数里加上一句button.addActionListener(this);来给button添加一个监听事件,点击按钮就会触发这个事件,执行actionPerformed方法
一看就知道是获取连接的url不对,jdbc:odbc:DBInfo改成具体的url就OK了。
另外,你不会是用的access数据库吧???
access的url是:String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=PageData.mdb;pwd=123456";
你的actionformed要写在addAtionListener中,你单独写actionformed,ActionEvent e无值,怎么处理执行actionformed方法?