Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question Error accessing a static method (JSP, Java)

    JSP file
    ......................................
    Code:
    //reading params from the form
    String name = request.getParameter("name");
    String year_prime = request.getParameter("year");
    int year = Integer.parseInt(year_prime);
    
    //inserting the params into table Students
    StudentsManager.getInstance().
    fillStudents(name, year);
    // here I have the "fillStudents underlined"
    ......................................
    _____________________________________

    Java file
    ________

    Code:
    public class StudentsManager {
    
    private static StudentsManager instance;
    
    private StudentsManager(){
    
    }
    
    public static StudentsManager getInstance(){
    if (instance == null){
    instance = new StudentsManager();
    }
    return instance;
    }
    
    public static void fillStudents(String name, int year){
    Connection con = ConnectionsManager.getInstance()
    .getConnection(); 
    Statement stmt = ConnectionsManager.getInstance()
    .getStatement(con);
    
    try {
    stmt.executeUpdate("insert into students(name,year) values('"+name+"', year)");
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    ......................................

    The error says:

    - The method fillStudents(String, int) is undefined the type StudentsManager.

    The warning says:

    - The static method fillStudents(String, int) from the type StudentsManager should be accessed in a static way.


  2. Users who have thanked brood_snow for this post:

    anne12 (07-02-2012)

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Its not being accessed statically. getInstance returns an instance of StudentsManager. Either call fillStudents directly off of the StudentsManager, or change fillStudents to a non static method.

  • Users who have thanked Fou-Lu for this post:

    anne12 (07-02-2012)

  • #3
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Smile Yup, it worked

    It worked!

    thanks a lot


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •