1
public C[] getC() throws SQLException, ClassNotFoundException  {
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   String url = "jdbc:odbc:Mydb";
   String user = "user1";
   String password = "password";
   Connection con = DriverManager.getConnection(url,user,password);
   Statement smt= con.createStatement();
   String query = "Select ssn, cname from customer";
   ResultSet rs = smt.executeQuery(query);
   C [] c = new C[getNumberOfCustomers()];
   while (rs.next()){
      String ssn = rs.getString("ssn");
      String customer_name = rs.getString("cname");
   }    
   return custarray;    
}

Here is the code of C:

public class C {

   private String name;
   private String SocialSecurityNumber;

   public C(String name, String SocialSecurityNumber) {
      this.name = name;
      this.SocialSecurityNumber = SocialSecurityNumber;
   }

   public String getName() { return name; }
   public void setName(String name) { this.name = name; }
   public String getSocialSecurityNumber() { return SocialSecurityNumber; }
   public void setSsn(String SocialSecurityNumber ) {
      this.SocialSecurityNumber = SocialSecurityNumber;
   }
}

Here I am having problem in passing ssn and customer_name to the C[] array

Aubin
  • 14,617
  • 9
  • 61
  • 84

2 Answers2

1
C [] c = new C[getNumberOfCustomers()];
int i = 0;
while (rs.next()){
   c[i++] = new C( rs.getString( "cname" ), rs.getString( "ssn" ));
}

FYI: A List<C> will be better than an array of C, the following code use a reusable list, new occurs only when target == null (first time use for example).

public List<C> getC( List<C> target ) throws SQLException,ClassNotFoundException{
   if( target == null ) {
      target = new LinkedList<C>();
   }
   else {
      target.clear();
   }
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   String url = "jdbc:odbc:Mydb";
   String user = "user1";
   String password = "password";
   Connection con = DriverManager.getConnection(url,user,password);
   Statement smt= con.createStatement();
   String query = "Select ssn, cname from customer";
   ResultSet rs = smt.executeQuery(query);
   while( rs.next()){
      target.add( new C( rs.getString("cname"), rs.getString("ssn")));
   } 
   return target;
}
Community
  • 1
  • 1
Aubin
  • 14,617
  • 9
  • 61
  • 84
  • I am getting nullpointerexception for the above.Here is the code public class C { private String name; private String SocialSecurityNumber; public C(String name, String SocialSecurityNumber) { this.name = name; this.SocialSecurityNumber = SocialSecurityNumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSocialSecurityNumber() { return SocialSecurityNumber; } public void setSsn(String SocialSecurityNumber) { this.SocialSecurityNumber = SocialSecurityNumber; } – user2382753 May 14 '13 at 18:04
0

You can do this:

List<C> list = new ArrayList<C>();
while (rs.next()){
    String ssn = rs.getString("ssn");
    String customer_name = rs.getString("cname");
    C c = new C(ssn, customer_name);
    list.add(c);
}
C[] lc = new C[list.size()];
list.toArray(lc);
3d5oN
  • 113
  • 3
  • You are assuming the existence of a particular `C` constructor. What makes you think it actually exists? – PM 77-1 May 14 '13 at 17:54