【问题标题】:Java - Implementing DAOJava - 实现 DAO
【发布时间】:2016-11-08 23:53:24
【问题描述】:

我正在努力实现这个 DOA 类。我添加了 getConnectioncloseConnection 方法,但是,当添加 ArrayList<Employees> 时,我总是会出错。我在下面添加了我的代码。有人给了我一个 UML 图供我使用。

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

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) 处的类型

【问题讨论】:

  • 您是否有理由自己这样做而不是使用 Hibernate 或 MyBatis 之类的框架?
  • 你得到什么错误?
  • 我没有看到您的班级中定义了 getConnectioncloseConnection
  • 我也没有看到任何 ArrayList。
  • 您的主要方法最好完全位于您的模型之外。

标签: java sqlite dao


【解决方案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...
        }
    }
}

【讨论】:

  • 我已将以下代码添加到 getConnection()Connection connection = null;尝试 { Class.forName("org.sqlite.JDBC");连接 = DriverManager.getConnection("jdbc:sqlite:employeeDatabase.sqlite"); } catch ( 异常 e ) { System.err.println( e.getClass().getName() + ": " + e.getMessage() ); System.exit(0); } System.out.println("连接 - 找到数据库驱动");
猜你喜欢
  • 1970-01-01
  • 2012-11-17
  • 1970-01-01
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多