0

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+"')");
    }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
LydsE
  • 21
  • 1
  • 4
  • The code shown cannot cause the error you describe, as the code shown doesn't contain a method `InsertIntoDB` nor a class `UseDB`. Please make sure you post the right code, but more importantly, first search for the error message and try to understand its causes. Right now you are trying to use us as your debuggers. – Mark Rotteveel Dec 06 '17 at 17:03
  • Thank you for your comment. I always look at other options before adding something here, but this time I really couldn't find the answer. – LydsE Dec 06 '17 at 17:13
  • 1
    Seriously: the difference between static and non-static methods is a **super basic** thing. If you really don't understand the differences between these two things, and the error that comes with that, then honestly: you are absolutely overburdening yourself. Then you shouldnt do DB related code. And you shouldnt be doing UI code. You should step back and learn such basics. – GhostCat Dec 06 '17 at 17:13

0 Answers0