Java 检查JTextField的内容,然后以字符串形式存储以从另一个类访问

Java 检查JTextField的内容,然后以字符串形式存储以从另一个类访问,java,swing,jdbc,Java,Swing,Jdbc,我正在创建一个Java应用程序,允许用户通过GUI界面将数据插入MySQL数据库。它的结构是为所有GUI使用一个主类,然后为每个函数在外部使用单独的类(创建表、插入数据等) 我在GUI部分创建了一个函数,该函数通过使用actionListener检查用户何时按下JButton,然后打开一个JOptionPane,其中有一个面板,其中包含一些JTextfields,这将进入一个if/else语句,以检查第一个JTextField中是否包含文本(其余数据可以为null)。一旦这个过程完成,它在JTe

我正在创建一个Java应用程序,允许用户通过GUI界面将数据插入MySQL数据库。它的结构是为所有GUI使用一个主类,然后为每个函数在外部使用单独的类(创建表、插入数据等)

我在GUI部分创建了一个函数,该函数通过使用actionListener检查用户何时按下JButton,然后打开一个JOptionPane,其中有一个面板,其中包含一些JTextfields,这将进入一个if/else语句,以检查第一个JTextField中是否包含文本(其余数据可以为null)。一旦这个过程完成,它在JTextField上运行一个getText并将其存储在一个字符串中

主要类别:

package catalogue;

    addSupplier.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae) {
            //Call a JOptionPane and give it an int value to be able to check status of button presses without another ActionListner
            int result = JOptionPane.showConfirmDialog(null, addSupplierPanel, "Please enter supplier name", JOptionPane.OK_CANCEL_OPTION);
            //Checks if OK button is pressed
            if (result == JOptionPane.OK_OPTION){

                //Check if user entered any text into suppNameIn (Cannot be null)
                if(suppNameIn.getText().equals("")){

                    //Dispense error as suppNameIn is empty
                    System.out.println("Pressed Okay, nothing entered");

                } else {

                    //Grab the text from the JTextField and convert it into a usable string
                    String suppNameInsert = suppNameIn.getText();
                    String suppCollectionInsert = suppCollectionIn.getText();

                    //Confirmation that data has been read in from JTextField correctly
                    System.out.println(suppNameInsert + " Successfully entered");
                    System.out.println(suppCollectionInsert + " Successfully entered");

                    try{
                        //Connect to DB
                        Connection conn = CatalogueDB.getConnection();
                        //Prepare statement to run the insert script using the strings as input variables
                        PreparedStatement posted = conn.prepareStatement("INSERT INTO supplier (suppName, suppCollection) VALUES ('"+suppNameInsert+"', '"+suppCollectionInsert+"')");
                        //Push the PreparedStatement 'posted'
                        posted.executeUpdate();
                    //Catch if anything goes wrong while connecting to the database
                    } catch(Exception e){System.out.println("Error adding supplier");}
                    //Finish by printing a message to say the insert has worked.
                    finally{
                        System.out.println("Insert Completed.");
                    }
                }
            }
        }
    });
}
单独的insertData类:

package catalogue;

import java.sql.Connection;
 import java.sql.PreparedStatement;

public class insertData {
    CatalogueUI.suppNameInsert;

    public void insertSupplier() throws Exception{
        try{
            //Connect to DB
            Connection conn = CatalogueDB.getConnection();
            PreparedStatement posted = conn.prepareStatement("INSERT INTO supplier (suppName, suppCollection) VALUES ('"+suppNameInsert+"', '"+suppCollectionInsert+"')");
            posted.executeUpdate();
        } catch(Exception e){System.out.println("Error adding supplier");}
        finally{
            System.out.println("Insert Completed.");
        }
    }

}
我遇到的问题是,我无法访问主类之外的字符串。我是否以不正确的方式来实现我想要的目标,或者我是否有一个独立的问题,而不是我认为我是什么

我无法访问主类之外的字符串

当然。班级独立于其他班级工作。一个类不知道在任何其他类中使用了哪些变量

如果要创建两个类,则需要将参数从一个类传递到另一个类。这与调用方法并将参数传递给方法时没有什么不同

我想说没有理由单独上课。SQL访问将只是主类中的一个方法

PreparedStatement posted = conn.prepareStatement("INSERT INTO supplier (suppName, suppCollection) VALUES ('"+suppNameInsert+"', '"+suppCollectionInsert+"')");
此外,您正在错误地使用
PreparedStatement
。使用
PreparedStatement
的目的是将参数传递给
PreparedStatement
。正确使用
PreparedStatement
将使上述代码更易于阅读和维护


请参阅:了解更多信息。

另外,最好转义这些变量,以防止SQL注入和其他可能的攻击。谢谢您的建议!我不特别希望这个insert存储在这个类中的原因是,我将创建一个更大的insert,它有五个输入,我将以与上面发布的类似的方式配置这个insert,并执行if-else语句,以确保正确的JTextFields被占用,我看到这个很快就变大了,为了便于阅读,我想要一种单独做的方法。虽然我现在可能只是决定将其保留在主类中。@NomadMaker这是一个很好的观点,我将记住这一点,对于我将来制作的任何应用程序,我目前正在进行的这个项目不会上线,我只是尝试为将来的项目学习一些MySQL/JDBC实现!