November 28th, 2012, 01:57 PM
-
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
-
Look at the type of the variable
Code:
for (WorkersInfos info : work) {
November 28th, 2012, 02:53 PM
-
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
-
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
-
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
-
Originally Posted by yoyo123123
it the same spelling as the class
Code:
public class WorkersInfo
November 28th, 2012, 03:46 PM
-
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
-
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.