简体   繁体   English

使用另一个类java.language.String中的方法

[英]Use method from another class, java.language.String

I've got two classes: 我有两节课:

MAIN and DBCONNECT MAINDBCONNECT

DBCONNECT has all the coding including the methods that will connect to, insert and update a database/table. DBCONNECT具有所有编码,包括将要连接,插入和更新数据库/表的方法。

MAIN is where my GUI is created and uses the DBCONNECT class. MAIN是创建GUI的地方,并使用DBCONNECT类。

In my DBCONNECT class I have created a method that writes the input info to a table. 在我的DBCONNECT类中,我创建了一个将输入信息写入表的方法。

String sqlInsert1 = "INSERT INTO Drivers (IDNumber, FirstName, LastName) VALUES " + Id +"," + FirstName+"," + Surname;
String sqlInsert2 = "INSERT INTO Offences(IDNumber, SpeedLimit, DriverSpeed, SeatBelt, DrunkenDriving, DriversLicense) VALUES" + Id + SpeedLimit + DriversSpeed + Seatbelt + DrunkenDriving + License;
String sqlInsert3 = "INSERT INTO DriverPoints(IDNumber, Points) VALUES" + Id + Points;

 public void writeToDB(String sqlInsert1, String sqlInsert2, String sqlInsert3)
    {
        try
        {
            Statement st = conn.createStatement();
            st.executeUpdate(sqlInsert1);
            st.executeUpdate(sqlInsert2);
            st.executeUpdate(sqlInsert3);
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Error: Could not read from database");
        }
    }

And In my MAIN class I want to use this method when the Save button is clicked 在我的MAIN类中,当我单击“ 保存”按钮时,我想使用此方法

public void actionPerformed(ActionEvent ae)
{
    if (ae.equals(save))
    {
        database.writeToDB();
    }
}

It keeps giving me an error 它一直给我一个错误

required:java.language.String,java.language.String,java.language.String

Please let me know what I am doing wrong here. 请让我知道我在做什么错。

you need to write like this: 您需要这样写:

public void actionPerformed(ActionEvent ae)
{
    if (ae.equals(save))
    {
        String sqlInsert1 = "INSERT INTO Drivers (IDNumber, FirstName, LastName) VALUES " + Id +"," + FirstName+"," + Surname;
        String sqlInsert2 = "INSERT INTO Offences(IDNumber, SpeedLimit, DriverSpeed, SeatBelt, DrunkenDriving, DriversLicense) VALUES" + Id + SpeedLimit + DriversSpeed + Seatbelt + DrunkenDriving + License;
        String sqlInsert3 = "INSERT INTO DriverPoints(IDNumber, Points) VALUES" + Id + Points;

        database.writeToDB(sqlInsert1,sqlInsert2,sqlInsert3);
    }

It is because your method writeToDB accepts three String parameters and you are giving no arguments when you are calling it in your code. 这是因为您的方法writeToDB接受三个String参数,并且在代码中调用它时没有提供任何参数。

And the string variables that you have created before your writeToDb method are not the same as the variables declared as parameters in the writeToDB method. 而且,你有你以前创建的字符串变量writeToDb方法是不一样的声明为参数变量writeToDB方法。

Look at this question to know the difference between arguments and parameters 查看此问题以了解参数和参数之间区别

From my understanding 从我的理解

There is no exact function call writeToDB(); 没有确切的函数调用writeToDB(); because writeToDB() is defined with paramaters but in function call empty parameter have been used . 因为writeToDB()是用参数定义的,但是在函数调用中使用了空参数。

Instead of this 代替这个

   database.writeToDB();

Should use something like this , use those variables and initialize here inside the actionPerformed() 应该使用这样的东西,使用这些变量并在actionPerformed()中初始化

   database..writeToDB(sqlInsert1,sqlInsert2,sqlInsert3);

Also you have missed the parenthesis in insert statement after values 您也错过了插入语句中的括号后的值

Example: 例:

   sqlINsert1="INSERT INTO Tablename(ID, StreetName, City)Values(,))";

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

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