2

我从这一行得到“不能从静态上下文引用非静态方法”错误:

createStudent("stu00001", new Date(631152000000)), "m", "WB", new Type_Name("Bob", "", "Smith"));

你如何正确形成“日期”?我查看了 API 并尝试了不同的方法,但我仍然收到日期错误。

package grade_db;

import bean.Student;
import bean.Type_Name;
import bean.University;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
 *
 * @author Sam
 */

public class Main {

EntityManager em;
EntityManagerFactory emf;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("db/grades.odb");

        EntityManager em;
        em = emf.createEntityManager();

        createStudent("stu00001", new Date(631152000000)), "m", "WB", new Type_Name("Bob", "", "Smith"));

        em.close();
        emf.close();
    }


    public Student createStudent(String student_id, Date dob, String gender, String nationality, Type_Name name){
    Student stu = new Student();
    stu.setDob(dob);
    stu.setGender(gender);
        stu.setName(name);
    stu.setNationality(nationality);

        stu.setCampus_id("cam00001");
        stu.setCourse_id(null);
        stu.setStudent_id(student_id);

    em.persist(stu);
    return stu;
}
}
4

3 回答 3

5

问题是您试图createStudent()main(). 如果您将createStudent()方法更改为静态的,那么您应该很高兴:

public static Student createStudent(String student_id, Date dob, String gender, String nationality, Type_Name name) {
    // ... And so on 
}

编辑: OP 指出,仅此更改在访问变量em和时给他另一个错误emf。要解决这个问题,您还需要制作这些 variables static

static EntityManager em;
static EntityManagerFactory emf;

那时,您班级中的所有内容都是静态的。假设这是一个简单的一次性或示例——我很乐意假设因为这个类被调用了Main——让一切都是静态的就可以了。总而言之,代码如下所示:

package grade_db;

import bean.Student;
import bean.Type_Name;
import bean.University;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
 *
 * @author Sam
 */

public class Main {

    static EntityManager em;
    static EntityManagerFactory emf;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("db/grades.odb");

        EntityManager em;
        em = emf.createEntityManager();

        createStudent("stu00001", new Date(631152000000)), "m", "WB", new Type_Name("Bob", "", "Smith"));

        em.close();
        emf.close();
    }

    public static Student createStudent(String student_id, Date dob, String gender, String nationality, Type_Name name){
        Student stu = new Student();
        stu.setDob(dob);
        stu.setGender(gender);
        stu.setName(name);
        stu.setNationality(nationality);

        stu.setCampus_id("cam00001");
        stu.setCourse_id(null);
        stu.setStudent_id(student_id);

        em.persist(stu);
        return stu;
    }
}
于 2013-04-12T19:22:22.493 回答
2

制作方法

       public static Student createStudent(String student_id, Date dob, String gender, String nationality, Type_Name name){
Student stu = new Student();

静止的

于 2013-04-12T19:23:52.973 回答
0

这是因为 CreateStudent 是对象的成员方法。Main 是静态的,不是对象的一部分。

您需要创建一个新的 Main 对象,或使 student 成为静态对象才能使其正常工作。

如果您将 createStudent 设为静态,它将起作用。

public static Student createStudent(String student_id, Date dob, String gender, String nationality, Type_Name name)
于 2013-04-12T19:23:09.467 回答