另一个类中的access变量,用于sql语句java

8yparm6h  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(307)

这对你们来说可能是一个简单的问题,但我正在尝试访问一个sql语句设置的变量,然后将其传递到另一个类中的另一个sql语句中。
这个想法是当用户登录时,我得到他们的staffid,然后我想在另一个select语句中使用这个数字。
我试图使变量公开并从另一个类访问它,但它返回空的。
登录代码:

public static String StaffID;

// SignIn ActionListener 
            signIn.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {

                    try{
                    Class.forName(DRIVER);
                    // Connection to the Database
                    con = DriverManager.getConnection(DATABASE_URL,"root","");

                    // Gets text from textfields and assigns them to variables
                    s1 = tfUsername.getText();
                    s2 = tfPassword.getText();

                    Statement st = con.createStatement();   

                    // SQL Statements 
                    st.executeQuery("SELECT * FROM login WHERE UName= '"+s1+"' and PWord = '"+s2+"'");

                    // Extracts data from statement to a result set
                    ResultSet rs = st.getResultSet();

                    if (rs.next())
                    {
                    // Gets text from Textfields and assigns them to variables
                    StaffID = rs.getString("StaffID");

                    }

其他类别:

try{
                    Class.forName(DRIVER);
                    // Connection to the Database
                    con = DriverManager.getConnection(DATABASE_URL,"root","");

                    signIn sign = new signIn();

                    //Creates Statement Connection
                    Statement st = con.createStatement();   

                    // SQL Statements 
                    st.executeQuery("SELECT * FROM rota WHERE StaffID = '"+sign.StaffID+"'");

                    // Extracts data from statement to a result set
                    ResultSet rs = st.getResultSet();

                    if (rs.next()) {

                        tfRotaTask1.setText(rs.getString("Task1"));
                        tfRotaTask2.setText(rs.getString("Task2"));
                        tfRotaTask3.setText(rs.getString("Task3"));
                        tfRotaTask4.setText(rs.getString("Task4"));
                        tfRotaTask5.setText(rs.getString("Task5"));
                        tfRotaTask6.setText(rs.getString("Task6"));
                        tfRotaTask7.setText(rs.getString("Task7"));
                        tfRotaTask8.setText(rs.getString("Task8"));

                        }
                    else{
                        //JOptionPane.showMessageDialog(null, "Staff Member Not Found");
                    }

把这个放在上下文中,我做了一个员工管理轮值制。
抱歉,如果我的java看起来很差:(
任何帮助都可以。

f2uvfpb9

f2uvfpb91#

首先,将您的查询封装在一个类中,以便于使用。

public class QueryUtils
    {
     public static String staffId(...yourArgs)
    {
    String staffId = "";
     // Make the logic for getting the StringId
    return staffId;
    }
}

好的,您可以从应用程序中的任何地方调用此查询。即使是其他查询。使用基本的面向对象和模式设计,你会解决这个问题。

wbrvyc0a

wbrvyc0a2#

在rs.next()的条件为真之前,您不会初始化staffid的值,请尝试像这样生成else语句

if (rs.next())
                {
                // Gets text from Textfields and assigns them to variables
                StaffID = rs.getString("StaffID");

                }

然后在另一个类中测试staffid的值,如果它为null,那么在executequery中有错误,因为我在代码中没有看到任何错误

相关问题