Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 28th, 2012, 01:57 PM
yoyo123123 yoyo123123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 yoyo123123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 38 m 2 sec
Reputation Power: 0
Homework - 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)

Reply With Quote
  #2  
Old November 28th, 2012, 02:44 PM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,826 bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 9 h 9 m 26 sec
Reputation Power: 1248
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to bullet
Look at the type of the variable

Code:
for (WorkersInfos info : work) {

Reply With Quote
  #3  
Old November 28th, 2012, 02:53 PM
yoyo123123 yoyo123123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 yoyo123123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 38 m 2 sec
Reputation Power: 0
Quote:
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;
    }

Reply With Quote
  #4  
Old November 28th, 2012, 03:26 PM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,826 bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 9 h 9 m 26 sec
Reputation Power: 1248
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to bullet
Quote:
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.

Reply With Quote
  #5  
Old November 28th, 2012, 03:39 PM
yoyo123123 yoyo123123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 yoyo123123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 38 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by bullet
In both cases look at the spelling of the type.

it the same spelling as the class

Reply With Quote
  #6  
Old November 28th, 2012, 03:41 PM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,826 bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 9 h 9 m 26 sec
Reputation Power: 1248
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to bullet
Quote:
Originally Posted by yoyo123123
it the same spelling as the class

Code:
public class WorkersInfo

Reply With Quote
  #7  
Old November 28th, 2012, 03:46 PM
yoyo123123 yoyo123123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 yoyo123123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 38 m 2 sec
Reputation Power: 0
Quote:
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)

Reply With Quote
  #8  
Old November 29th, 2012, 06:22 AM
Revoh Revoh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Location: Portugal
Posts: 38 Revoh User rank is Corporal (100 - 500 Reputation Level)Revoh User rank is Corporal (100 - 500 Reputation Level)Revoh User rank is Corporal (100 - 500 Reputation Level)Revoh User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 6 h 34 m 46 sec
Reputation Power: 8
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Workers List

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap