简体   繁体   English

Mysql java 不能使用来自另一个类的连接方法

[英]Mysql java cannot use connection method from another class

I am currently a fist year student doing software engineering and have currently ran into some problem while developing a project for university.我目前是一名从事软件工程的一年级学生,目前在为大学开发项目时遇到了一些问题。 I am currently working on a netbeans project which allows users to login and retrieve information from an mysql database.我目前正在开发一个 netbeans 项目,该项目允许用户从 mysql 数据库登录和检索信息。 I currently have a separate class for datahandling where I have written the connection method see below.我目前有一个单独的数据处理类,我在其中编写了连接方法,请参见下文。 However when I create the a new object of the datahandler in my Jframe class and try to call the connection method it does not allow me to use insert statements(see below) which I have programmed behind buttons(in order to retrieve information from txtfields).但是,当我在 Jframe 类中创建数据处理程序的新对象并尝试调用连接方法时,它不允许我使用我在按钮后面编程的插入语句(见下文)(以便从 txtfields 中检索信息) . However when I move the connection method to the Jframe class it works perfectly fine.但是,当我将连接方法移动到 Jframe 类时,它工作得很好。 I was wonder if I am missing something really obvious here, or if it is just horrible design.我想知道我是否在这里遗漏了一些非常明显的东西,或者它是否只是可怕的设计。

Thanks for your support!谢谢您的支持!

    private void connection(){
    //from data handler class

    try{

    String mysqlUrl = "jdbc:mysql://localhost/test";
    String username = "root";
    String password = "";
    con = DriverManager.getConnection(mysqlUrl, username, password);
    }
    catch(Exception e){
        System.err.println("Got an exception");
        System.err.println(e.getMessage());

    }
  }
 }

    private void btnRegisterActionPerformed(java.awt.event.ActionEvent evt) {                                            

    try{
        Datahandler test = new Datahandler();
        test.connection();

        //create my mysql database connection


        String query = "INSERT INTO User(customerStatus ,title, firstname, lastname, contactNo, email, username, password)" 
                + "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        pst = con.prepareStatement(query);

        pst.setString(1,"Beginner");
        pst.setString(2,cmdGender.getSelectedItem().toString());
        pst.setString(3,txtFirstname.getText());
        pst.setString(4,txtLastname.getText());
        pst.setString(5,txtPhone.getText());
        pst.setString(6,txtEmail.getText());
        pst.setString(7,txtUsername.getText());
        pst.setString(8,txtPassword.getText());
        pst.execute();

        /* inserts the values username and password into a separate table in the database and
        makes use of the to ensure that the login is sercure(I will possibly make a change to
            how the structure of the databases work since there is an security issue here
            */
            String query2 = "INSERT INTO Login(username, password)" 
                    + "VALUES (?, ?)";
            pst = con.prepareStatement(query2);
            pst.setString(1,txtUsername.getText());
            pst.setString(2,txtPassword.getText());

            pst.execute();


            JOptionPane.showMessageDialog(null, "Registration completed!");

            //reset registration boxes into empty text
            txtFirstname.setText("");
            txtLastname.setText("");
            txtPhone.setText("");
            txtEmail.setText("");
            txtUsername.setText("");
            txtPassword.setText("");

        }
        catch (Exception e){

            System.err.println("Got an exception");
            System.err.println(e.getMessage());
            JOptionPane.showMessageDialog(null, "Something went wrong try to register again");

        }
}                                           

your getConnection() method is not returning anything.您的getConnection()方法没有返回任何内容。 instead it is setting connection object to obejct con in that specific class.相反,它将连接对象设置为该特定类中的 obejct con。

con = DriverManager.getConnection(mysqlUrl, username, password);

This is why you are able to use it in your Jframe class, because it contains con property which gets set in getConnection() method.这就是您可以在Jframe类中使用它的原因,因为它包含在getConnection()方法中设置的con属性。 but in your new class there is no filed con that is having proper connection object.但是在您的新课程中,没有具有正确连接对象的已归档骗局

Modify method getConnection() so that it return connection object, and use that connection object returned from this method to further processing.修改方法getConnection()使其返回连接对象,并使用从该方法返回的连接对象进行进一步处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM