这里有几段代码写一个登陆注册模块,里面有的地方的注释已经写好了,但还是有的地方不太懂,求各位讲解一下
这段里面开头的EventQueue.invokeLater这一部分我不是很明白为什么要这样写
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginJFrame frame = new LoginJFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace(); //标出错误
}
}
});
}
public LoginJFrame() {
setTitle("欢迎登录");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();//获得屏幕尺寸
int width = 380;//软件的宽高
int height = 300;
setBounds((d.width-width)/2, (d.height-height)/2, width, height);//窗口的坐标和尺寸,这种方式居中
this.setResizable(false);//使窗口无法放大
CardLayout cardLayout=new CardLayout(); //卡片布局,可以叠加组件
contentPane = new JPanel(); //创建面板
contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));//设置边界,让下面的粉色边框露出来
setContentPane(contentPane);
contentPane.setLayout(cardLayout);
this.getContentPane().setBackground(Color.pink);//美化,加粉色边框
JPanel userPanel = new JPanel();
contentPane.add(userPanel); //添加用户面板,登陆注册
userPanel.setLayout(null); //很重要,缺少了他就变成流式布局了
userName = new JTextField();
userName.setBounds(148, 55, 122, 21); //账号输入框的位置大小
userPanel.add(userName);
userPassword = new JPasswordField();
userPassword.setBounds(148, 96, 122, 21); //密码输入框的位置大小
userPanel.add(userPassword);
JButton userButton1 = new JButton("登陆"); //新建登录按钮
userButton1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
userLoginActionPerformed(event);
}
});
userButton1.setBounds(72, 159, 93, 23);
userPanel.add(userButton1);
JButton userButton2 = new JButton("注册");
userButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
userRegisterActionPerformed(event);
}
});
userButton2.setBounds(220, 159, 93, 23);
userPanel.add(userButton2);
JLabel lbll = new JLabel("账号:");
lbll.setBounds(72, 58, 54, 15);
userPanel.add(lbll);
JLabel label = new JLabel("密码:");
label.setBounds(72, 99, 54, 15);
userPanel.add(label);
}
private void userLoginActionPerformed(ActionEvent event) {
String uname=userName.getText();
String upassword=userPassword.getText(); //这两句是截取下来账号和密码
UserDaoImpl userDaoImpl=new UserDaoImpl(); //创建userdao,调用userdao的方法验证用户名和密码。
if(userDaoImpl.certifyUser(uname, upassword)) //如果验证通过
{
JOptionPane.showMessageDialog(this, "登录成功"); //提示登陆成功
StudentJFrame studentJFrame=new StudentJFrame();
studentJFrame.setBounds(600, 300, 800, 600); //进入后页面的大小
studentJFrame.setVisible(true); //显示界面
}
else
{
JOptionPane.showMessageDialog(this, "登录失败,账号或密码错误!","登陆学生管理系统",JOptionPane.ERROR_MESSAGE);
}
}
private void userRegisterActionPerformed(ActionEvent event) {
String uname=userName.getText();
String upassword=userPassword.getText();
User user=new User(uname,upassword);
UserDaoImpl userDaoImpl=new UserDaoImpl();
if(userDaoImpl.addUser(user)) {
JOptionPane.showMessageDialog(this, "注册成功");
}
else {
JOptionPane.showMessageDialog(this, "注册失败!","注册学生管理系统",JOptionPane.ERROR_MESSAGE);
}
}
}
public class UserDaoImpl implements UserDao
{
@Override
public boolean addUser(User user) {
String insert="insert into user(uname,upassword) values('"+user.getUname()+"','"+user.getUpassword()+"')";
try {
DBUtil.runUpdate(insert);
return true;
} catch (SQLException ex) {
Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}
@Override
public boolean update(User user) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public User getUserbyID(int id) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean certifyUser(String uname, String upassword) { //用户认证
String select="select * from user where uname='"+uname+"' and upassword='"+upassword+"'"; //查询数据库有没有账号密码
boolean isCertifyUser=false;
try {
ResultSet rs=DBUtil.runQuery(select); // 调用runQuery方法,查询select语句,返回结果为ResultSet
if(rs!=null) // 判断返回结果是否为空
{
isCertifyUser=rs.next(); // 获取结果集中是否存在下一条记录,next()用于判断是否存在下一条记录,存在则是true,不存在则是false
DBUtil.realeaseAll(); // 调用realeaseAll()释放资源
}
} catch (SQLException ex) { // 这里如果捕获到SQLException异常
Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex); //打印
}
return isCertifyUser;
}
}
这里的UserDao有什么用阿
public interface UserDao
{
public boolean addUser(User user);
public boolean update(User user);
public User getUserbyID(int id);
public boolean certifyUser(String uname,String upassword);
}
public class DBUtil {
private static String driver = ("com.mysql.cj.jdbc.Driver");
private static String URL = "jdbc:mysql://localhost:3306/caipiao?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT";
private static Connection con = null;
private static Statement smt = null;
private static ResultSet rs = null;
private static Connection createConnection() {
try {
Class.forName(driver);
return DriverManager.getConnection(URL, "root", "root");
} catch (SQLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (java.lang.ClassNotFoundException e) {
System.out.println("Can't load Driver");
}
return null;
}
public static int runUpdate(String sql) throws SQLException {
int count = 0;
if (con == null) {
//如果con没有初始化,那么调用函数初始化一下
//如果con已经初始化完毕,那么直接使用已经建立的连接,不重新初始化
con = createConnection();
}
if (smt == null) {
//同上,如果smt没有初始化,从con中获取对象
smt = con.createStatement();
}
count = smt.executeUpdate(sql);//执行sql语句
//count返回的是受影响的行数
if (smt != null) {//关闭连接
smt.close();
smt = null;
}
if (con != null) {//关闭连接
con.close();
con = null;
}
return count;
}
public static ResultSet runQuery(String sql) throws SQLException {
if (con == null) {
con = createConnection();
}
if (smt == null) {
smt = con.createStatement();
}
return smt.executeQuery(sql);
}
public static void realeaseAll() {
if (rs != null) {
try {
rs.close();
rs = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if (smt != null) {
try {
smt.close();
smt = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
con = null;
} catch (SQLException ex) {
Logger.getLogger(DBUtil.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void closeConnection(Connection conn) {
System.out.println("...");
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public class User
{
private int uid;
private String uname;
private String upassword;
public User() {
}
public User(String uname, String upassword) {
this.uname = uname;
this.upassword = upassword;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpassword() {
return upassword;
}
public void setUpassword(String upassword) {
this.upassword = upassword;
}
}
麻烦各位了,看不懂这几段代码怎么联系到一起的