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

2
  • 3
    Please post the code of C Commented May 14, 2013 at 17:34
  • 2
    What problem are you having? Commented May 14, 2013 at 17:36

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

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; }
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);

1 Comment

You are assuming the existence of a particular C constructor. What makes you think it actually exists?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.