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.
you need to write like this:
It is because your method
writeToDBaccepts threeStringparameters and you are giving no arguments when you are calling it in your code.And the string variables that you have created before your
writeToDbmethod are not the same as the variables declared as parameters in thewriteToDBmethod.Look at this question to know the difference between arguments and parameters