大家好,我使用JavaSE做了一个图形界面,内容包括:标签组件:姓名、密码、登陆系统,  文本框、密码框、注册按钮、登陆按钮;  当用户单击“注册”按钮时用户要在姓名的文本框中填入姓名,在密码框中输入密码,此时姓名和密码将被发送到mysql数据库中被保存起来;当用户再登陆时只要正确的输入和数据库中相同的姓名和密码就可以成功的登陆了。以下是我写的代码,可是注册的信息却不能发送到数据库,且不能成功登陆,不知是为什么,这里请大家给看看,指点迷津,希望能解决我的迷惑,谢谢了!
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JPasswordField;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
class  RegisterOrLogin{
JFrame frame = new JFrame("register");
JLabel lab1 = new JLabel("用户名:");
JLabel lab2 = new JLabel("密     码:");
static TextField name = new TextField();
static JPasswordField password = new JPasswordField();
    JButton login = new JButton("登   陆:");
    JButton register  = new JButton("注   册:");
    JLabel lab3 = new JLabel("用户登陆系统");
   
public RegisterOrLogin(){
frame.setLayout(new GridLayout(2,1));
 JPanel pan = new JPanel(new GridLayout(2,3));
pan.add(lab1);
pan.add(name);
pan.add(login);
pan.add(lab2);
pan.add(password);
pan.add(register);
frame.add(pan);
frame.add(lab3);
register.addActionListener(new ActionAdapter(){ @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==register){
    name.addTextListener(null);
     
}
}
 
});
login.addActionListener(new ActionListener(){ @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==login){
if(name.getText().equals("SELECT name FROM user")&& password.getPassword().equals("SELECT password FROM user")){
lab3.setText("登陆成功");
}else{
lab3.setText("登陆失败");
}
}
}

});
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1);
}
});
}
}class Base{

public Base() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mldn","root",""); 
PreparedStatement pst = con.prepareStatement("INSERT INTO user(name,password) VALUES (?,?)");
pst.setString(1,RegisterOrLogin.name.toString());
pst.setString(2,RegisterOrLogin.password.toString());
}

}
public class GraphAndDatabase { /**
 * @param args
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
            new RegisterOrLogin();
            new Base();
}}

解决方案 »

  1.   

    if(name.getText().equals("SELECT name FROM user")&& password.getPassword().equals("SELECT password FROM user"))这个是什么逻辑?怎么直接比较sql语句?
      

  2.   

    ActionAdapter改成ActionListener
    另外这个按钮事件处理代码中应该有new Base()
      

  3.   

    饿。好久没有接触swing了。忘却了。楼主我觉得你在比较SQL那里出问题了~
      

  4.   

    将getText()的值与 执行完sql语句返回的值比较,怎么直接与sql语句比较呢?
      

  5.   

    以下是经过修改的内容,但是却不能完成。package lib;import java.awt.event.WindowAdapter;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowEvent;
    import java.awt.event.ActionEvent;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.Font;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JPasswordField;
    import javax.swing.JPanel;class Base {
    static String name = null;
    static String password = null; public Base() throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/mldn", "root", "");
    PreparedStatement pst = con
    .prepareStatement("INSERT INTO user(name,password,age) VALUES (?,?,?)");
    pst.setString(1, RegisterOrLogin.name.getText());
    pst.setString(2, RegisterOrLogin.password.getText().toString());
    pst.setInt(3, 22);
    ResultSet rs = pst.executeQuery("SELECT name,password FROM user");
    while (rs.next()) {
    name = rs.getString("name");
    password = rs.getString("password");
    }
    pst.executeUpdate();
    }}class LoginCheck {
    Base b;
    ActionHandle a; public LoginCheck(Base b, ActionHandle a) {
    this.b = b;
    this.a = a;
    } public boolean validate() {
    if (a.nameText.equals(b.name) && a.passText.equals(b.password)) {
    return true;
    } else {
    return false;
    }
    }
    };class ActionHandle {
    private JFrame frame = new JFrame("Welcome To MLDN");
    private JButton submit = new JButton("登陆");
    private JButton reset = new JButton("重置");
    private JLabel nameLab = new JLabel("用户名:");
    private JLabel passLab = new JLabel("密   码:");
    private JLabel infoLab = new JLabel("用户登陆系统");
    static JTextField nameText = new JTextField(10);
    static  JPasswordField passText = new JPasswordField();
    private JPanel pan = new JPanel(); public ActionHandle() {
    Font fnt = new Font("Serief", Font.ITALIC + Font.BOLD, 12);
    infoLab.setFont(fnt); // 设置标签的显示文字
    submit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e){
    if (e.getSource() == submit) {
    String tname = nameText.getText();
    String tpass = new String(passText.getPassword());
    LoginCheck log = null;
    try {
    log = new LoginCheck(new Base(),new ActionHandle());
    } catch (Exception e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
    }
    try {
    if (log.validate()) {
    infoLab.setText("登陆成功,欢迎光临!");
    } else {
    infoLab.setText("登陆失败,错误的用户名或密码!");
    }
    } catch (Exception e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    }
    }
    });
    reset.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == reset) {
    nameText.setText("");
    passText.setText("");
    infoLab.setText("用户登陆系统");
    }
    }
    }); frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(1);
    }
    }); // 加入事件
    frame.setLayout(null);
    nameLab.setBounds(5, 5, 60, 20);
    passLab.setBounds(5, 30, 60, 20);
    infoLab.setBounds(5, 65, 220, 30);
    nameText.setBounds(65, 5, 100, 20);
    passText.setBounds(65, 30, 100, 20);
    submit.setBounds(165, 5, 60, 20);
    reset.setBounds(165, 30, 60, 20);
    frame.add(nameLab);
    frame.add(passLab);
    frame.add(infoLab);
    frame.add(nameText);
    frame.add(passText);
    frame.add(submit);
    frame.add(reset);
    frame.setSize(280, 130);
    frame.setBackground(Color.WHITE);
    frame.setLocation(300, 200);
    frame.setVisible(true); }
    };public class MyActionEventDemo03 {
    public static void main(String args[]) throws Exception {
    ActionHandle a = new ActionHandle();
            Base b = new Base();
    new LoginCheck(b,a); }
    };