Java - 实现DAO

时间:2016-11-08 23:53:25

标签: java sqlite dao

我正在努力实施这个DOA课程。我添加了getConnectioncloseConnection方法,但是,在添加ArrayList<Employees>时,我总是收到错误消息。我在下面添加了我的代码。我已经获得了一个UML图表。

我做错了什么?有人可能会指出我正确的方向吗?

Class Diagram

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();
            }
        }
    }

}
  

线程“main”中的异常java.lang.Error:未解析的编译   问题:
  员工无法解决类型问题   参数selectallEmployees的非法修饰符;只允许决赛   员工无法解析为EmployeeDAO.main类型(EmployeeDAO.java:61)

1 个答案:

答案 0 :(得分:1)

我认为三角形是受保护的或公共字段。红色框是私人领域。绿色圆圈都是公共方法。顶部圆圈上的C是构造函数。

您的问题代码不遵循......

根据您的图表,您的课程需要从这样开始编写。

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...

你的主要方法根本不应该在那个班级。

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...
        }
    }
}