我正在努力实施这个DOA课程。我添加了getConnection
和closeConnection
方法,但是,在添加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)
答案 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...
}
}
}