我正在尝试一个java和microsoft访问项目,我在其中进行调查并将结果上传到数据库。 GUI工作,我现在的问题是将数据插入数据库表。
我的主要问题是我无法在任何地方使用StudentAdd函数而无法获得"无法从UseDataBase类型对非静态方法StudentAdd进行静态引用。"
感谢任何帮助。下面上传了类(我将省略导入,我保留所有这些,我保证)。
调查班的重要部分:
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);
数据库类:
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+"')");
}