Hello all i want is to display entire content of my database table on Console. I am trying to fetch record from database first and store in ArrayList but it does not display any thing insted it display this:
[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]
I am fully confused to what to do. My code is:
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;}
}
My EmployeeList java code is:
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();
}
}
}
}
My database is:
Employee
EMPN ENAME JOB_TITLE
1 abc xyz
2 pqr mno
3 lmn hij
Please help me i have posted my full code and database where am i going wrong how to display it in a console
You have to implement toString()
in your EmployeeDTO
class. If you are using Eclipse you can generate the method: right click the class -> Source -> Generate toString()...
You need to override toString
in your EmployeeDTO
class. You could use String.format
to do the formatting:
@Override
public String toString() {
return String.format("%d\t%s\t%s", empNo, eName, jobTitle);
}
To print each employee
on a separate line you could use:
for (EmployeeDTO employee : employees) {
System.out.println(employee);
}
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);
}
In this code, you are putting Object into ArrayList, not an actual String/Value into it. That's why you are getting as com.suven.java.EmployeeDTO@970c0e
Looks like you need an implementation of toString()
in your DTO. You'd implement that such that it returns a string consisting of your employee name, job etc... Here's a link to 10 tips for implementing toString() .
I note also that you print the list of employees after you read each employee. I suspect rather that you need to print the employee itself.
System.out.println(employees); // this is the list itself!
You are doing it all right except in the line
System.out.println(employees);
in the while loop.
You cannot display ArrayList like that with out overloading toString() in your EmployeeDTO class.
Instead, to display array list, do this after the while loop
for(EmployeeDTO emp : employees)
{
System.out.println(emp.getEmpNo() + " " + emp.getEName() + " " + emp.getJob());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.