您好,我只想在控制台上显示我的数据库表的全部内容。我正在尝试首先从数据库中获取记录并存储在 ArrayList 中,但它没有显示任何内容,它显示以下内容:
[com.suven.java.EmployeeDTO@970c0e]
[com.suven.java.EmployeeDTO@970c0e, com.suven.java.EmployeeDTO@987197]
[com.suven.java.EmployeeDTO@970c0e, com.suven.java.EmployeeDTO@987197, com.suven.java.EmployeeDTO@497904]
我完全不知道该怎么做。我的代码是:
EmployeeDTO java file
public class EmployeeDTO
{
//private properties
private int empNo;
private String eName;
private String jobTitle;
//setters
public void setEmpNo(int val){empNo=val;}
public void setEName(String val){eName=val;}
public void setJob(String val){jobTitle=val;}
// getters
public int getEmpNo(){return empNo;}
public String getEName(){return eName;}
public String getJob(){return jobTitle;}
}
我的 EmployeeList java 代码是:
public class EmployeeList
{
public static void main(String argv[])
{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try
{
// Load the JDBC driver
// This can be skipped for Derby, but derbyclient.jar has to be in the CLASSPATH
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String accessFileName = "E:/Database/java";
conn=DriverManager.getConnection("jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+accessFileName+".accdb;");
// Build an SQL String
String sqlQuery = "SELECT * from Employee";
// Create a Statement object
stmt = conn.createStatement();
// Execute SQL and get obtain the ResultSet object
rs = stmt.executeQuery(sqlQuery);
ArrayList<EmployeeDTO> employees = new ArrayList<EmployeeDTO>();
// Process the result set - print Employees
while (rs.next())
{
EmployeeDTO currentEmp = new EmployeeDTO();
currentEmp.setEmpNo(rs.getInt("EMPNO"));
currentEmp.setEName(rs.getString("ENAME"));
currentEmp.setJob(rs.getString("JOB_TITLE"));
employees.add(currentEmp);
System.out.println(employees);
}
}
catch( SQLException se )
{
System.out.println ("SQLError: " + se.getMessage ()+ " code: " + se.getErrorCode ());
}
catch( Exception e )
{
System.out.println(e.getMessage());
e.printStackTrace();
}
finally
{
// clean up the system resources
try
{
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
我的数据库是:
Employee
EMPN ENAME JOB_TITLE
1 abc xyz
2 pqr mno
3 lmn hij
请帮助我,我已经发布了我的完整代码和数据库我哪里出错了如何在控制台中显示它