0

I'm not doing very well with what I'm trying to do at the moment. The aim is to to get all information from a MySQL table and put it into an array. Once into an array I will use the priority queue in java and try to get it to work with that.... but that's not the issue I'm stuck on at the moment.

public static void array(String args[]) { // create priority queue

   try { 
        String url = "jdbc:mysql://localhost:3306/project"; 
        Connection conn = DriverManager.getConnection(url,"root","nbuser"); 

        PreparedStatement stmt = conn.prepareStatement("SELECT user_id,s_date,e_date,d_date,department,projectname,projectapplication FROM booking");
        ResultSet rs; 
        rs=stmt.executeQuery();

                  List<User> userList = new ArrayList<User>();
        while(rs.next()) {
        User user = new User(); //issue here with non-static variable
        user.userid(rs.getString("user_id"));
        user.s_date(rs.getObject("s_date"));
        user.e_date(rs.getObject("e_date"));
        user.d_date(rs.getObject("d_date"));
        user.department(rs.getObject("department"));
        user.projectname(rs.getObject("projectname"));
        user.projectapplication(rs.getObject("projectapplication"));
        user.priority(rs.getObject("priority"));

        userList.add(user);
     }

        conn.close(); 
    } catch (Exception e) { 
        System.err.println("Got an exception! "); 
        System.err.println(e.getMessage()); 
    } 
} 

The aim is to get it to run through all the different sections in the table and populate them into an array, but I'm stuck on populating the array.

Any help would be appreciated thanks.

EDIT:

public class User {
public String userid;
public String s_date;
public String e_date;
public String d_date;
public String department;
public String projectname;
public String projectapplication;
public int priority;

    private void userid(String string) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void s_date(Object object) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void e_date(Object object) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void d_date(Object object) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void department(Object object) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void projectname(Object object) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void projectapplication(Object object) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void priority(Object object) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

similar to this?

2
  • You're selecting a lot of fields. Which of them need to be added to array? Commented Feb 7, 2013 at 21:16
  • It would be far better and easier if you can use an ORM like JPA. Look for it's implementation that suits you, like, Hibernate, EclipseLink, whatever. They will be easier to work with Commented Feb 7, 2013 at 21:16

1 Answer 1

1

Create a User class with the appropriate fields in the SQL table and keep each user in a list.

  List<User> userList = new ArrayList<User>();
  while(rs.next()) {
     User user = new User();
     user.setId(rs.getString("user_id"));
     user.setStartDate(rs.getTimestamp("s_date");
     ....
     userList.add(user);
  }
  return userList;

EDIT:

  class User {
   private String id;
   private Date startDate;
   //other fields need to be declared as well as their setter methods

   public void setStartDate(Date startDate) {
      this.startDate = startDate;
   }

   public void setId(String id) {
      this.id = id;
   }
  }

I would also recommend to take a look at JPA and Hibernate.

Sign up to request clarification or add additional context in comments.

10 Comments

User class like added above in my edit? so from there on would it be a case of user.(stringname)rs.getObject("....") ? bit unsure in regards to this
It depends on the fields, for example if you keep date fields as timestamp in db, I would go with Date class not String
you can use rs.getString(fieldname) or you can use rs.getObject(fieldname) and cast it afterward
I've editted my class in the edit section then implemented your function in the original question but commented in where I'm having an error, also is the class right or am I completely off track (most likely case)
It is better practice to name setter methods with set prefix with field name. For example; setId, setDepartmant ..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.