The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Homework - Workers List
Discuss Workers List in the Java Help forum on Dev Shed. Workers List Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 28th, 2012, 01:57 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
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<Connection> con = 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<Connection> allConnections = 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<WorkersInfos> work = new LinkedList<WorkersInfos>();
private static WorkerLoad instance = new WorkerLoad();
public void loadFromDb(int workerid) throws SQLException {
WorkerLoad ret = new WorkerLoad();
Connection con = MYSQLconnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM workers WHERE id=?");
ps.setInt(1, workerid);
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[] args) throws 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<WorkersInfos> getWork() {
return work;
}
public static class WorkersInfos {
private String namez;
private int idz;
public WorkersInfos(int idz, String 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<WorkersInfos> work = 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)
|

November 28th, 2012, 02:44 PM
|
 |
Java Junkie
|
|
Join Date: Jan 2004
Location: Mobile, Alabama
|
|
Look at the type of the variable
Code:
for (WorkersInfos info : work) {
|

November 28th, 2012, 02:53 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
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;
}
|

November 28th, 2012, 03:26 PM
|
 |
Java Junkie
|
|
Join Date: Jan 2004
Location: Mobile, Alabama
|
|
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.
|

November 28th, 2012, 03:39 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
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
|

November 28th, 2012, 03:41 PM
|
 |
Java Junkie
|
|
Join Date: Jan 2004
Location: Mobile, Alabama
|
|
Quote: | Originally Posted by yoyo123123 it the same spelling as the class |
Code:
public class WorkersInfo
|

November 28th, 2012, 03:46 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
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)
|

November 29th, 2012, 06:22 AM
|
|
Contributing User
|
|
Join Date: Mar 2007
Location: Portugal
Posts: 38
  
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|