I am attempting a java and microsoft access project in which I take a survey and upload the results to a database. The GUI works, my problem is now with inserting data into the DB table.
My main problem is that I cannot use the StudentAdd function anywhere without getting a "Cannot make a static reference to the non-static method StudentAdd from the type UseDataBase."
Any help is appreciated. Classes are uploaded below (I am going to omit the imports, I have all of them, I promise).
Important part of Survey Class:
Button button_1 = new Button("Finish");
button_1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)//to send stuff to DB
{
int Button = JOptionPane.YES_NO_OPTION;
int option = JOptionPane.showConfirmDialog(frame, "Your data has been uploaded \n Yes to repeat the surrvey for a new class \n No to exit", "Repeat the surver?", Button);
if(option == JOptionPane.YES_OPTION)
{
frame.setVisible(true);
connecting("stable");
}
else
{
frame.setVisible(false);
}
//This line gives the error:
UseDataBase.StudentAdd (fname, lname, grade, subject, lvl, hw);
}
});
button_1.setBounds(263, 392, 116, 29);
frame.getContentPane().add(button_1);
DB Class:
public class DataBase
{
private Connection connect;
public DataBase(String name)
{
try// tries to connect to db
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connect = DriverManager.getConnection("jdbc:odbc:DRIVER ={Microsoft Access Driver (*.mdb)};DBQ ="+name+".mdb");
System.out.println("Connected");
}
catch(Exception e)// if cannot connect then
{
System.out.println("Unable to connect!");
}
}
ResultSet queryTbl(String sql) throws SQLException
{
Statement statement = connect.createStatement();
ResultSet rs = statement.executeQuery(sql);
return rs;
}
public void update(String sql) throws SQLException
{
Statement statement = connect.createStatement();
statement.executeUpdate(sql);
statement.close();
}
}
UseDB Class:
public class UseDataBase
{
DataBase surveyDB = new DataBase("Survey");
public void main (String [] args) throws SQLException
{
new UseDataBase ();
}
//Function I am calling
void StudentAdd (String fname, String lname, int grade, String subject, String lvl, String hw) throws SQLException
{
surveyDB.update("INSERT INTO stable (Name, Surname, Grade, Subject, Level, Homework Hours)VALUES('"+fname+"','"+ lname+"','"+ grade+"','" +subject+"','" +lvl+"','"+ hw+"')");
}