当用户在swing中成功登录时打开新窗口 [英] Open new window when user login successfully in swing

查看:104
本文介绍了当用户在swing中成功登录时打开新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事一个基于 Swing 的小项目.在这个项目中,我想在用户登录成功时打开另一个窗口,但是当我单击 log-in 按钮时发生异常,例如 非法尝试提前退出",虽然我的数据库连接很好,因为当我显示消息对话框时,它显示成功登录.这是我的代码.

I am working on a Swing based small project. In this project I want to open another window when user log-in successfully but when I click log-in button an exception occurred, like "Illegal attempt to exit early", while my database connection is well because when I show message dialog then it shows successful log-in. Here is my code.

package org.Core.Art;

import java.awt.Component;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import java.sql.*;
import javax.swing.*;
import org.openide.util.Exceptions;

public final class CoreTopComponent extends TopComponent { 

    private String password;
    private String pwd;
    private Component jPasswordField1;
    private Component jButton1;
    //private Object text_password;

    public CoreTopComponent() {
        initComponents();
    } 

    private void ClickActionPerformed(java.awt.event.ActionEvent evt) {                
        // TODO add your handling code here:
        Object  password = text_password.getText();       

        String query = "Select * from assistant where pass = '"+password+"'";

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/auto_lube","root", "mehar");    
            Statement stmt = conn.createStatement();
            stmt.execute(query);    
            ResultSet rs = stmt.getResultSet();
            boolean recordfound = rs.next();

            if (recordfound) {
                Login regFace = new Login();
                regFace.setVisible(true);
                dispose(); 
                //JOptionPane.showMessageDialog(null,"You are login successfully");
            } else {
                JOptionPane.showMessageDialog(null,"Try again");
                // new Login_fram().setVisible(true);
            }

        conn.close();

        } catch(java.lang.ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null,"Exception Occured"+ex.getMessage());
        } catch(Exception ex) {
            JOptionPane.showMessageDialog(null,"Exception Occured"+ex.getMessage());    
        }
    }

    private void text_passwordActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        Object  password = text_password;
    }

    public static void main(String args[]) {
        CoreTopComponent core = new CoreTopComponent();
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CoreTopComponent().setVisible(true);
            }
        });
     }
}

我的另一个类是 Login.java 并且我希望当用户单击 log_in 按钮时,如果找到记录然后打开一个新窗口或 Login.java.

And my other class is Login.java and I want when user click log_in button if record found then open a new window or Login.java.

推荐答案

在这些行:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/auto_lube","root", "mehar")

请注意,您正在尝试使用 JdbcOdbcDriver 连接到 MySQL 数据库,这不正确,但可能是异常的原因.您应该改用 MySQL 连接器.此外,自 JDK8 起不推荐使用 JDBC-ODBC 桥接器.请参阅在 Java 8 中删除 JDBC ODBC 桥

Note you're trying to connect to a MySQL database using JdbcOdbcDriver which is not correct but probably the exception's cause. You should use MysQL Connector instead. Besides JDBC-ODBC bridge is deprecated since JDK8. See Removal of JDBC ODBC bridge in java 8

您可以在 这个答案.简而言之:

There are some other JDBC tips you can follow in this answer. In a nutshell:

  • Use PreparedStatement properly to avoid SQL Injection attaks.
  • The use of DriverManager.getConnection() is discouraged and should be replaced by DataSource.getConnection(). How to do this? See linked answer.
  • Since Java 7 you can use try-with-resources which is a more elegant way to deal with try-catch blocks and closeable resources.

请注意,您的代码中有几个概念性错误.例如这个 SQL 查询(注意使用准备好的语句的正确方法):

Please note there are several conceptual errors in your code. For example this SQL query (note the proper way of using prepared statements):

String query = "SELECT * FROM assistant WHERE pass = ?";

此查询将返回密码等于某个参数的所有记录,因此如果两个或更多人具有相同的密码,它将返回多个记录.不太可能,是的,但可能.登录系统是基于用户名和密码的,所以你应该查询用户名,然后检查密码是否有效.更不用说密码不应以纯文本形式存储,而应至少加密(如果散列更好).

This query will return all records where password is equal to some parameter, so if two or more people have the same password it will return more than a single record. Unlikely, yes, but possible. Log-in systems are based on user names and passwords, so you should query for user name and then check if password is valid or not. Not to mention that password should not be stored as plain text but at least encrypted (better if hashed).

另一方面,根据您的代码,很难判断您使用的是 Swing 标准组件还是其他一些基于 Swing 的 API.我的意思是,什么是 org.openide.*?Swing 组件的导入在哪里?

On the other hand based on your code it is really hard to tell if you are using Swing standard components or some other API based on Swing. I mean, what is org.openide.*? Where are the imports for Swing components?

最后,如果您仍然遇到问题,请发布异常的堆栈跟踪.

Finally, if you still having problems, pelase post the exception's stack trace.

这篇关于当用户在swing中成功登录时打开新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆