package frame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class LoginFrame extends JFrame implements ActionListener {
JTextField userName,passWord;
JLabel name,psw,tip;
Box boxHOne,boxHTwo,boxVOne,boxVTwo;
JButton logIn,signUp;
public LoginFrame(){
setTitle("推箱子");
setLayout(new FlowLayout());
boxHOne=Box.createHorizontalBox();
boxVOne=Box.createVerticalBox();
boxVTwo=Box.createVerticalBox();
name=new JLabel("用户名:");
psw=new JLabel("密码");
boxVOne.add(name);
boxVOne.add(psw);
userName=new JTextField(15);
passWord=new JTextField(15);
boxVTwo.add(userName);
boxVTwo.add(passWord);
boxHOne.add(boxVOne);
boxHOne.add(Box.createHorizontalStrut(10));
boxHOne.add(boxVTwo);
logIn=new JButton("登录");
logIn.addActionListener(this);
logIn.setFocusable(true);
signUp=new JButton("注册");
signUp.addActionListener(this);
boxHTwo=Box.createHorizontalBox();
boxHTwo.add(logIn);
boxHTwo.add(signUp);
add(boxHOne);
add(boxHTwo);
setBounds(1100,500,320,200);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==logIn)
dataQuery();
else if(e.getSource()==signUp)
new SignUpFrame();
}
public void dataQuery(){
Connection con;
Statement sql;
ResultSet rs;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch (Exception e){
e.printStackTrace();
}
try{
String uri="jdbc:mysql://localhost:3306/pushbox?useUnicode=true&characterEncoding=utf8&&useSSL=false";
String user="root";
String password="123456";
con= DriverManager.getConnection(uri,user,password);
sql=con.createStatement();
String pasw1=null,pasw=null,uName=null;
do{
uName=userName.getText();
rs=sql.executeQuery("SELECT username,password FROM user WHERE username='"+uName+"'");
pasw=passWord.getText();
rs.last();
if(rs.getRow()==0){
tip=new JLabel("用户名不存在");
add(tip);
userName.setText("");
passWord.setText("");
}
else {
pasw1=rs.getString("password");
if(pasw.equals(pasw1)){
new MainFrame();
break;
}
else{
tip=new JLabel("密码错误");
add(tip);
}
}
}while(rs.getRow()==0||!pasw.equals(pasw1));
con.close();
}catch (SQLException e){
e.printStackTrace();
}
}
public static void main(String args[]){
new LoginFrame();
}
}
输入错误的用户名和密码没反应,而且窗口关也关不上,正确的用户名和密码可以成功运行,