0

I've got two classes:

MAIN and DBCONNECT

DBCONNECT has all the coding including the methods that will connect to, insert and update a database/table.

MAIN is where my GUI is created and uses the DBCONNECT class.

In my DBCONNECT class I have created a method that writes the input info to a table.

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

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.

Stijn Geukens
  • 15,454
  • 8
  • 66
  • 101
Jaun Lloyd
  • 199
  • 2
  • 4
  • 13
  • It is `java.lang.String` If you have the Strings as fields, you don't need to pass them as parameters and you won't have to pass anything. – Peter Lawrey Nov 01 '12 at 10:24
  • @Thanga It means you are trying to call a method which takes one argument, but you haven't given it one. You need to work out what value you should be passing. – Peter Lawrey Nov 01 '12 at 11:04
  • @PeterLawrey yes agreed.. the same scenario was above Jaun's query too, database.writetoDB() invokes the function with empty params and the definitions has 3 args then how it is possible with out args please ? i could be wrong :) – thar45 Nov 01 '12 at 11:08
  • @Thanga You can do it without args if the method has no parameters. If the values are always the same you don't need to pass them as arguments. – Peter Lawrey Nov 01 '12 at 11:23

2 Answers2

3

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.

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.

Look at this question to know the difference between arguments and parameters

Community
  • 1
  • 1
Abubakkar
  • 15,488
  • 8
  • 55
  • 83
0

From my understanding

There is no exact function call writeToDB(); because writeToDB() is defined with paramaters but in function call empty parameter have been used .

Instead of this

   database.writeToDB();

Should use something like this , use those variables and initialize here inside the 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(,))";
thar45
  • 3,518
  • 4
  • 31
  • 48