user6353178 user6353178 - 24 days ago 19
Java Question

Java - Implementing DAO

I'm struggling to implement this DOA class. I've added the getConnection() and closeConnection() methods, however, when adding the ArrayList I always get an error. I've added my code below. I've been given a UML diagram to use.

enter image description here

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class EmployeeDAO {


public static void main(String[] args) throws ClassNotFoundException {

Connection con = null;
Statement st = null;
ResultSet rs = null;

Class.forName("org.sqlite.JDBC");

try{

String dbURL = "jdbc:sqlite:employeeDatabase.sqlite";
con = DriverManager.getConnection(dbURL);
st = con.createStatement();
rs = st.executeQuery("SELECT * FROM employees;" );

while(rs.next()){
System.out.println(rs.getString("ID"));
System.out.println(rs.getString("Name"));
System.out.println(rs.getString("Gender"));
System.out.println(rs.getString("DOB"));
System.out.println(rs.getString("Address"));
System.out.println(rs.getString("Postcode"));
System.out.println(rs.getString("NIN"));
System.out.println(rs.getString("JobTitle"));
System.out.println(rs.getString("StartDate"));
System.out.println(rs.getString("Salary"));
System.out.println(rs.getString("Email"));
}

} catch (SQLException ex){
ex.printStackTrace();

} finally{
try{
if (rs != null){
rs.close();
}
if (st !=null){
st.close();
}
if (con !=null){
con.close();
}


} catch (SQLException ex) {
ex.printStackTrace();
}
}


}
}



Exception in thread "main" java.lang.Error: Unresolved compilation
problems:

Employees cannot be resolved to a type

Illegal modifier for parameter selectallEmployees; only final is permitted

Employees cannot be resolved to a type at EmployeeDAO.main(EmployeeDAO.java:61)


What am I doing wrong? Could someone possibly point me in the right direction? Thank you.

Answer

I think the triangles are protected or public fields. The red boxes are private fields. Green circles are all public methods. The C on the top circle is a constructor.

Your question's code follows none of that...

According to your diagram, your class needs to be written starting like so.

public class EmployeeDAO {

    private Connection con;
    private Statement st;
    private ResultSet rs;

    public EmployeeDAO() { 
        // TODO: Should initialize 'con', probably
    }

    public Statement getConnection() { 
        return st; // confused... getConnection should 'return con', no?
    }

    public void closeConnection() { 
        if (con != null) con.close();
    }

    public ArrayList<Employee> selectAllEmployees() {
        return new ArrayList<Employee>(); // TODO: replace with database stuff
    }

    // etc...

And your main method should not be in that class at all.

public class Controller {

    public Controller() { }

    public static void main(String[] args) {
        EmployeeDAO dao = new EmployeeDAO();
        List<Employee> emps = dao.selectAllEmployees();

        for (Employee e : emps) { 
            // etc...
        }
    }
}
Comments