Thread: Workers List

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Workers List


    Hello, I got homework to make a worker list, I have to make it with 3 files:
    1-MYSQL connection
    2-WorkerLoad - loads from the database
    3-WorkersInfo - prints the database info
    I succeded to the first 2 but having troubles with the third, help please??

    the first file is:
    PHP Code:
    package connection;

    import java.sql.*;
    import java.util.Collection;
    import java.util.LinkedList;

    public class 
    MYSQLconnection{
        private static 
    ThreadLocal<Connectioncon = new LocalConnection();
        
        public static 
    Connection getConnection() {
            return 
    con.get();
        }
        
        public static 
    void closeAll() throws SQLException {
            for (
    Connection con LocalConnection.allConnections) {
                
    con.close();
            }
        }
        public static class 
    LocalConnection extends ThreadLocal<Connection> {
            public static 
    Collection<ConnectionallConnections = new LinkedList<Connection>();

            @
    Override
            
    protected Connection initialValue() {
                
    String driver "com.mysql.jdbc.Driver";
                
    String url "jdbc:mysql://localhost:3306/";
                
    String userName "root";
                
    String password "ROOT";
                
    String dbName "schoolproject";
                try {
                    Class.
    forName(driver).newInstance();
                    
    Connection conn DriverManager.getConnection(url+dbName,userName,password);
                    
    System.out.println("Connected to the database");
                    
    allConnections.add(conn);
                    return 
    conn;
                } catch (
    Exception e) {
                    
    e.printStackTrace();
                    return 
    null;
                }
            }
        }

    the second:
    PHP Code:
    package Loads;

    import connection.MYSQLconnection;
    import Infos.WorkersInfo;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.LinkedList;
    import java.util.List;

    //id name

    public class WorkerLoad {
        private 
    int id;
        private 
    String name;
        private List<
    WorkersInfoswork = new LinkedList<WorkersInfos>();
        private static 
    WorkerLoad instance = new WorkerLoad();
        
        public 
    void loadFromDb(int workeridthrows SQLException {
            
    WorkerLoad ret = new WorkerLoad();
            
    Connection con MYSQLconnection.getConnection();
            
    PreparedStatement ps con.prepareStatement("SELECT * FROM workers WHERE id=?");
            
    ps.setInt(1workerid);
            
    ResultSet rs ps.executeQuery();
            
    /*if (!rs.next()) {
                rs.close();
                ps.close();
                System.out.println("something failed"); 
            }*/
            
    while(rs.next()){
                
    System.out.println("dfsgdsfgdfsgdfs");
                
    ret.id rs.getInt("id");
                
    ret.name rs.getString("name");
                
    System.out.println("worker number "+ret.id);
                final 
    WorkersInfos rank = new WorkersInfos(rs.getInt("id"),rs.getString("name"));
                
    work.add(rank);

                
            }
            
    rs.close();
            
    ps.close();
        }
      
        
        public static 
    void main(String[] argsthrows SQLException {
            
    WorkerLoad abc = new WorkerLoad();
            
    abc.loadFromDb(1);
            
    abc.loadFromDb(2);
            
    WorkersInfo vvv = new WorkersInfo();
            
    vvv.check();
            

        }
      
        public static 
    WorkerLoad getInstance() {
            return 
    instance;
        }
            public List<
    WorkersInfosgetWork() {
            return 
    work;
        }
            public static class 
    WorkersInfos {

            private 
    String namez;
            private 
    int idz;

            public 
    WorkersInfos(int idzString namez) {
                
    this.idz idz;
                
    this.namez namez;
            }

            public 
    String getName() {
                return 
    namez;
            }

            public 
    int getId() {
                return 
    idz;
            }
        }
            
            
            

    the third:
    PHP Code:
    package Infos;



    import Loads.WorkerLoad;
    import Loads.WorkerLoad.WorkersInfos;
    import java.util.Arrays;
    import java.util.List;

    public class 
    WorkersInfo {
        public 
    void check(){
            
    StringBuilder ret = new StringBuilder();
            
    String s null;
            final List<
    WorkersInfoswork WorkerLoad.getInstance().getWork();
            for (
    WorkersInfos info work) {
                
    ret.append("name = ");
                
    ret.append(info.getName());
                
    ret.append("id = ");
                
    ret.append(info.getId());
            }
                
    s=ret.toString();
                
    System.out.println(s);         

        }

    I don't know why, but in the third, the FOR don't work


    (sorry for the bad english)
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Look at the type of the variable

    Code:
    for (WorkersInfos info : work) {
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by bullet
    Look at the type of the variable

    Code:
    for (WorkersInfos info : work) {
    no thats fine, I think the error is :
    Code:
        public List<WorkersInfos> getWork() {
            return work;
        }
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by yoyo123123
    no thats fine, I think the error is :
    Code:
        public List<WorkersInfos> getWork() {
            return work;
        }
    In both cases look at the spelling of the type.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by bullet
    In both cases look at the spelling of the type.
    it the same spelling as the class
  10. #6
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by yoyo123123
    it the same spelling as the class
    Code:
    public class WorkersInfo
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by bullet
    Code:
    public class WorkersInfo
    there is WorkersInfo and WorkersInfos with "s" in the end.
    WorkersInfos with "s" is where "work" list *sits* but when I do System.out.Println(getInstance().getWork().size);
    it gives me 0 when it needs to give me 2
    (while the LoadFromDB , it does give me 2 elements, but when I call to getWork() it gives me 0... I don't know why but it doesn't want to save the list)
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    38
    Rep Power
    9
    First of all why did you make such a huge amount of high coupling?

    Because your classes don't make much sense it would be correct to have WorkersInfo as an object Worker with id and name.

    But anyway.

    Could it be since you have made WorkersInfos a static class?

    And why do you use this. for set methods and get methos you don't?

    P.S. why did you place WorkersInfos inside WorkerLoad?
    Last edited by Revoh; November 29th, 2012 at 06:26 AM.

IMN logo majestic logo threadwatch logo seochat tools logo