Welcome

JDBC Tutorial


Using ArrayList and HashMap in JDBC

      In real time Applications data stored in Java collection Objects like ArrayList,Sets or HashMaps etc., and while retreiving Data From the Databases, retreived data stored in ResultSet Object. There should be way to Send Collection Objects to Database, or convert ResultSet Object to UserDefined Classes or Vice Cersa.

For ex: Employee Data Stored in ArrayList
package javaTutorial;
import java.time.LocalDate;
import java.util.Objects;
public class Employee {
		public Employee(int empno, String empName, float salary, LocalDate hireDate, String job, int deptNo,
			int manager) {
		    super();
		    this.empno = empno;
		    this.empName = empName;
		    this.salary = salary;
		    this.hireDate = hireDate;
		    this.job = job;
		    this.deptNo = deptNo;
		    this.manager = manager;
		}
		@Override
		public String toString() {
		    return "Employee [empno=" + empno + ", empName=" + empName + ", salary=" + salary + ", hireDate="
			    + hireDate + ", job=" + job + ", deptNo=" + deptNo + ", manager=" + manager + "]";
		}
		@Override
		public int hashCode() {
		    return Objects.hash(deptNo, empName, empno, hireDate, job, manager, salary);
		}
		@Override
		public boolean equals(Object obj) {
		    if (this == obj)
			return true;
		    if (obj == null)
			return false;
		    if (getClass() != obj.getClass())
			return false;
		    Employee other = (Employee) obj;
		    return deptNo == other.deptNo && Objects.equals(empName, other.empName) && empno == other.empno
			    && Objects.equals(hireDate, other.hireDate) && Objects.equals(job, other.job)
			    && manager == other.manager
			    && Float.floatToIntBits(salary) == Float.floatToIntBits(other.salary);
		}
		public int getEmpno() {
		    return empno;
		}
		public void setEmpno(int empno) {
		    this.empno = empno;
		}
		public String getEmpName() {
		    return empName;
		}
		public void setEmpName(String empName) {
		    this.empName = empName;
		}
		public float getSalary() {
		    return salary;
		}
		public void setSalary(float salary) {
		    this.salary = salary;
		}
		public LocalDate getHireDate() {
		    return hireDate;
		}
		public void setHireDate(LocalDate hireDate) {
		    this.hireDate = hireDate;
		}
		public String getJob() {
		    return job;
		}
		public void setJob(String job) {
		    this.job = job;
		}
		public int getManager() {
		    return manager;
		}
		public void setManager(int manager) {
		    this.manager = manager;
		}
		public int getDeptNo() {
		    return deptNo;
		}
		public void setDeptNo(int deptNo) {
		    this.deptNo = deptNo;
		}

		int empno;
		String empName;
		float  salary;
		LocalDate hireDate;
		String job;
		int deptNo;
		int manager;
};

			ArrayList empList = new ArrayList<>(){
				new Employee(),
				new Employee(),
				new Employee(),
				new Employee(),
			};
			
      		
      		

ADS