tags:

views:

184

answers:

1

Possible Duplicate:
how to store the data in a object array, which collected from data base?

The Below code will retrieve data from the database. i want to store the data in a pojo object. It will be appreciative if anyone demonstrate with the code or dont hesitate to edit my code.

I want to know about the creation and implementation of POJO objects. Actually what is meant by POJO object. Jdbc object=new Jdbc(); is this a pojo object. If so means how to declare a object array and how to use it to store array variables.

import java.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Jdbc {
static int id[] = new int[10];
static String name[] = new String[10];
static int salary[] = new int[10];
public static void main(String arg[])
{
try {
Statement stmt;
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/dwr";
Connection con = DriverManager.getConnection(url,"root", "1234");
System.out.println("URL: " + url);
System.out.println("Connection: " + con);
stmt = con.createStatement();
System.out.println("Enter EmpId:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int empId=Integer.parseInt(br.readLine());
System.out.println("Enter Name:");
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
String name1=br1.readLine();
System.out.println("Enter Salary:");
BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));
int salary1=Integer.parseInt(br2.readLine());
stmt.executeUpdate("INSERT INTO employee set EmpId='"+empId+"', Name='"+name1+"',     salary='"+salary1+"';");
stmt =             con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("SELECT * " + "from employee ORDER BY EmpId");
System.out.println("Display all results:");
int i = 0;
while(rs.next()){
id[i]= rs.getInt("Empid");
name[i]= rs.getString("Name");
salary[i]= rs.getInt("Salary");
System.out.println("\n EmpId " + id[i]+ "\n name " + name[i] + "\n salary " + salary[i]);
}

}catch( Exception e ) {
e.printStackTrace();
}
Jdbc pojo = new Jdbc();

}
}
+3  A: 

POJO is Plain Old Java Object. It is a term that was coined when EJBs came into the picture to differentiate between the two (EJBs implemented javax.ejb.*). Here is the wiki link for more on POJO.

I think this is what you are wanting to do.

First Declare a class called Employee.

public class Employee{  
  private Integer empId;  
  private Integer salary;  
  private String name;  
  //add getters and setters for these 3 members.  
}

Then modify your record retrieval in the following way:

if(rs!=null{  
 List<Employee> empList = new ArrayList<Employee>();  
 while(rs.next()){  
    Employee emp = new Employee();  
    emp.setEmpId(rs.getInt("Empid"));  
    emp.setSalary(rs.getInt("Salary"));  
    emp.setName(rs.getString("Name"));  
    empList.add(emp);  
  }  
}

At the end of the traversal empList will contain all the records that you retrieved and each entry in the list will be of type Employee. Then you can use the getter for them to get each individual element (like salary, name, id).

Hope this helps.

CoolBeans
NooBDevelopeR